aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/IO/SharpCifs/Smb/SmbFile.cs
blob: 151ec35c4838a6f7812c2a70f249654d1822d94a (plain)
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
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
// This code is derived from jcifs smb client library <jcifs at samba dot org>
// Ported by J. Arturo <webmaster at komodosoft dot net>
//  
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 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
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using SharpCifs.Dcerpc;
using SharpCifs.Dcerpc.Msrpc;
using SharpCifs.Netbios;
using SharpCifs.Util;
using SharpCifs.Util.Sharpen;

namespace SharpCifs.Smb
{
    /// <summary>This class represents a resource on an SMB network.</summary>
    /// <remarks>
    /// This class represents a resource on an SMB network. Mainly these
    /// resources are files and directories however an <code>SmbFile</code>
    /// may also refer to servers and workgroups. If the resource is a file or
    /// directory the methods of <code>SmbFile</code> follow the behavior of
    /// the well known
    /// <see cref="FilePath">Sharpen.FilePath</see>
    /// class. One fundamental difference
    /// is the usage of a URL scheme [1] to specify the target file or
    /// directory. SmbFile URLs have the following syntax:
    /// <blockquote><pre>
    /// smb://[[[domain;]username[:password]@]server[:port]/[[share/[dir/]file]]][?[param=value[param2=value2[...]]]
    /// </pre></blockquote>
    /// This example:
    /// <blockquote><pre>
    /// smb://storage15/public/foo.txt
    /// </pre></blockquote>
    /// would reference the file <code>foo.txt</code> in the share
    /// <code>public</code> on the server <code>storage15</code>. In addition
    /// to referencing files and directories, jCIFS can also address servers,
    /// and workgroups.
    /// <p>
    /// <font color="#800000"><i>Important: all SMB URLs that represent
    /// workgroups, servers, shares, or directories require a trailing slash '/'.
    /// </i></font>
    /// <p>
    /// When using the <tt>java.net.URL</tt> class with
    /// 'smb://' URLs it is necessary to first call the static
    /// <tt>jcifs.Config.registerSmbURLHandler();</tt> method. This is required
    /// to register the SMB protocol handler.
    /// <p>
    /// The userinfo component of the SMB URL (<tt>domain;user:pass</tt>) must
    /// be URL encoded if it contains reserved characters. According to RFC 2396
    /// these characters are non US-ASCII characters and most meta characters
    /// however jCIFS will work correctly with anything but '@' which is used
    /// to delimit the userinfo component from the server and '%' which is the
    /// URL escape character itself.
    /// <p>
    /// The server
    /// component may a traditional NetBIOS name, a DNS name, or IP
    /// address. These name resolution mechanisms and their resolution order
    /// can be changed (See <a href="../../../resolver.html">Setting Name
    /// Resolution Properties</a>). The servername and path components are
    /// not case sensitive but the domain, username, and password components
    /// are. It is also likely that properties must be specified for jcifs
    /// to function (See <a href="../../overview-summary.html#scp">Setting
    /// JCIFS Properties</a>). Here are some examples of SMB URLs with brief
    /// descriptions of what they do:
    /// <p>[1] This URL scheme is based largely on the <i>SMB
    /// Filesharing URL Scheme</i> IETF draft.
    /// <p><table border="1" cellpadding="3" cellspacing="0" width="100%">
    /// <tr bgcolor="#ccccff">
    /// <td colspan="2"><b>SMB URL Examples</b></td>
    /// <tr><td width="20%"><b>URL</b></td><td><b>Description</b></td></tr>
    /// <tr><td width="20%"><code>smb://users-nyc;miallen:mypass@angus/tmp/</code></td><td>
    /// This URL references a share called <code>tmp</code> on the server
    /// <code>angus</code> as user <code>miallen</code> who's password is
    /// <code>mypass</code>.
    /// </td></tr>
    /// <tr><td width="20%">
    /// <code>smb://Administrator:P%40ss@msmith1/c/WINDOWS/Desktop/foo.txt</code></td><td>
    /// A relativly sophisticated example that references a file
    /// <code>msmith1</code>'s desktop as user <code>Administrator</code>. Notice the '@' is URL encoded with the '%40' hexcode escape.
    /// </td></tr>
    /// <tr><td width="20%"><code>smb://angus/</code></td><td>
    /// This references only a server. The behavior of some methods is different
    /// in this context(e.g. you cannot <code>delete</code> a server) however
    /// as you might expect the <code>list</code> method will list the available
    /// shares on this server.
    /// </td></tr>
    /// <tr><td width="20%"><code>smb://myworkgroup/</code></td><td>
    /// This syntactically is identical to the above example. However if
    /// <code>myworkgroup</code> happends to be a workgroup(which is indeed
    /// suggested by the name) the <code>list</code> method will return
    /// a list of servers that have registered themselves as members of
    /// <code>myworkgroup</code>.
    /// </td></tr>
    /// <tr><td width="20%"><code>smb://</code></td><td>
    /// Just as <code>smb://server/</code> lists shares and
    /// <code>smb://workgroup/</code> lists servers, the <code>smb://</code>
    /// URL lists all available workgroups on a netbios LAN. Again,
    /// in this context many methods are not valid and return default
    /// values(e.g. <code>isHidden</code> will always return false).
    /// </td></tr>
    /// <tr><td width="20%"><code>smb://angus.foo.net/d/jcifs/pipes.doc</code></td><td>
    /// The server name may also be a DNS name as it is in this example. See
    /// <a href="../../../resolver.html">Setting Name Resolution Properties</a>
    /// for details.
    /// </td></tr>
    /// <tr><td width="20%"><code>smb://192.168.1.15/ADMIN$/</code></td><td>
    /// The server name may also be an IP address. See &lt;a
    /// href="../../../resolver.html"&gt;Setting Name Resolution Properties</a>
    /// for details.
    /// </td></tr>
    /// <tr><td width="20%">
    /// <code>smb://domain;username:password@server/share/path/to/file.txt</code></td><td>
    /// A prototypical example that uses all the fields.
    /// </td></tr>
    /// <tr><td width="20%"><code>smb://myworkgroup/angus/ &lt;-- ILLEGAL </code></td><td>
    /// Despite the hierarchial relationship between workgroups, servers, and
    /// filesystems this example is not valid.
    /// </td></tr>
    /// <tr><td width="20%">
    /// <code>smb://server/share/path/to/dir &lt;-- ILLEGAL </code></td><td>
    /// URLs that represent workgroups, servers, shares, or directories require a trailing slash '/'.
    /// </td></tr>
    /// <tr><td width="20%">
    /// <code>smb://MYGROUP/?SERVER=192.168.10.15</code></td><td>
    /// SMB URLs support some query string parameters. In this example
    /// the <code>SERVER</code> parameter is used to override the
    /// server name service lookup to contact the server 192.168.10.15
    /// (presumably known to be a master
    /// browser) for the server list in workgroup <code>MYGROUP</code>.
    /// </td></tr>
    /// </table>
    /// <p>A second constructor argument may be specified to augment the URL
    /// for better programmatic control when processing many files under
    /// a common base. This is slightly different from the corresponding
    /// <code>java.io.File</code> usage; a '/' at the beginning of the second
    /// parameter will still use the server component of the first parameter. The
    /// examples below illustrate the resulting URLs when this second contructor
    /// argument is used.
    /// <p><table border="1" cellpadding="3" cellspacing="0" width="100%">
    /// <tr bgcolor="#ccccff">
    /// <td colspan="3">
    /// <b>Examples Of SMB URLs When Augmented With A Second Constructor Parameter</b></td>
    /// <tr><td width="20%">
    /// <b>First Parameter</b></td><td><b>Second Parameter</b></td><td><b>Result</b></td></tr>
    /// <tr><td width="20%"><code>
    /// smb://host/share/a/b/
    /// </code></td><td width="20%"><code>
    /// c/d/
    /// </code></td><td><code>
    /// smb://host/share/a/b/c/d/
    /// </code></td></tr>
    /// <tr><td width="20%"><code>
    /// smb://host/share/foo/bar/
    /// </code></td><td width="20%"><code>
    /// /share2/zig/zag
    /// </code></td><td><code>
    /// smb://host/share2/zig/zag
    /// </code></td></tr>
    /// <tr><td width="20%"><code>
    /// smb://host/share/foo/bar/
    /// </code></td><td width="20%"><code>
    /// ../zip/
    /// </code></td><td><code>
    /// smb://host/share/foo/zip/
    /// </code></td></tr>
    /// <tr><td width="20%"><code>
    /// smb://host/share/zig/zag
    /// </code></td><td width="20%"><code>
    /// smb://foo/bar/
    /// </code></td><td><code>
    /// smb://foo/bar/
    /// </code></td></tr>
    /// <tr><td width="20%"><code>
    /// smb://host/share/foo/
    /// </code></td><td width="20%"><code>
    /// ../.././.././../foo/
    /// </code></td><td><code>
    /// smb://host/foo/
    /// </code></td></tr>
    /// <tr><td width="20%"><code>
    /// smb://host/share/zig/zag
    /// </code></td><td width="20%"><code>
    /// /
    /// </code></td><td><code>
    /// smb://host/
    /// </code></td></tr>
    /// <tr><td width="20%"><code>
    /// smb://server/
    /// </code></td><td width="20%"><code>
    /// ../
    /// </code></td><td><code>
    /// smb://server/
    /// </code></td></tr>
    /// <tr><td width="20%"><code>
    /// smb://
    /// </code></td><td width="20%"><code>
    /// myworkgroup/
    /// </code></td><td><code>
    /// smb://myworkgroup/
    /// </code></td></tr>
    /// <tr><td width="20%"><code>
    /// smb://myworkgroup/
    /// </code></td><td width="20%"><code>
    /// angus/
    /// </code></td><td><code>
    /// smb://myworkgroup/angus/ &lt;-- ILLEGAL<br />(But if you first create an <tt>SmbFile</tt> with 'smb://workgroup/' and use and use it as the first parameter to a constructor that accepts it with a second <tt>String</tt> parameter jCIFS will factor out the 'workgroup'.)
    /// </code></td></tr>
    /// </table>
    /// <p>Instances of the <code>SmbFile</code> class are immutable; that is,
    /// once created, the abstract pathname represented by an SmbFile object
    /// will never change.
    /// </remarks>
    /// <seealso cref="FilePath">Sharpen.FilePath</seealso>
    public class SmbFile : UrlConnection
    {
        internal const int ORdonly = 0x01;

        internal const int OWronly = 0x02;

        internal const int ORdwr = 0x03;

        internal const int OAppend = 0x04;

        internal const int OCreat = 0x0010;

        internal const int OExcl = 0x0020;

        internal const int OTrunc = 0x0040;

        /// <summary>
        /// When specified as the <tt>shareAccess</tt> constructor parameter,
        /// other SMB clients (including other threads making calls into jCIFS)
        /// will not be permitted to access the target file and will receive "The
        /// file is being accessed by another process" message.
        /// </summary>
        /// <remarks>
        /// When specified as the <tt>shareAccess</tt> constructor parameter,
        /// other SMB clients (including other threads making calls into jCIFS)
        /// will not be permitted to access the target file and will receive "The
        /// file is being accessed by another process" message.
        /// </remarks>
        public const int FileNoShare = 0x00;

        /// <summary>
        /// When specified as the <tt>shareAccess</tt> constructor parameter,
        /// other SMB clients will be permitted to read from the target file while
        /// this file is open.
        /// </summary>
        /// <remarks>
        /// When specified as the <tt>shareAccess</tt> constructor parameter,
        /// other SMB clients will be permitted to read from the target file while
        /// this file is open. This constant may be logically OR'd with other share
        /// access flags.
        /// </remarks>
        public const int FileShareRead = 0x01;

        /// <summary>
        /// When specified as the <tt>shareAccess</tt> constructor parameter,
        /// other SMB clients will be permitted to write to the target file while
        /// this file is open.
        /// </summary>
        /// <remarks>
        /// When specified as the <tt>shareAccess</tt> constructor parameter,
        /// other SMB clients will be permitted to write to the target file while
        /// this file is open. This constant may be logically OR'd with other share
        /// access flags.
        /// </remarks>
        public const int FileShareWrite = 0x02;

        /// <summary>
        /// When specified as the <tt>shareAccess</tt> constructor parameter,
        /// other SMB clients will be permitted to delete the target file while
        /// this file is open.
        /// </summary>
        /// <remarks>
        /// When specified as the <tt>shareAccess</tt> constructor parameter,
        /// other SMB clients will be permitted to delete the target file while
        /// this file is open. This constant may be logically OR'd with other share
        /// access flags.
        /// </remarks>
        public const int FileShareDelete = 0x04;

        /// <summary>
        /// A file with this bit on as returned by <tt>getAttributes()</tt> or set
        /// with <tt>setAttributes()</tt> will be read-only
        /// </summary>
        public const int AttrReadonly = 0x01;

        /// <summary>
        /// A file with this bit on as returned by <tt>getAttributes()</tt> or set
        /// with <tt>setAttributes()</tt> will be hidden
        /// </summary>
        public const int AttrHidden = 0x02;

        /// <summary>
        /// A file with this bit on as returned by <tt>getAttributes()</tt> or set
        /// with <tt>setAttributes()</tt> will be a system file
        /// </summary>
        public const int AttrSystem = 0x04;

        /// <summary>
        /// A file with this bit on as returned by <tt>getAttributes()</tt> is
        /// a volume
        /// </summary>
        public const int AttrVolume = 0x08;

        /// <summary>
        /// A file with this bit on as returned by <tt>getAttributes()</tt> is
        /// a directory
        /// </summary>
        public const int AttrDirectory = 0x10;

        /// <summary>
        /// A file with this bit on as returned by <tt>getAttributes()</tt> or set
        /// with <tt>setAttributes()</tt> is an archived file
        /// </summary>
        public const int AttrArchive = 0x20;

        internal const int AttrCompressed = 0x800;

        internal const int AttrNormal = 0x080;

        internal const int AttrTemporary = 0x100;

        internal const int AttrGetMask = 0x7FFF;

        internal const int AttrSetMask = 0x30A7;

        internal const int DefaultAttrExpirationPeriod = 5000;

        internal static readonly int HashDot = ".".GetHashCode();

        internal static readonly int HashDotDot = "..".GetHashCode();

        //internal static LogStream log = LogStream.GetInstance();
        public LogStream Log
        {
            get { return LogStream.GetInstance(); }
        }

        internal static long AttrExpirationPeriod;

        internal static bool IgnoreCopyToException;

        static SmbFile()
        {
            // Open Function Encoding
            // create if the file does not exist
            // fail if the file exists
            // truncate if the file exists
            // share access
            // file attribute encoding
            // extended file attribute encoding(others same as above)
            /*try
            {
                Sharpen.Runtime.GetType("jcifs.Config");
            }
            catch (TypeLoadException cnfe)
            {
                Sharpen.Runtime.PrintStackTrace(cnfe);
            }*/

            AttrExpirationPeriod = Config.GetLong("jcifs.smb.client.attrExpirationPeriod", DefaultAttrExpirationPeriod
                );
            IgnoreCopyToException = Config.GetBoolean("jcifs.smb.client.ignoreCopyToException"
                , true);
            Dfs = new Dfs();
        }

        /// <summary>
        /// Returned by
        /// <see cref="GetType()">GetType()</see>
        /// if the resource this <tt>SmbFile</tt>
        /// represents is a regular file or directory.
        /// </summary>
        public const int TypeFilesystem = 0x01;

        /// <summary>
        /// Returned by
        /// <see cref="GetType()">GetType()</see>
        /// if the resource this <tt>SmbFile</tt>
        /// represents is a workgroup.
        /// </summary>
        public const int TypeWorkgroup = 0x02;

        /// <summary>
        /// Returned by
        /// <see cref="GetType()">GetType()</see>
        /// if the resource this <tt>SmbFile</tt>
        /// represents is a server.
        /// </summary>
        public const int TypeServer = 0x04;

        /// <summary>
        /// Returned by
        /// <see cref="GetType()">GetType()</see>
        /// if the resource this <tt>SmbFile</tt>
        /// represents is a share.
        /// </summary>
        public const int TypeShare = 0x08;

        /// <summary>
        /// Returned by
        /// <see cref="GetType()">GetType()</see>
        /// if the resource this <tt>SmbFile</tt>
        /// represents is a named pipe.
        /// </summary>
        public const int TypeNamedPipe = 0x10;

        /// <summary>
        /// Returned by
        /// <see cref="GetType()">GetType()</see>
        /// if the resource this <tt>SmbFile</tt>
        /// represents is a printer.
        /// </summary>
        public const int TypePrinter = 0x20;

        /// <summary>
        /// Returned by
        /// <see cref="GetType()">GetType()</see>
        /// if the resource this <tt>SmbFile</tt>
        /// represents is a communications device.
        /// </summary>
        public const int TypeComm = 0x40;

        private string _canon;

        private string _share;

        private long _createTime;

        private long _lastModified;

        private int _attributes;

        private long _attrExpiration;

        private long _size;

        private long _sizeExpiration;

        private bool _isExists;

        private int _shareAccess = FileShareRead | FileShareWrite | FileShareDelete;

        private bool _enableDfs = Config.GetBoolean("jcifs.smb.client.enabledfs", false);

        private SmbComBlankResponse _blankResp;

        private DfsReferral _dfsReferral;

        protected internal static Dfs Dfs;

        internal NtlmPasswordAuthentication Auth;

        internal SmbTree Tree;

        internal string Unc;

        internal int Fid;

        internal int Type;

        internal bool Opened;

        internal int TreeNum;

        public bool EnableDfs
        {
            get { return _enableDfs; }
            set { _enableDfs = value; }
        }

        /// <summary>
        /// Constructs an SmbFile representing a resource on an SMB network such as
        /// a file or directory.
        /// </summary>
        /// <remarks>
        /// Constructs an SmbFile representing a resource on an SMB network such as
        /// a file or directory. See the description and examples of smb URLs above.
        /// </remarks>
        /// <param name="url">A URL string</param>
        /// <exception cref="System.UriFormatException">
        /// If the <code>parent</code> and <code>child</code> parameters
        /// do not follow the prescribed syntax
        /// </exception>
        public SmbFile(string url)
            : this(new Uri(url))
        {
        }

        /// <summary>
        /// Constructs an SmbFile representing a resource on an SMB network such
        /// as a file or directory.
        /// </summary>
        /// <remarks>
        /// Constructs an SmbFile representing a resource on an SMB network such
        /// as a file or directory. The second parameter is a relative path from
        /// the <code>parent SmbFile</code>. See the description above for examples
        /// of using the second <code>name</code> parameter.
        /// </remarks>
        /// <param name="context">A base <code>SmbFile</code></param>
        /// <param name="name">A path string relative to the <code>parent</code> paremeter</param>
        /// <exception cref="System.UriFormatException">
        /// If the <code>parent</code> and <code>child</code> parameters
        /// do not follow the prescribed syntax
        /// </exception>
        /// <exception cref="UnknownHostException">If the server or workgroup of the <tt>context</tt> file cannot be determined
        /// 	</exception>
        public SmbFile(SmbFile context, string name)
            : this(context.IsWorkgroup0
                () ? new Uri("smb://" + name) : new Uri(context.Url.AbsoluteUri + name),
                context.Auth)
        {

            this._enableDfs = context.EnableDfs;

            if (!context.IsWorkgroup0())
            {
                Addresses = context.Addresses;

                if (context._share != null)
                {
                    Tree = context.Tree;
                    _dfsReferral = context._dfsReferral;
                }                
            }
        }

        /// <summary>
        /// Constructs an SmbFile representing a resource on an SMB network such
        /// as a file or directory.
        /// </summary>
        /// <remarks>
        /// Constructs an SmbFile representing a resource on an SMB network such
        /// as a file or directory. The second parameter is a relative path from
        /// the <code>parent</code>. See the description above for examples of
        /// using the second <code>chile</code> parameter.
        /// </remarks>
        /// <param name="context">A URL string</param>
        /// <param name="name">A path string relative to the <code>context</code> paremeter</param>
        /// <exception cref="System.UriFormatException">
        /// If the <code>context</code> and <code>name</code> parameters
        /// do not follow the prescribed syntax
        /// </exception>
        /*public SmbFile(string context, string name)
            : this(new Uri(new Uri(null, context), name))
        {
        }*/

        public SmbFile(string context, string name)
            : this(new Uri(context + name))
        {

        }


        /// <summary>
        /// Constructs an SmbFile representing a resource on an SMB network such
        /// as a file or directory.
        /// </summary>
        /// <remarks>
        /// Constructs an SmbFile representing a resource on an SMB network such
        /// as a file or directory.
        /// </remarks>
        /// <param name="url">A URL string</param>
        /// <param name="auth">The credentials the client should use for authentication</param>
        /// <exception cref="System.UriFormatException">If the <code>url</code> parameter does not follow the prescribed syntax
        /// 	</exception>
        public SmbFile(string url, NtlmPasswordAuthentication auth)
            : this(new Uri(url, UriKind.RelativeOrAbsolute),
             auth)
        {

        }

        /// <summary>Constructs an SmbFile representing a file on an SMB network.</summary>
        /// <remarks>
        /// Constructs an SmbFile representing a file on an SMB network. The
        /// <tt>shareAccess</tt> parameter controls what permissions other
        /// clients have when trying to access the same file while this instance
        /// is still open. This value is either <tt>FILE_NO_SHARE</tt> or any
        /// combination of <tt>FILE_SHARE_READ</tt>, <tt>FILE_SHARE_WRITE</tt>,
        /// and <tt>FILE_SHARE_DELETE</tt> logically OR'd together.
        /// </remarks>
        /// <param name="url">A URL string</param>
        /// <param name="auth">The credentials the client should use for authentication</param>
        /// <param name="shareAccess">Specifies what access other clients have while this file is open.
        /// 	</param>
        /// <exception cref="System.UriFormatException">If the <code>url</code> parameter does not follow the prescribed syntax
        /// 	</exception>
        public SmbFile(string url, NtlmPasswordAuthentication auth, int shareAccess)
            : this
                (new Uri(url), auth)
        {
            // Initially null; set by getUncPath; dir must end with '/'
            // Can be null
            // For getDfsPath() and getServerWithDfs()
            // Cannot be null
            // Initially null
            // Initially null; set by getUncPath; never ends with '/'
            // Initially 0; set by open()
            if ((shareAccess & ~(FileShareRead | FileShareWrite | FileShareDelete)) !=
                0)
            {
                throw new RuntimeException("Illegal shareAccess parameter");
            }
            this._shareAccess = shareAccess;
        }

        /// <summary>
        /// Constructs an SmbFile representing a resource on an SMB network such
        /// as a file or directory.
        /// </summary>
        /// <remarks>
        /// Constructs an SmbFile representing a resource on an SMB network such
        /// as a file or directory. The second parameter is a relative path from
        /// the <code>context</code>. See the description above for examples of
        /// using the second <code>name</code> parameter.
        /// </remarks>
        /// <param name="context">A URL string</param>
        /// <param name="name">A path string relative to the <code>context</code> paremeter</param>
        /// <param name="auth">The credentials the client should use for authentication</param>
        /// <exception cref="System.UriFormatException">
        /// If the <code>context</code> and <code>name</code> parameters
        /// do not follow the prescribed syntax
        /// </exception>
        public SmbFile(string context, string name, NtlmPasswordAuthentication auth)
            : this
                (new Uri(context + name)
                , auth)
        {

        }


        /// <summary>
        /// Constructs an SmbFile representing a resource on an SMB network such
        /// as a file or directory.
        /// </summary>
        /// <remarks>
        /// Constructs an SmbFile representing a resource on an SMB network such
        /// as a file or directory. The second parameter is a relative path from
        /// the <code>context</code>. See the description above for examples of
        /// using the second <code>name</code> parameter. The <tt>shareAccess</tt>
        /// parameter controls what permissions other clients have when trying
        /// to access the same file while this instance is still open. This
        /// value is either <tt>FILE_NO_SHARE</tt> or any combination
        /// of <tt>FILE_SHARE_READ</tt>, <tt>FILE_SHARE_WRITE</tt>, and
        /// <tt>FILE_SHARE_DELETE</tt> logically OR'd together.
        /// </remarks>
        /// <param name="context">A URL string</param>
        /// <param name="name">A path string relative to the <code>context</code> paremeter</param>
        /// <param name="auth">The credentials the client should use for authentication</param>
        /// <param name="shareAccess">Specifies what access other clients have while this file is open.
        /// 	</param>
        /// <exception cref="System.UriFormatException">
        /// If the <code>context</code> and <code>name</code> parameters
        /// do not follow the prescribed syntax
        /// </exception>
        public SmbFile(string context, string name, NtlmPasswordAuthentication auth, int
            shareAccess)
            : this(new Uri(context + name), auth)
        {
            if ((shareAccess & ~(FileShareRead | FileShareWrite | FileShareDelete)) !=
                0)
            {
                throw new RuntimeException("Illegal shareAccess parameter");
            }
            this._shareAccess = shareAccess;
        }

        /// <summary>
        /// Constructs an SmbFile representing a resource on an SMB network such
        /// as a file or directory.
        /// </summary>
        /// <remarks>
        /// Constructs an SmbFile representing a resource on an SMB network such
        /// as a file or directory. The second parameter is a relative path from
        /// the <code>context</code>. See the description above for examples of
        /// using the second <code>name</code> parameter. The <tt>shareAccess</tt>
        /// parameter controls what permissions other clients have when trying
        /// to access the same file while this instance is still open. This
        /// value is either <tt>FILE_NO_SHARE</tt> or any combination
        /// of <tt>FILE_SHARE_READ</tt>, <tt>FILE_SHARE_WRITE</tt>, and
        /// <tt>FILE_SHARE_DELETE</tt> logically OR'd together.
        /// </remarks>
        /// <param name="context">A base <code>SmbFile</code></param>
        /// <param name="name">A path string relative to the <code>context</code> file path</param>
        /// <param name="shareAccess">Specifies what access other clients have while this file is open.
        /// 	</param>
        /// <exception cref="System.UriFormatException">
        /// If the <code>context</code> and <code>name</code> parameters
        /// do not follow the prescribed syntax
        /// </exception>
        /// <exception cref="UnknownHostException"></exception>
        public SmbFile(SmbFile context, string name, int shareAccess)
            : this(context.IsWorkgroup0() ? new Uri("smb://" + name) : new Uri(
                          context.Url.AbsoluteUri + name), context.Auth)
        {
            if ((shareAccess & ~(FileShareRead | FileShareWrite | FileShareDelete)) !=
                0)
            {
                throw new RuntimeException("Illegal shareAccess parameter");
            }

            if (!context.IsWorkgroup0())
            {
                this.Addresses = context.Addresses;

                if (context._share != null || context.Tree != null)
                {
                    Tree = context.Tree;
                    _dfsReferral = context._dfsReferral;
                }
            }

            this._shareAccess = shareAccess;
            this._enableDfs = context.EnableDfs;
        }

        /// <summary>
        /// Constructs an SmbFile representing a resource on an SMB network such
        /// as a file or directory from a <tt>URL</tt> object.
        /// </summary>
        /// <remarks>
        /// Constructs an SmbFile representing a resource on an SMB network such
        /// as a file or directory from a <tt>URL</tt> object.
        /// </remarks>
        /// <param name="url">The URL of the target resource</param>
        protected SmbFile(Uri url)
            : this(url, new NtlmPasswordAuthentication(url.GetUserInfo
                ()))
        {
        }

        /// <summary>
        /// Constructs an SmbFile representing a resource on an SMB network such
        /// as a file or directory from a <tt>URL</tt> object and an
        /// <tt>NtlmPasswordAuthentication</tt> object.
        /// </summary>
        /// <remarks>
        /// Constructs an SmbFile representing a resource on an SMB network such
        /// as a file or directory from a <tt>URL</tt> object and an
        /// <tt>NtlmPasswordAuthentication</tt> object.
        /// </remarks>
        /// <param name="url">The URL of the target resource</param>
        /// <param name="auth">The credentials the client should use for authentication</param>
        public SmbFile(Uri url, NtlmPasswordAuthentication auth)
        {
            this.Auth = auth ?? new NtlmPasswordAuthentication(url.GetUserInfo());
            Url = url;
            GetUncPath0();
        }

        /// <exception cref="System.UriFormatException"></exception>
        /// <exception cref="UnknownHostException"></exception>
        /*internal SmbFile(Jcifs.Smb.SmbFile context, string name, int type, int attributes
            , long createTime, long lastModified, long size)
            : this(context.IsWorkgroup0() ?
                new Uri(null, "smb://" + name + "/") : new Uri(context.url,
                name + ((attributes & ATTR_DIRECTORY) > 0 ? "/" : string.Empty)))*/
        internal SmbFile(SmbFile context, string name, int type, int attributes
            , long createTime, long lastModified, long size)
            : this(context.IsWorkgroup0() ?
                new Uri("smb://" + name + "/") : new Uri(context.Url.AbsoluteUri +
                name + ((attributes & AttrDirectory) > 0 ? "/" : string.Empty)))
        {
            Auth = context.Auth;
            if (context._share != null)
            {
                Tree = context.Tree;
                _dfsReferral = context._dfsReferral;
            }
            int last = name.Length - 1;
            if (name[last] == '/')
            {
                name = Runtime.Substring(name, 0, last);
            }
            if (context._share == null)
            {
                Unc = "\\";
            }
            else
            {
                if (context.Unc.Equals("\\"))
                {
                    Unc = '\\' + name;
                }
                else
                {
                    Unc = context.Unc + '\\' + name;
                }
            }

            if (!context.IsWorkgroup0())
            {
                Addresses = context.Addresses;
            }

            this._enableDfs = context.EnableDfs;

            this.Type = type;
            this._attributes = attributes;
            this._createTime = createTime;
            this._lastModified = lastModified;
            this._size = size;
            _isExists = true;
            _attrExpiration = _sizeExpiration = Runtime.CurrentTimeMillis() + AttrExpirationPeriod;
        }

        private SmbComBlankResponse Blank_resp()
        {
            if (_blankResp == null)
            {
                _blankResp = new SmbComBlankResponse();
            }
            return _blankResp;
        }

        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        internal virtual void ResolveDfs(ServerMessageBlock request)
        {
            if (!_enableDfs)
            {
                Connect0();
                return;
            }

            if (request is SmbComClose)
            {
                return;
            }
            Connect0();
            DfsReferral dr = Dfs.Resolve(Tree.Session.transport.TconHostName, Tree.Share, Unc
                , Auth);
            if (dr != null)
            {
                string service = null;
                if (request != null)
                {
                    switch (request.Command)
                    {
                        case ServerMessageBlock.SmbComTransaction:
                        case ServerMessageBlock.SmbComTransaction2:
                            {
                                switch (((SmbComTransaction)request).SubCommand & 0xFF)
                                {
                                    case SmbComTransaction.Trans2GetDfsReferral:
                                        {
                                            break;
                                        }

                                    default:
                                        {
                                            service = "A:";
                                            break;
                                        }
                                }
                                break;
                            }

                        default:
                            {
                                service = "A:";
                                break;
                            }
                    }
                }
                DfsReferral start = dr;
                SmbException se = null;
                do
                {
                    try
                    {
                        if (Log.Level >= 2)
                        {
                            Log.WriteLine("DFS redirect: " + dr);
                        }
                        UniAddress addr = UniAddress.GetByName(dr.Server);
                        SmbTransport trans = SmbTransport.GetSmbTransport(addr, Url.Port);
                        trans.Connect();
                        Tree = trans.GetSmbSession(Auth).GetSmbTree(dr.Share, service);
                        if (dr != start && dr.Key != null)
                        {
                            dr.Map.Put(dr.Key, dr);
                        }
                        se = null;
                        break;
                    }
                    catch (IOException ioe)
                    {
                        if (ioe is SmbException)
                        {
                            se = (SmbException)ioe;
                        }
                        else
                        {
                            se = new SmbException(dr.Server, ioe);
                        }
                    }
                    dr = dr.Next;
                }
                while (dr != start);
                if (se != null)
                {
                    throw se;
                }
                if (Log.Level >= 3)
                {
                    Log.WriteLine(dr);
                }
                _dfsReferral = dr;
                if (dr.PathConsumed < 0)
                {
                    dr.PathConsumed = 0;
                }
                else
                {
                    if (dr.PathConsumed > Unc.Length)
                    {
                        dr.PathConsumed = Unc.Length;
                    }
                }
                string dunc = Runtime.Substring(Unc, dr.PathConsumed);
                if (dunc.Equals(string.Empty))
                {
                    dunc = "\\";
                }
                if (!dr.Path.Equals(string.Empty))
                {
                    dunc = "\\" + dr.Path + dunc;
                }
                Unc = dunc;
                if (request != null && request.Path != null && request.Path.EndsWith("\\") && dunc
                    .EndsWith("\\") == false)
                {
                    dunc += "\\";
                }
                if (request != null)
                {
                    request.Path = dunc;
                    request.Flags2 |= SmbConstants.Flags2ResolvePathsInDfs;
                }
            }
            else
            {
                if (Tree.InDomainDfs && !(request is NtTransQuerySecurityDesc) && !(request is SmbComClose
                    ) && !(request is SmbComFindClose2))
                {
                    throw new SmbException(NtStatus.NtStatusNotFound, false);
                }
                if (request != null)
                {
                    request.Flags2 &= ~SmbConstants.Flags2ResolvePathsInDfs;
                }
            }
        }

        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        internal virtual void Send(ServerMessageBlock request, ServerMessageBlock response
            )
        {
            for (; ; )
            {
                ResolveDfs(request);
                try
                {
                    Tree.Send(request, response);
                    break;
                }
                catch (DfsReferral dre)
                {
                    if (dre.ResolveHashes)
                    {
                        throw;
                    }
                    request.Reset();
                }
            }
        }

        internal static string QueryLookup(string query, string param)
        {
            char[] instr = query.ToCharArray();
            int i;
            int ch;
            int st;
            int eq;
            st = eq = 0;
            for (i = 0; i < instr.Length; i++)
            {
                ch = instr[i];
                if (ch == '&')
                {
                    if (eq > st)
                    {
                        string p = new string(instr, st, eq - st);
                        if (Runtime.EqualsIgnoreCase(p, param))
                        {
                            eq++;
                            return new string(instr, eq, i - eq);
                        }
                    }
                    st = i + 1;
                }
                else
                {
                    if (ch == '=')
                    {
                        eq = i;
                    }
                }
            }
            if (eq > st)
            {
                string p = new string(instr, st, eq - st);
                if (Runtime.EqualsIgnoreCase(p, param))
                {
                    eq++;
                    return new string(instr, eq, instr.Length - eq);
                }
            }
            return null;
        }

        internal UniAddress[] Addresses;

        internal int AddressIndex;

        /// <exception cref="UnknownHostException"></exception>
        internal virtual UniAddress GetAddress()
        {
            if (AddressIndex == 0)
            {
                return GetFirstAddress();
            }
            return Addresses[AddressIndex - 1];
        }

        /// <exception cref="UnknownHostException"></exception>
        internal virtual UniAddress GetFirstAddress()
        {
            AddressIndex = 0;
            string host = Url.GetHost();
            string path = Url.AbsolutePath;
            string query = Url.GetQuery();

            if (Addresses != null && Addresses.Length > 0)
            {
                return GetNextAddress();
            }

            if (query != null)
            {
                string server = QueryLookup(query, "server");
                if (!string.IsNullOrEmpty(server))
                {
                    Addresses = new UniAddress[1];
                    Addresses[0] = UniAddress.GetByName(server);
                    return GetNextAddress();
                }
                string address = QueryLookup(query, "address");
                if (!string.IsNullOrEmpty(address))
                {
                    byte[] ip = Extensions.GetAddressByName(address).GetAddressBytes();
                    Addresses = new UniAddress[1];
                    //addresses[0] = new UniAddress(IPAddress.Parse(host, ip));
                    Addresses[0] = new UniAddress(IPAddress.Parse(host));
                    return GetNextAddress();
                }
            }
            if (host.Length == 0)
            {
                try
                {
                    NbtAddress addr = NbtAddress.GetByName(NbtAddress.MasterBrowserName, 0x01, null);
                    Addresses = new UniAddress[1];
                    Addresses[0] = UniAddress.GetByName(addr.GetHostAddress());
                }
                catch (UnknownHostException uhe)
                {
                    NtlmPasswordAuthentication.InitDefaults();
                    if (NtlmPasswordAuthentication.DefaultDomain.Equals("?"))
                    {
                        throw;
                    }
                    Addresses = UniAddress.GetAllByName(NtlmPasswordAuthentication.DefaultDomain, true
                        );
                }
            }
            else
            {
                if (path.Length == 0 || path.Equals("/"))
                {
                    Addresses = UniAddress.GetAllByName(host, true);
                }
                else
                {
                    Addresses = UniAddress.GetAllByName(host, false);
                }
            }
            return GetNextAddress();
        }

        internal virtual UniAddress GetNextAddress()
        {
            UniAddress addr = null;
            if (AddressIndex < Addresses.Length)
            {
                addr = Addresses[AddressIndex++];
            }
            return addr;
        }

        internal virtual bool HasNextAddress()
        {
            return AddressIndex < Addresses.Length;
        }

        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        internal virtual void Connect0()
        {
            try
            {
                Connect();
            }
            catch (UnknownHostException uhe)
            {
                throw new SmbException("Failed to connect to server", uhe);
            }
            catch (SmbException se)
            {
                throw;
            }
            catch (IOException ioe)
            {
                throw new SmbException("Failed to connect to server", ioe);
            }
        }

        /// <exception cref="System.IO.IOException"></exception>
        internal virtual void DoConnect()
        {
            SmbTransport trans;
            UniAddress addr;
            addr = GetAddress();

            if (Tree != null && Tree.Session.transport.Address.Equals(addr))
            {
                trans = Tree.Session.transport;
            }
            else
            {
                trans = SmbTransport.GetSmbTransport(addr, Url.Port);
                Tree = trans.GetSmbSession(Auth).GetSmbTree(_share, null);
            }


            string hostName = GetServerWithDfs();
            if (_enableDfs)
            {
                Tree.InDomainDfs = Dfs.Resolve(hostName, Tree.Share, null, Auth) != null;
            }
            if (Tree.InDomainDfs)
            {
                Tree.ConnectionState = 2;
            }
            try
            {
                if (Log.Level >= 3)
                {
                    Log.WriteLine("doConnect: " + addr);
                }
                Tree.TreeConnect(null, null);
            }
            catch (SmbAuthException sae)
            {
                NtlmPasswordAuthentication a;
                SmbSession ssn;
                if (_share == null)
                {
                    // IPC$ - try "anonymous" credentials
                    ssn = trans.GetSmbSession(NtlmPasswordAuthentication.Null);
                    Tree = ssn.GetSmbTree(null, null);
                    Tree.TreeConnect(null, null);
                }
                else
                {
                    if ((a = NtlmAuthenticator.RequestNtlmPasswordAuthentication(Url.ToString(), sae)
                        ) != null)
                    {
                        Auth = a;
                        ssn = trans.GetSmbSession(Auth);
                        Tree = ssn.GetSmbTree(_share, null);
                        Tree.InDomainDfs = Dfs.Resolve(hostName, Tree.Share, null, Auth) != null;
                        if (Tree.InDomainDfs)
                        {
                            Tree.ConnectionState = 2;
                        }
                        Tree.TreeConnect(null, null);
                    }
                    else
                    {
                        if (Log.Level >= 1 && HasNextAddress())
                        {
                            Runtime.PrintStackTrace(sae, Log);
                        }
                        throw;
                    }
                }
            }
        }

        /// <summary>It is not necessary to call this method directly.</summary>
        /// <remarks>
        /// It is not necessary to call this method directly. This is the
        /// <tt>URLConnection</tt> implementation of <tt>connect()</tt>.
        /// </remarks>
        /// <exception cref="System.IO.IOException"></exception>
        public void Connect()
        {
            SmbTransport trans;
            SmbSession ssn;
            UniAddress addr;
            if (IsConnected())
            {
                return;
            }
            GetUncPath0();
            GetFirstAddress();
            for (; ; )
            {
                try
                {
                    DoConnect();
                    return;
                }
                catch (SmbAuthException sae)
                {
                    throw;
                }
                catch (SmbException se)
                {
                    // Prevents account lockout on servers with multiple IPs
                    if (GetNextAddress() == null)
                    {
                        throw;
                    }
                    else
                    {
                        RemoveCurrentAddress();
                    }

                    if (Log.Level >= 3)
                    {
                        Runtime.PrintStackTrace(se, Log);
                    }
                }
            }
        }

        internal virtual bool IsConnected()
        {
            return Tree != null && Tree.ConnectionState == 2;
        }

        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        internal virtual int Open0(int flags, int access, int attrs, int options)
        {
            int f;
            Connect0();
            if (Log.Level >= 3)
            {
                Log.WriteLine("open0: " + Unc);
            }
            if (Tree.Session.transport.HasCapability(SmbConstants.CapNtSmbs))
            {
                SmbComNtCreateAndXResponse response = new SmbComNtCreateAndXResponse();
                SmbComNtCreateAndX request = new SmbComNtCreateAndX(Unc, flags, access, _shareAccess
                    , attrs, options, null);
                if (this is SmbNamedPipe)
                {
                    request.Flags0 |= 0x16;
                    request.DesiredAccess |= 0x20000;
                    response.IsExtended = true;
                }
                Send(request, response);
                f = response.Fid;
                _attributes = response.ExtFileAttributes & AttrGetMask;
                _attrExpiration = Runtime.CurrentTimeMillis() + AttrExpirationPeriod;
                _isExists = true;
            }
            else
            {
                SmbComOpenAndXResponse response = new SmbComOpenAndXResponse();
                Send(new SmbComOpenAndX(Unc, access, flags, null), response);
                f = response.Fid;
            }
            return f;
        }

        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        internal virtual void Open(int flags, int access, int attrs, int options)
        {
            if (IsOpen())
            {
                return;
            }
            Fid = Open0(flags, access, attrs, options);
            Opened = true;
            TreeNum = Tree.TreeNum;
        }

        internal virtual bool IsOpen()
        {
            bool ans = Opened && IsConnected() && TreeNum == Tree.TreeNum;
            return ans;
        }

        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        internal virtual void Close(int f, long lastWriteTime)
        {
            if (Log.Level >= 3)
            {
                Log.WriteLine("close: " + f);
            }
            Send(new SmbComClose(f, lastWriteTime), Blank_resp());
        }

        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        internal virtual void Close(long lastWriteTime)
        {
            if (IsOpen() == false)
            {
                return;
            }
            Close(Fid, lastWriteTime);
            Opened = false;
        }

        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        internal virtual void Close()
        {
            Close(0L);
        }

        /// <summary>
        /// Returns the <tt>NtlmPasswordAuthentication</tt> object used as
        /// credentials with this file or pipe.
        /// </summary>
        /// <remarks>
        /// Returns the <tt>NtlmPasswordAuthentication</tt> object used as
        /// credentials with this file or pipe. This can be used to retrieve the
        /// username for example:
        /// <tt>
        /// String username = f.getPrincipal().getName();
        /// </tt>
        /// The <tt>Principal</tt> object returned will never be <tt>null</tt>
        /// however the username can be <tt>null</tt> indication anonymous
        /// credentials were used (e.g. some IPC$ services).
        /// </remarks>
        public virtual Principal GetPrincipal()
        {
            return Auth;
        }

        /// <summary>Returns the last component of the target URL.</summary>
        /// <remarks>
        /// Returns the last component of the target URL. This will
        /// effectively be the name of the file or directory represented by this
        /// <code>SmbFile</code> or in the case of URLs that only specify a server
        /// or workgroup, the server or workgroup will be returned. The name of
        /// the root URL <code>smb://</code> is also <code>smb://</code>. If this
        /// <tt>SmbFile</tt> refers to a workgroup, server, share, or directory,
        /// the name will include a trailing slash '/' so that composing new
        /// <tt>SmbFile</tt>s will maintain the trailing slash requirement.
        /// </remarks>
        /// <returns>
        /// The last component of the URL associated with this SMB
        /// resource or <code>smb://</code> if the resource is <code>smb://</code>
        /// itself.
        /// </returns>
        public virtual string GetName()
        {
            GetUncPath0();
            if (_canon.Length > 1)
            {
                int i = _canon.Length - 2;
                while (_canon[i] != '/')
                {
                    i--;
                }
                return Runtime.Substring(_canon, i + 1);
            }
            if (_share != null)
            {
                return _share + '/';
            }
            if (Url.GetHost().Length > 0)
            {
                return Url.GetHost() + '/';
            }
            return "smb://";
        }

        /// <summary>
        /// Everything but the last component of the URL representing this SMB
        /// resource is effectivly it's parent.
        /// </summary>
        /// <remarks>
        /// Everything but the last component of the URL representing this SMB
        /// resource is effectivly it's parent. The root URL <code>smb://</code>
        /// does not have a parent. In this case <code>smb://</code> is returned.
        /// </remarks>
        /// <returns>
        /// The parent directory of this SMB resource or
        /// <code>smb://</code> if the resource refers to the root of the URL
        /// hierarchy which incedentally is also <code>smb://</code>.
        /// </returns>
        public virtual string GetParent()
        {
            string str = Url.Authority;
            if (str.Length > 0)
            {
                StringBuilder sb = new StringBuilder("smb://");
                sb.Append(str);
                GetUncPath0();
                if (_canon.Length > 1)
                {
                    sb.Append(_canon);
                }
                else
                {
                    sb.Append('/');
                }
                str = sb.ToString();
                int i = str.Length - 2;
                while (str[i] != '/')
                {
                    i--;
                }
                return Runtime.Substring(str, 0, i + 1);
            }
            return "smb://";
        }

        /// <summary>Returns the full uncanonicalized URL of this SMB resource.</summary>
        /// <remarks>
        /// Returns the full uncanonicalized URL of this SMB resource. An
        /// <code>SmbFile</code> constructed with the result of this method will
        /// result in an <code>SmbFile</code> that is equal to the original.
        /// </remarks>
        /// <returns>The uncanonicalized full URL of this SMB resource.</returns>
        public virtual string GetPath()
        {
            return Url.ToString();
        }

        internal virtual string GetUncPath0()
        {
            if (Unc == null)
            {
                char[] instr = Url.LocalPath.ToCharArray();
                char[] outstr = new char[instr.Length];
                int length = instr.Length;
                int i;
                int o;
                int state;

                state = 0;
                o = 0;
                for (i = 0; i < length; i++)
                {
                    switch (state)
                    {
                        case 0:
                            {
                                if (instr[i] != '/')
                                {
                                    return null;
                                }
                                outstr[o++] = instr[i];
                                state = 1;
                                break;
                            }

                        case 1:
                            {
                                if (instr[i] == '/')
                                {
                                    break;
                                }
                                if (instr[i] == '.' && ((i + 1) >= length || instr[i + 1] == '/'))
                                {
                                    i++;
                                    break;
                                }
                                if ((i + 1) < length && instr[i] == '.' && instr[i + 1] == '.' && ((i + 2) >= length
                                                                                               || instr[i + 2] == '/'))
                                {
                                    i += 2;
                                    if (o == 1)
                                    {
                                        break;
                                    }
                                    do
                                    {
                                        o--;
                                    }
                                    while (o > 1 && outstr[o - 1] != '/');
                                    break;
                                }
                                state = 2;
                                goto case 2;
                            }

                        case 2:
                            {
                                if (instr[i] == '/')
                                {
                                    state = 1;
                                }
                                outstr[o++] = instr[i];
                                break;
                            }
                    }
                }
                _canon = new string(outstr, 0, o);
                if (o > 1)
                {
                    o--;
                    i = _canon.IndexOf('/', 1);
                    if (i < 0)
                    {
                        _share = Runtime.Substring(_canon, 1);
                        Unc = "\\";
                    }
                    else
                    {
                        if (i == o)
                        {
                            _share = Runtime.Substring(_canon, 1, i);
                            Unc = "\\";
                        }
                        else
                        {
                            _share = Runtime.Substring(_canon, 1, i);
                            Unc = Runtime.Substring(_canon, i, outstr[o] == '/' ? o : o + 1);
                            Unc = Unc.Replace('/', '\\');
                        }
                    }
                }
                else
                {
                    _share = null;
                    Unc = "\\";
                }
            }
            return Unc;
        }

        /// <summary>Retuns the Windows UNC style path with backslashs intead of forward slashes.
        /// 	</summary>
        /// <remarks>Retuns the Windows UNC style path with backslashs intead of forward slashes.
        /// 	</remarks>
        /// <returns>The UNC path.</returns>
        public virtual string GetUncPath()
        {
            GetUncPath0();
            if (_share == null)
            {
                return "\\\\" + Url.GetHost();
            }
            return "\\\\" + Url.GetHost() + _canon.Replace('/', '\\');
        }

        /// <summary>
        /// Returns the full URL of this SMB resource with '.' and '..' components
        /// factored out.
        /// </summary>
        /// <remarks>
        /// Returns the full URL of this SMB resource with '.' and '..' components
        /// factored out. An <code>SmbFile</code> constructed with the result of
        /// this method will result in an <code>SmbFile</code> that is equal to
        /// the original.
        /// </remarks>
        /// <returns>The canonicalized URL of this SMB resource.</returns>
        public virtual string GetCanonicalPath()
        {
            string str = Url.Authority;
            GetUncPath0();
            if (str.Length > 0)
            {
                return "smb://" + Url.Authority + _canon;
            }
            return "smb://";
        }

        /// <summary>Retrieves the share associated with this SMB resource.</summary>
        /// <remarks>
        /// Retrieves the share associated with this SMB resource. In
        /// the case of <code>smb://</code>, <code>smb://workgroup/</code>,
        /// and <code>smb://server/</code> URLs which do not specify a share,
        /// <code>null</code> will be returned.
        /// </remarks>
        /// <returns>The share component or <code>null</code> if there is no share</returns>
        public virtual string GetShare()
        {
            return _share;
        }

        internal virtual string GetServerWithDfs()
        {
            if (_dfsReferral != null)
            {
                return _dfsReferral.Server;
            }
            return GetServer();
        }

        /// <summary>Retrieve the hostname of the server for this SMB resource.</summary>
        /// <remarks>
        /// Retrieve the hostname of the server for this SMB resource. If this
        /// <code>SmbFile</code> references a workgroup, the name of the workgroup
        /// is returned. If this <code>SmbFile</code> refers to the root of this
        /// SMB network hierarchy, <code>null</code> is returned.
        /// </remarks>
        /// <returns>
        /// The server or workgroup name or <code>null</code> if this
        /// <code>SmbFile</code> refers to the root <code>smb://</code> resource.
        /// </returns>
        public virtual string GetServer()
        {
            string str = Url.GetHost();
            if (str.Length == 0)
            {
                return null;
            }
            return str;
        }

        /// <summary>Returns type of of object this <tt>SmbFile</tt> represents.</summary>
        /// <remarks>Returns type of of object this <tt>SmbFile</tt> represents.</remarks>
        /// <returns>
        /// <tt>TYPE_FILESYSTEM, TYPE_WORKGROUP, TYPE_SERVER, TYPE_SHARE,
        /// TYPE_PRINTER, TYPE_NAMED_PIPE</tt>, or <tt>TYPE_COMM</tt>.
        /// </returns>
        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        public new virtual int GetType()
        {
            if (Type == 0)
            {
                if (GetUncPath0().Length > 1)
                {
                    Type = TypeFilesystem;
                }
                else
                {
                    if (_share != null)
                    {
                        // treeConnect good enough to test service type
                        Connect0();
                        if (_share.Equals("IPC$"))
                        {
                            Type = TypeNamedPipe;
                        }
                        else
                        {
                            if (Tree.Service.Equals("LPT1:"))
                            {
                                Type = TypePrinter;
                            }
                            else
                            {
                                if (Tree.Service.Equals("COMM"))
                                {
                                    Type = TypeComm;
                                }
                                else
                                {
                                    Type = TypeShare;
                                }
                            }
                        }
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(Url.Authority))
                        {
                            Type = TypeWorkgroup;
                        }
                        else
                        {
                            UniAddress addr;
                            try
                            {
                                addr = GetAddress();
                            }
                            catch (UnknownHostException uhe)
                            {
                                throw new SmbException(Url.ToString(), uhe);
                            }
                            if (addr.GetAddress() is NbtAddress)
                            {
                                int code = ((NbtAddress)addr.GetAddress()).GetNameType();
                                if (code == 0x1d || code == 0x1b)
                                {
                                    Type = TypeWorkgroup;
                                    return Type;
                                }
                            }
                            Type = TypeServer;
                        }
                    }
                }
            }
            return Type;
        }

        /// <exception cref="UnknownHostException"></exception>
        internal virtual bool IsWorkgroup0()
        {
            if (Type == TypeWorkgroup || Url.GetHost().Length == 0)
            {
                Type = TypeWorkgroup;
                return true;
            }
            GetUncPath0();
            if (_share == null)
            {
                UniAddress addr = GetAddress();
                if (addr.GetAddress() is NbtAddress)
                {
                    int code = ((NbtAddress)addr.GetAddress()).GetNameType();
                    if (code == 0x1d || code == 0x1b)
                    {
                        Type = TypeWorkgroup;
                        return true;
                    }
                }
                Type = TypeServer;
            }
            return false;
        }

        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        internal virtual IInfo QueryPath(string path, int infoLevel)
        {
            Connect0();
            if (Log.Level >= 3)
            {
                Log.WriteLine("queryPath: " + path);
            }
            if (Tree.Session.transport.HasCapability(SmbConstants.CapNtSmbs))
            {
                Trans2QueryPathInformationResponse response = new Trans2QueryPathInformationResponse
                    (infoLevel);
                Send(new Trans2QueryPathInformation(path, infoLevel), response);
                return response.Info;
            }
            else
            {
                SmbComQueryInformationResponse response = new SmbComQueryInformationResponse(Tree
                    .Session.transport.Server.ServerTimeZone * 1000 * 60L);
                Send(new SmbComQueryInformation(path), response);
                return response;
            }
        }

        /// <summary>Tests to see if the SMB resource exists.</summary>
        /// <remarks>
        /// Tests to see if the SMB resource exists. If the resource refers
        /// only to a server, this method determines if the server exists on the
        /// network and is advertising SMB services. If this resource refers to
        /// a workgroup, this method determines if the workgroup name is valid on
        /// the local SMB network. If this <code>SmbFile</code> refers to the root
        /// <code>smb://</code> resource <code>true</code> is always returned. If
        /// this <code>SmbFile</code> is a traditional file or directory, it will
        /// be queried for on the specified server as expected.
        /// </remarks>
        /// <returns>
        /// <code>true</code> if the resource exists or is alive or
        /// <code>false</code> otherwise
        /// </returns>
        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        public virtual bool Exists()
        {
            if (_attrExpiration > Runtime.CurrentTimeMillis())
            {
                return _isExists;
            }
            _attributes = AttrReadonly | AttrDirectory;
            _createTime = 0L;
            _lastModified = 0L;
            _isExists = false;
            try
            {
                if (Url.GetHost().Length == 0)
                {
                }
                else
                {
                    if (_share == null)
                    {
                        if (GetType() == TypeWorkgroup)
                        {
                            UniAddress.GetByName(Url.GetHost(), true);
                        }
                        else
                        {
                            UniAddress.GetByName(Url.GetHost()).GetHostName();
                        }
                    }
                    else
                    {
                        if (GetUncPath0().Length == 1 || Runtime.EqualsIgnoreCase(_share, "IPC$"))
                        {
                            Connect0();
                        }
                        else
                        {
                            // treeConnect is good enough
                            IInfo info = QueryPath(GetUncPath0(), Trans2QueryPathInformationResponse.SMB_QUERY_FILE_BASIC_INFO
                                );
                            _attributes = info.GetAttributes();
                            _createTime = info.GetCreateTime();
                            _lastModified = info.GetLastWriteTime();
                        }
                    }
                }
                _isExists = true;
            }
            catch (UnknownHostException)
            {
            }
            catch (SmbException se)
            {
                switch (se.GetNtStatus())
                {
                    case NtStatus.NtStatusNoSuchFile:
                    case NtStatus.NtStatusObjectNameInvalid:
                    case NtStatus.NtStatusObjectNameNotFound:
                    case NtStatus.NtStatusObjectPathNotFound:
                        {
                            break;
                        }

                    default:
                        {
                            throw;
                        }
                }
            }
            _attrExpiration = Runtime.CurrentTimeMillis() + AttrExpirationPeriod;
            return _isExists;
        }

        /// <summary>
        /// Tests to see if the file this <code>SmbFile</code> represents can be
        /// read.
        /// </summary>
        /// <remarks>
        /// Tests to see if the file this <code>SmbFile</code> represents can be
        /// read. Because any file, directory, or other resource can be read if it
        /// exists, this method simply calls the <code>exists</code> method.
        /// </remarks>
        /// <returns><code>true</code> if the file is read-only</returns>
        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        public virtual bool CanRead()
        {
            if (GetType() == TypeNamedPipe)
            {
                // try opening the pipe for reading?
                return true;
            }
            return Exists();
        }

        // try opening and catch sharing violation?
        /// <summary>
        /// Tests to see if the file this <code>SmbFile</code> represents
        /// exists and is not marked read-only.
        /// </summary>
        /// <remarks>
        /// Tests to see if the file this <code>SmbFile</code> represents
        /// exists and is not marked read-only. By default, resources are
        /// considered to be read-only and therefore for <code>smb://</code>,
        /// <code>smb://workgroup/</code>, and <code>smb://server/</code> resources
        /// will be read-only.
        /// </remarks>
        /// <returns>
        /// <code>true</code> if the resource exists is not marked
        /// read-only
        /// </returns>
        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        public virtual bool CanWrite()
        {
            if (GetType() == TypeNamedPipe)
            {
                // try opening the pipe for writing?
                return true;
            }
            return Exists() && (_attributes & AttrReadonly) == 0;
        }

        /// <summary>Tests to see if the file this <code>SmbFile</code> represents is a directory.
        /// 	</summary>
        /// <remarks>Tests to see if the file this <code>SmbFile</code> represents is a directory.
        /// 	</remarks>
        /// <returns><code>true</code> if this <code>SmbFile</code> is a directory</returns>
        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        public virtual bool IsDirectory()
        {
            if (GetUncPath0().Length == 1)
            {
                return true;
            }
            if (!Exists())
            {
                return false;
            }
            return (_attributes & AttrDirectory) == AttrDirectory;
        }

        /// <summary>Tests to see if the file this <code>SmbFile</code> represents is not a directory.
        /// 	</summary>
        /// <remarks>Tests to see if the file this <code>SmbFile</code> represents is not a directory.
        /// 	</remarks>
        /// <returns><code>true</code> if this <code>SmbFile</code> is not a directory</returns>
        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        public virtual bool IsFile()
        {
            if (GetUncPath0().Length == 1)
            {
                return false;
            }
            Exists();
            return (_attributes & AttrDirectory) == 0;
        }

        /// <summary>
        /// Tests to see if the file this SmbFile represents is marked as
        /// hidden.
        /// </summary>
        /// <remarks>
        /// Tests to see if the file this SmbFile represents is marked as
        /// hidden. This method will also return true for shares with names that
        /// end with '$' such as <code>IPC$</code> or <code>C$</code>.
        /// </remarks>
        /// <returns><code>true</code> if the <code>SmbFile</code> is marked as being hidden</returns>
        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        public virtual bool IsHidden()
        {
            if (_share == null)
            {
                return false;
            }
            if (GetUncPath0().Length == 1)
            {
                if (_share.EndsWith("$"))
                {
                    return true;
                }
                return false;
            }
            Exists();
            return (_attributes & AttrHidden) == AttrHidden;
        }

        /// <summary>
        /// If the path of this <code>SmbFile</code> falls within a DFS volume,
        /// this method will return the referral path to which it maps.
        /// </summary>
        /// <remarks>
        /// If the path of this <code>SmbFile</code> falls within a DFS volume,
        /// this method will return the referral path to which it maps. Otherwise
        /// <code>null</code> is returned.
        /// </remarks>
        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        public virtual string GetDfsPath()
        {
            ResolveDfs(null);
            if (_dfsReferral == null)
            {
                return null;
            }
            string path = "smb:/" + _dfsReferral.Server + "/" + _dfsReferral.Share + Unc;
            path = path.Replace('\\', '/');
            if (IsDirectory())
            {
                path += '/';
            }
            return path;
        }

        /// <summary>Retrieve the time this <code>SmbFile</code> was created.</summary>
        /// <remarks>
        /// Retrieve the time this <code>SmbFile</code> was created. The value
        /// returned is suitable for constructing a
        /// <see cref="System.DateTime">System.DateTime</see>
        /// object
        /// (i.e. seconds since Epoch 1970). Times should be the same as those
        /// reported using the properties dialog of the Windows Explorer program.
        /// For Win95/98/Me this is actually the last write time. It is currently
        /// not possible to retrieve the create time from files on these systems.
        /// </remarks>
        /// <returns>
        /// The number of milliseconds since the 00:00:00 GMT, January 1,
        /// 1970 as a <code>long</code> value
        /// </returns>
        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        public virtual long CreateTime()
        {
            if (GetUncPath0().Length > 1)
            {
                Exists();
                return _createTime;
            }
            return 0L;
        }

        /// <summary>
        /// Retrieve the last time the file represented by this
        /// <code>SmbFile</code> was modified.
        /// </summary>
        /// <remarks>
        /// Retrieve the last time the file represented by this
        /// <code>SmbFile</code> was modified. The value returned is suitable for
        /// constructing a
        /// <see cref="System.DateTime">System.DateTime</see>
        /// object (i.e. seconds since Epoch
        /// 1970). Times should be the same as those reported using the properties
        /// dialog of the Windows Explorer program.
        /// </remarks>
        /// <returns>
        /// The number of milliseconds since the 00:00:00 GMT, January 1,
        /// 1970 as a <code>long</code> value
        /// </returns>
        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        public virtual long LastModified()
        {
            if (GetUncPath0().Length > 1)
            {
                Exists();
                return _lastModified;
            }
            return 0L;
        }

        /// <summary>List the contents of this SMB resource.</summary>
        /// <remarks>
        /// List the contents of this SMB resource. The list returned by this
        /// method will be;
        /// <ul>
        /// <li> files and directories contained within this resource if the
        /// resource is a normal disk file directory,
        /// <li> all available NetBIOS workgroups or domains if this resource is
        /// the top level URL <code>smb://</code>,
        /// <li> all servers registered as members of a NetBIOS workgroup if this
        /// resource refers to a workgroup in a <code>smb://workgroup/</code> URL,
        /// <li> all browseable shares of a server including printers, IPC
        /// services, or disk volumes if this resource is a server URL in the form
        /// <code>smb://server/</code>,
        /// <li> or <code>null</code> if the resource cannot be resolved.
        /// </ul>
        /// </remarks>
        /// <returns>
        /// A <code>String[]</code> array of files and directories,
        /// workgroups, servers, or shares depending on the context of the
        /// resource URL
        /// </returns>
        /// <exception cref="SmbException"></exception>
        public virtual string[] List()
        {
            return List("*", AttrDirectory | AttrHidden | AttrSystem, null, null);
        }

        /// <summary>List the contents of this SMB resource.</summary>
        /// <remarks>
        /// List the contents of this SMB resource. The list returned will be
        /// identical to the list returned by the parameterless <code>list()</code>
        /// method minus filenames filtered by the specified filter.
        /// </remarks>
        /// <param name="filter">a filename filter to exclude filenames from the results</param>
        /// <exception cref="SmbException"># @return An array of filenames</exception>
        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        public virtual string[] List(ISmbFilenameFilter filter)
        {
            return List("*", AttrDirectory | AttrHidden | AttrSystem, filter, null);
        }

        /// <summary>
        /// List the contents of this SMB resource as an array of
        /// <code>SmbFile</code> objects.
        /// </summary>
        /// <remarks>
        /// List the contents of this SMB resource as an array of
        /// <code>SmbFile</code> objects. This method is much more efficient than
        /// the regular <code>list</code> method when querying attributes of each
        /// file in the result set.
        /// <p>
        /// The list of <code>SmbFile</code>s returned by this method will be;
        /// <ul>
        /// <li> files and directories contained within this resource if the
        /// resource is a normal disk file directory,
        /// <li> all available NetBIOS workgroups or domains if this resource is
        /// the top level URL <code>smb://</code>,
        /// <li> all servers registered as members of a NetBIOS workgroup if this
        /// resource refers to a workgroup in a <code>smb://workgroup/</code> URL,
        /// <li> all browseable shares of a server including printers, IPC
        /// services, or disk volumes if this resource is a server URL in the form
        /// <code>smb://server/</code>,
        /// <li> or <code>null</code> if the resource cannot be resolved.
        /// </ul>
        /// </remarks>
        /// <returns>
        /// An array of <code>SmbFile</code> objects representing file
        /// and directories, workgroups, servers, or shares depending on the context
        /// of the resource URL
        /// </returns>
        /// <exception cref="SmbException"></exception>
        public virtual SmbFile[] ListFiles()
        {
            return ListFiles("*", AttrDirectory | AttrHidden | AttrSystem, null, null);
        }

        /// <summary>
        /// The CIFS protocol provides for DOS "wildcards" to be used as
        /// a performance enhancement.
        /// </summary>
        /// <remarks>
        /// The CIFS protocol provides for DOS "wildcards" to be used as
        /// a performance enhancement. The client does not have to filter
        /// the names and the server does not have to return all directory
        /// entries.
        /// <p>
        /// The wildcard expression may consist of two special meta
        /// characters in addition to the normal filename characters. The '*'
        /// character matches any number of characters in part of a name. If
        /// the expression begins with one or more '?'s then exactly that
        /// many characters will be matched whereas if it ends with '?'s
        /// it will match that many characters <i>or less</i>.
        /// <p>
        /// Wildcard expressions will not filter workgroup names or server names.
        /// <blockquote><pre>
        /// winnt&gt; ls c?o
        /// clock.avi                  -rw--      82944 Mon Oct 14 1996 1:38 AM
        /// Cookies                    drw--          0 Fri Nov 13 1998 9:42 PM
        /// 2 items in 5ms
        /// </pre></blockquote>
        /// </remarks>
        /// <param name="wildcard">a wildcard expression</param>
        /// <exception cref="SmbException">SmbException</exception>
        /// <returns>
        /// An array of <code>SmbFile</code> objects representing file
        /// and directories, workgroups, servers, or shares depending on the context
        /// of the resource URL
        /// </returns>
        /// <exception cref="SmbException"></exception>
        public virtual SmbFile[] ListFiles(string wildcard)
        {
            return ListFiles(wildcard, AttrDirectory | AttrHidden | AttrSystem, null, null
                );
        }

        /// <summary>List the contents of this SMB resource.</summary>
        /// <remarks>
        /// List the contents of this SMB resource. The list returned will be
        /// identical to the list returned by the parameterless <code>listFiles()</code>
        /// method minus files filtered by the specified filename filter.
        /// </remarks>
        /// <param name="filter">a filter to exclude files from the results</param>
        /// <returns>An array of <tt>SmbFile</tt> objects</returns>
        /// <exception cref="SmbException">SmbException</exception>
        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        public virtual SmbFile[] ListFiles(ISmbFilenameFilter filter)
        {
            return ListFiles("*", AttrDirectory | AttrHidden | AttrSystem, filter, null);
        }

        /// <summary>List the contents of this SMB resource.</summary>
        /// <remarks>
        /// List the contents of this SMB resource. The list returned will be
        /// identical to the list returned by the parameterless <code>listFiles()</code>
        /// method minus filenames filtered by the specified filter.
        /// </remarks>
        /// <param name="filter">a file filter to exclude files from the results</param>
        /// <returns>An array of <tt>SmbFile</tt> objects</returns>
        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        public virtual SmbFile[] ListFiles(ISmbFileFilter filter)
        {
            return ListFiles("*", AttrDirectory | AttrHidden | AttrSystem, null, filter);
        }

        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        internal virtual string[] List(string wildcard, int searchAttributes, ISmbFilenameFilter
             fnf, ISmbFileFilter ff)
        {
            List<object> list = new List<object>();
            DoEnum(list, false, wildcard, searchAttributes, fnf, ff);

            return Collections.ToArray<string>(list); //Collections.ToArray<string>(list);
        }

        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        internal virtual SmbFile[] ListFiles(string wildcard, int searchAttributes
            , ISmbFilenameFilter fnf, ISmbFileFilter ff)
        {
            List<object> list = new List<object>();
            DoEnum(list, true, wildcard, searchAttributes, fnf, ff);

            return Collections.ToArray<SmbFile>(list); //Collections.ToArray<SmbFile>(list);
        }

        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        internal virtual void DoEnum(List<object> list, bool files, string wildcard, int searchAttributes
            , ISmbFilenameFilter fnf, ISmbFileFilter ff)
        {
            if (ff != null && ff is DosFileFilter)
            {
                DosFileFilter dff = (DosFileFilter)ff;
                if (dff.Wildcard != null)
                {
                    wildcard = dff.Wildcard;
                }
                searchAttributes = dff.Attributes;
            }
            try
            {
                int hostlen = Url.GetHost() != null ? Url.GetHost().Length : 0;
                if (hostlen == 0 || GetType() == TypeWorkgroup)
                {
                    DoNetServerEnum(list, files, wildcard, searchAttributes, fnf, ff);
                }
                else
                {
                    if (_share == null)
                    {
                        DoShareEnum(list, files, wildcard, searchAttributes, fnf, ff);
                    }
                    else
                    {
                        DoFindFirstNext(list, files, wildcard, searchAttributes, fnf, ff);
                    }
                }
            }
            catch (UnknownHostException uhe)
            {
                throw new SmbException(Url.ToString(), uhe);
            }
            catch (UriFormatException mue)
            {
                throw new SmbException(Url.ToString(), mue);
            }
        }

        private void RemoveCurrentAddress()
        {
            if (AddressIndex >= 1)
            {
                UniAddress[] aux = new UniAddress[Addresses.Length - 1];

                Array.Copy(Addresses, 1, aux, 0, Addresses.Length - 1);

                Addresses = aux;
                AddressIndex--;
            }
        }

        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        /// <exception cref="UnknownHostException"></exception>
        /// <exception cref="System.UriFormatException"></exception>
        internal virtual void DoShareEnum(List<object> list, bool files, string wildcard, int
             searchAttributes, ISmbFilenameFilter fnf, ISmbFileFilter ff)
        {
            string p = Url.AbsolutePath;
            IOException last = null;
            IFileEntry[] entries;
            UniAddress addr;
            IFileEntry e;
            Hashtable map;
            if (p.LastIndexOf('/') != (p.Length - 1))
            {
                throw new SmbException(Url + " directory must end with '/'");
            }
            if (GetType() != TypeServer)
            {
                throw new SmbException("The requested list operations is invalid: " + Url);
            }
            map = new Hashtable();
            if (_enableDfs && Dfs.IsTrustedDomain(GetServer(), Auth))
            {
                try
                {
                    entries = DoDfsRootEnum();
                    for (int ei = 0; ei < entries.Length; ei++)
                    {
                        e = entries[ei];
                        if (map.ContainsKey(e) == false)
                        {
                            map.Put(e, e);
                        }
                    }
                }
                catch (IOException ioe)
                {
                    if (Log.Level >= 4)
                    {
                        Runtime.PrintStackTrace(ioe, Log);
                    }
                }
            }
            addr = GetFirstAddress();
            while (addr != null)
            {
                try
                {
                    last = null;

                    DoConnect();
                    try
                    {
                        entries = DoMsrpcShareEnum();
                    }
                    catch (IOException ioe)
                    {
                        if (Log.Level >= 3)
                        {
                            Runtime.PrintStackTrace(ioe, Log);
                        }
                        entries = DoNetShareEnum();
                    }
                    for (int ei = 0; ei < entries.Length; ei++)
                    {
                        e = entries[ei];
                        if (map.ContainsKey(e) == false)
                        {
                            map.Put(e, e);
                        }
                    }
                    break;
                }
                catch (IOException ioe)
                {
                    if (Log.Level >= 3)
                    {
                        Runtime.PrintStackTrace(ioe, Log);
                    }
                    last = ioe;

                    if (!(ioe is SmbAuthException))
                    {
                        RemoveCurrentAddress();

                        addr = GetNextAddress();
                    }
                    else
                    {
                        break;
                    }
                }

            }
            if (last != null && map.Count == 0)
            {
                if (last is SmbException == false)
                {
                    throw new SmbException(Url.ToString(), last);
                }
                throw (SmbException)last;
            }
            //Iterator iter = map.Keys.Iterator();
            //while (iter.HasNext())
            foreach (var item in map.Keys)
            {
                e = (IFileEntry)item;
                string name = e.GetName();
                if (fnf != null && fnf.Accept(this, name) == false)
                {
                    continue;
                }
                if (name.Length > 0)
                {
                    // if !files we don't need to create SmbFiles here
                    SmbFile f = new SmbFile(this, name, e.GetType(), AttrReadonly
                         | AttrDirectory, 0L, 0L, 0L);
                    if (ff != null && ff.Accept(f) == false)
                    {
                        continue;
                    }
                    if (files)
                    {
                        list.Add(f);
                    }
                    else
                    {
                        list.Add(name);
                    }
                }
            }
        }

        /// <exception cref="System.IO.IOException"></exception>
        internal virtual IFileEntry[] DoDfsRootEnum()
        {
            MsrpcDfsRootEnum rpc;
            DcerpcHandle handle = null;
            IFileEntry[] entries;
            handle = DcerpcHandle.GetHandle("ncacn_np:" + GetAddress().GetHostAddress() + "[\\PIPE\\netdfs]"
                , Auth);
            try
            {
                rpc = new MsrpcDfsRootEnum(GetServer());
                handle.Sendrecv(rpc);
                if (rpc.Retval != 0)
                {
                    throw new SmbException(rpc.Retval, true);
                }
                return rpc.GetEntries();
            }
            finally
            {
                try
                {
                    handle.Close();
                }
                catch (IOException ioe)
                {
                    if (Log.Level >= 4)
                    {
                        Runtime.PrintStackTrace(ioe, Log);
                    }
                }
            }
        }

        /// <exception cref="System.IO.IOException"></exception>
        internal virtual IFileEntry[] DoMsrpcShareEnum()
        {
            MsrpcShareEnum rpc;
            DcerpcHandle handle;
            rpc = new MsrpcShareEnum(Url.GetHost());
            handle = DcerpcHandle.GetHandle("ncacn_np:" + GetAddress().GetHostAddress() + "[\\PIPE\\srvsvc]"
                , Auth);
            try
            {
                handle.Sendrecv(rpc);
                if (rpc.Retval != 0)
                {
                    throw new SmbException(rpc.Retval, true);
                }
                return rpc.GetEntries();
            }
            finally
            {
                try
                {
                    handle.Close();
                }
                catch (IOException ioe)
                {
                    if (Log.Level >= 4)
                    {
                        Runtime.PrintStackTrace(ioe, Log);
                    }
                }
            }
        }

        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        internal virtual IFileEntry[] DoNetShareEnum()
        {
            SmbComTransaction req = new NetShareEnum();
            SmbComTransactionResponse resp = new NetShareEnumResponse();
            Send(req, resp);
            if (resp.Status != WinError.ErrorSuccess)
            {
                throw new SmbException(resp.Status, true);
            }
            return resp.Results;
        }

        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        /// <exception cref="UnknownHostException"></exception>
        /// <exception cref="System.UriFormatException"></exception>
        internal virtual void DoNetServerEnum(List<object> list, bool files, string wildcard
            , int searchAttributes, ISmbFilenameFilter fnf, ISmbFileFilter ff)
        {
            int listType = Url.GetHost().Length == 0 ? 0 : GetType();
            SmbComTransaction req;
            SmbComTransactionResponse resp;
            if (listType == 0)
            {
                Connect0();
                req = new NetServerEnum2(Tree.Session.transport.Server.OemDomainName, NetServerEnum2
                    .SvTypeDomainEnum);
                resp = new NetServerEnum2Response();
            }
            else
            {
                if (listType == TypeWorkgroup)
                {
                    req = new NetServerEnum2(Url.GetHost(), NetServerEnum2.SvTypeAll);
                    resp = new NetServerEnum2Response();
                }
                else
                {
                    throw new SmbException("The requested list operations is invalid: " + Url);
                }
            }
            bool more;
            do
            {
                int n;
                Send(req, resp);
                if (resp.Status != WinError.ErrorSuccess && resp.Status != WinError.ErrorMoreData)
                {
                    throw new SmbException(resp.Status, true);
                }
                more = resp.Status == WinError.ErrorMoreData;
                n = more ? resp.NumEntries - 1 : resp.NumEntries;
                for (int i = 0; i < n; i++)
                {
                    IFileEntry e = resp.Results[i];
                    string name = e.GetName();
                    if (fnf != null && fnf.Accept(this, name) == false)
                    {
                        continue;
                    }
                    if (name.Length > 0)
                    {
                        // if !files we don't need to create SmbFiles here
                        SmbFile f = new SmbFile(this, name, e.GetType(), AttrReadonly
                             | AttrDirectory, 0L, 0L, 0L);
                        if (ff != null && ff.Accept(f) == false)
                        {
                            continue;
                        }
                        if (files)
                        {
                            list.Add(f);
                        }
                        else
                        {
                            list.Add(name);
                        }
                    }
                }
                if (GetType() != TypeWorkgroup)
                {
                    break;
                }
                req.SubCommand = unchecked(SmbComTransaction.NetServerEnum3);
                req.Reset(0, ((NetServerEnum2Response)resp).LastName);
                resp.Reset();
            }
            while (more);
        }

        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        /// <exception cref="UnknownHostException"></exception>
        /// <exception cref="System.UriFormatException"></exception>
        internal virtual void DoFindFirstNext(List<object> list, bool files, string wildcard
            , int searchAttributes, ISmbFilenameFilter fnf, ISmbFileFilter ff)
        {
            SmbComTransaction req;
            Trans2FindFirst2Response resp;
            int sid;
            string path = GetUncPath0();
            string p = Url.AbsolutePath;
            if (p.LastIndexOf('/') != (p.Length - 1))
            {
                throw new SmbException(Url + " directory must end with '/'");
            }
            req = new Trans2FindFirst2(path, wildcard, searchAttributes);
            resp = new Trans2FindFirst2Response();
            if (Log.Level >= 3)
            {
                Log.WriteLine("doFindFirstNext: " + req.Path);
            }
            Send(req, resp);
            sid = resp.Sid;
            req = new Trans2FindNext2(sid, resp.ResumeKey, resp.LastName);
            resp.SubCommand = SmbComTransaction.Trans2FindNext2;
            for (; ; )
            {
                for (int i = 0; i < resp.NumEntries; i++)
                {
                    IFileEntry e = resp.Results[i];
                    string name = e.GetName();
                    if (name.Length < 3)
                    {
                        int h = name.GetHashCode();
                        if (h == HashDot || h == HashDotDot)
                        {
                            if (name.Equals(".") || name.Equals(".."))
                            {
                                continue;
                            }
                        }
                    }
                    if (fnf != null && fnf.Accept(this, name) == false)
                    {
                        continue;
                    }
                    if (name.Length > 0)
                    {
                        SmbFile f = new SmbFile(this, name, TypeFilesystem, e.GetAttributes
                            (), e.CreateTime(), e.LastModified(), e.Length());
                        if (ff != null && ff.Accept(f) == false)
                        {
                            continue;
                        }
                        if (files)
                        {
                            list.Add(f);
                        }
                        else
                        {
                            list.Add(name);
                        }
                    }
                }
                if (resp.IsEndOfSearch || resp.NumEntries == 0)
                {
                    break;
                }
                req.Reset(resp.ResumeKey, resp.LastName);
                resp.Reset();
                Send(req, resp);
            }
            try
            {
                Send(new SmbComFindClose2(sid), Blank_resp());
            }
            catch (SmbException se)
            {
                if (Log.Level >= 4)
                {
                    Runtime.PrintStackTrace(se, Log);
                }
            }
        }

        /// <summary>
        /// Changes the name of the file this <code>SmbFile</code> represents to the name
        /// designated by the <code>SmbFile</code> argument.
        /// </summary>
        /// <remarks>
        /// Changes the name of the file this <code>SmbFile</code> represents to the name
        /// designated by the <code>SmbFile</code> argument.
        /// <p/>
        /// <i>Remember: <code>SmbFile</code>s are immutible and therefore
        /// the path associated with this <code>SmbFile</code> object will not
        /// change). To access the renamed file it is necessary to construct a
        /// new <tt>SmbFile</tt></i>.
        /// </remarks>
        /// <param name="dest">An <code>SmbFile</code> that represents the new pathname</param>
        /// <exception cref="System.ArgumentNullException">If the <code>dest</code> argument is <code>null</code>
        /// 	</exception>
        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        public virtual void RenameTo(SmbFile dest)
        {
            if (GetUncPath0().Length == 1 || dest.GetUncPath0().Length == 1)
            {
                throw new SmbException("Invalid operation for workgroups, servers, or shares");
            }
            ResolveDfs(null);
            dest.ResolveDfs(null);
            if (!Tree.Equals(dest.Tree))
            {
                throw new SmbException("Invalid operation for workgroups, servers, or shares");
            }
            if (Log.Level >= 3)
            {
                Log.WriteLine("renameTo: " + Unc + " -> " + dest.Unc);
            }
            _attrExpiration = _sizeExpiration = 0;
            dest._attrExpiration = 0;
            Send(new SmbComRename(Unc, dest.Unc), Blank_resp());
        }

        internal class WriterThread : Thread
        {
            internal byte[] B;

            internal int N;

            internal long Off;

            internal bool Ready;

            internal SmbFile Dest;

            internal SmbException E;

            internal bool UseNtSmbs;

            internal SmbComWriteAndX Reqx;

            internal SmbComWrite Req;

            internal ServerMessageBlock Resp;

            /// <exception cref="SharpCifs.Smb.SmbException"></exception>
            public WriterThread(SmbFile enclosing)
                : base("JCIFS-WriterThread")
            {
                this._enclosing = enclosing;
                UseNtSmbs = this._enclosing.Tree.Session.transport.HasCapability(SmbConstants.CapNtSmbs);
                if (UseNtSmbs)
                {
                    Reqx = new SmbComWriteAndX();
                    Resp = new SmbComWriteAndXResponse();
                }
                else
                {
                    Req = new SmbComWrite();
                    Resp = new SmbComWriteResponse();
                }
                Ready = false;
            }

            internal virtual void Write(byte[] b, int n, SmbFile dest, long off)
            {
                lock (this)
                {
                    this.B = b;
                    this.N = n;
                    this.Dest = dest;
                    this.Off = off;
                    Ready = false;
                    Runtime.Notify(this);
                }
            }

            public override void Run()
            {
                lock (this)
                {
                    try
                    {
                        for (; ; )
                        {
                            Runtime.Notify(this);
                            Ready = true;
                            while (Ready)
                            {
                                Runtime.Wait(this);
                            }
                            if (N == -1)
                            {
                                return;
                            }
                            if (UseNtSmbs)
                            {
                                Reqx.SetParam(Dest.Fid, Off, N, B, 0, N);
                                Dest.Send(Reqx, Resp);
                            }
                            else
                            {
                                Req.SetParam(Dest.Fid, Off, N, B, 0, N);
                                Dest.Send(Req, Resp);
                            }
                        }
                    }
                    catch (SmbException e)
                    {
                        this.E = e;
                    }
                    catch (Exception x)
                    {
                        E = new SmbException("WriterThread", x);
                    }
                    Runtime.Notify(this);
                }
            }

            private readonly SmbFile _enclosing;
        }

        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        internal virtual void CopyTo0(SmbFile dest, byte[][] b, int bsize, WriterThread
             w, SmbComReadAndX req, SmbComReadAndXResponse resp)
        {
            int i;
            if (_attrExpiration < Runtime.CurrentTimeMillis())
            {
                _attributes = AttrReadonly | AttrDirectory;
                _createTime = 0L;
                _lastModified = 0L;
                _isExists = false;
                IInfo info = QueryPath(GetUncPath0(), Trans2QueryPathInformationResponse.SMB_QUERY_FILE_BASIC_INFO
                    );
                _attributes = info.GetAttributes();
                _createTime = info.GetCreateTime();
                _lastModified = info.GetLastWriteTime();
                _isExists = true;
                _attrExpiration = Runtime.CurrentTimeMillis() + AttrExpirationPeriod;
            }
            if (IsDirectory())
            {
                SmbFile[] files;
                SmbFile ndest;
                string path = dest.GetUncPath0();
                if (path.Length > 1)
                {
                    try
                    {
                        dest.Mkdir();
                        dest.SetPathInformation(_attributes, _createTime, _lastModified);
                    }
                    catch (SmbException se)
                    {
                        if (se.GetNtStatus() != NtStatus.NtStatusAccessDenied && se.GetNtStatus() != NtStatus
                            .NtStatusObjectNameCollision)
                        {
                            throw;
                        }
                    }
                }
                files = ListFiles("*", AttrDirectory | AttrHidden | AttrSystem, null, null);
                try
                {
                    for (i = 0; i < files.Length; i++)
                    {
                        ndest = new SmbFile(dest, files[i].GetName(), files[i].Type, files[i]._attributes,
                            files[i]._createTime, files[i]._lastModified, files[i]._size);
                        files[i].CopyTo0(ndest, b, bsize, w, req, resp);
                    }
                }
                catch (UnknownHostException uhe)
                {
                    throw new SmbException(Url.ToString(), uhe);
                }
                catch (UriFormatException mue)
                {
                    throw new SmbException(Url.ToString(), mue);
                }
            }
            else
            {
                long off;
                try
                {
                    Open(ORdonly, 0, AttrNormal, 0);
                    try
                    {
                        dest.Open(OCreat | OWronly | OTrunc, SmbConstants.FileWriteData |
                             SmbConstants.FileWriteAttributes, _attributes, 0);
                    }
                    catch (SmbAuthException sae)
                    {
                        if ((dest._attributes & AttrReadonly) != 0)
                        {
                            dest.SetPathInformation(dest._attributes & ~AttrReadonly, 0L, 0L);
                            dest.Open(OCreat | OWronly | OTrunc, SmbConstants.FileWriteData |
                                 SmbConstants.FileWriteAttributes, _attributes, 0);
                        }
                        else
                        {
                            throw;
                        }
                    }
                    i = 0;
                    off = 0L;
                    for (; ; )
                    {
                        req.SetParam(Fid, off, bsize);
                        resp.SetParam(b[i], 0);
                        Send(req, resp);
                        lock (w)
                        {
                            if (w.E != null)
                            {
                                throw w.E;
                            }
                            while (!w.Ready)
                            {
                                try
                                {
                                    Runtime.Wait(w);
                                }
                                catch (Exception ie)
                                {
                                    throw new SmbException(dest.Url.ToString(), ie);
                                }
                            }
                            if (w.E != null)
                            {
                                throw w.E;
                            }
                            if (resp.DataLength <= 0)
                            {
                                break;
                            }
                            w.Write(b[i], resp.DataLength, dest, off);
                        }
                        i = i == 1 ? 0 : 1;
                        off += resp.DataLength;
                    }
                    dest.Send(new Trans2SetFileInformation(dest.Fid, _attributes, _createTime, _lastModified
                        ), new Trans2SetFileInformationResponse());
                    dest.Close(0L);
                }
                catch (SmbException se)
                {
                    if (IgnoreCopyToException == false)
                    {
                        throw new SmbException("Failed to copy file from [" + ToString() + "] to ["
                            + dest + "]", se);
                    }
                    if (Log.Level > 1)
                    {
                        Runtime.PrintStackTrace(se, Log);
                    }
                }
                finally
                {
                    Close();
                }
            }
        }

        /// <summary>
        /// This method will copy the file or directory represented by this
        /// <tt>SmbFile</tt> and it's sub-contents to the location specified by the
        /// <tt>dest</tt> parameter.
        /// </summary>
        /// <remarks>
        /// This method will copy the file or directory represented by this
        /// <tt>SmbFile</tt> and it's sub-contents to the location specified by the
        /// <tt>dest</tt> parameter. This file and the destination file do not
        /// need to be on the same host. This operation does not copy extended
        /// file attibutes such as ACLs but it does copy regular attributes as
        /// well as create and last write times. This method is almost twice as
        /// efficient as manually copying as it employs an additional write
        /// thread to read and write data concurrently.
        /// <p/>
        /// It is not possible (nor meaningful) to copy entire workgroups or
        /// servers.
        /// </remarks>
        /// <param name="dest">the destination file or directory</param>
        /// <exception cref="SmbException">SmbException</exception>
        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        public virtual void CopyTo(SmbFile dest)
        {
            SmbComReadAndX req;
            SmbComReadAndXResponse resp;
            WriterThread w;
            int bsize;
            byte[][] b;
            if (_share == null || dest._share == null)
            {
                throw new SmbException("Invalid operation for workgroups or servers");
            }
            req = new SmbComReadAndX();
            resp = new SmbComReadAndXResponse();
            Connect0();
            dest.Connect0();
            ResolveDfs(null);
            try
            {
                if (GetAddress().Equals(dest.GetAddress()) && _canon.RegionMatches(true, 0, dest._canon
                    , 0, Math.Min(_canon.Length, dest._canon.Length)))
                {
                    throw new SmbException("Source and destination paths overlap.");
                }
            }
            catch (UnknownHostException)
            {
            }
            w = new WriterThread(this);
            w.SetDaemon(true);
            w.Start();
            SmbTransport t1 = Tree.Session.transport;
            SmbTransport t2 = dest.Tree.Session.transport;
            if (t1.SndBufSize < t2.SndBufSize)
            {
                t2.SndBufSize = t1.SndBufSize;
            }
            else
            {
                t1.SndBufSize = t2.SndBufSize;
            }
            bsize = Math.Min(t1.RcvBufSize - 70, t1.SndBufSize - 70);
            b = new[] { new byte[bsize], new byte[bsize] };
            try
            {
                CopyTo0(dest, b, bsize, w, req, resp);
            }
            finally
            {
                w.Write(null, -1, null, 0);
            }
        }

        /// <summary>
        /// This method will delete the file or directory specified by this
        /// <code>SmbFile</code>.
        /// </summary>
        /// <remarks>
        /// This method will delete the file or directory specified by this
        /// <code>SmbFile</code>. If the target is a directory, the contents of
        /// the directory will be deleted as well. If a file within the directory or
        /// it's sub-directories is marked read-only, the read-only status will
        /// be removed and the file will be deleted.
        /// </remarks>
        /// <exception cref="SmbException">SmbException</exception>
        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        public virtual void Delete()
        {
            Exists();
            GetUncPath0();
            Delete(Unc);
        }

        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        internal virtual void Delete(string fileName)
        {
            if (GetUncPath0().Length == 1)
            {
                throw new SmbException("Invalid operation for workgroups, servers, or shares");
            }
            if (Runtime.CurrentTimeMillis() > _attrExpiration)
            {
                _attributes = AttrReadonly | AttrDirectory;
                _createTime = 0L;
                _lastModified = 0L;
                _isExists = false;
                IInfo info = QueryPath(GetUncPath0(), Trans2QueryPathInformationResponse.SMB_QUERY_FILE_BASIC_INFO
                    );
                _attributes = info.GetAttributes();
                _createTime = info.GetCreateTime();
                _lastModified = info.GetLastWriteTime();
                _attrExpiration = Runtime.CurrentTimeMillis() + AttrExpirationPeriod;
                _isExists = true;
            }
            if ((_attributes & AttrReadonly) != 0)
            {
                SetReadWrite();
            }
            if (Log.Level >= 3)
            {
                Log.WriteLine("delete: " + fileName);
            }
            if ((_attributes & AttrDirectory) != 0)
            {
                try
                {
                    SmbFile[] l = ListFiles("*", AttrDirectory | AttrHidden | AttrSystem, null, null
                        );
                    for (int i = 0; i < l.Length; i++)
                    {
                        l[i].Delete();
                    }
                }
                catch (SmbException se)
                {
                    if (se.GetNtStatus() != NtStatus.NtStatusNoSuchFile)
                    {
                        throw;
                    }
                }
                Send(new SmbComDeleteDirectory(fileName), Blank_resp());
            }
            else
            {
                Send(new SmbComDelete(fileName), Blank_resp());
            }
            _attrExpiration = _sizeExpiration = 0;
        }

        /// <summary>Returns the length of this <tt>SmbFile</tt> in bytes.</summary>
        /// <remarks>
        /// Returns the length of this <tt>SmbFile</tt> in bytes. If this object
        /// is a <tt>TYPE_SHARE</tt> the total capacity of the disk shared in
        /// bytes is returned. If this object is a directory or a type other than
        /// <tt>TYPE_SHARE</tt>, 0L is returned.
        /// </remarks>
        /// <returns>
        /// The length of the file in bytes or 0 if this
        /// <code>SmbFile</code> is not a file.
        /// </returns>
        /// <exception cref="SmbException">SmbException</exception>
        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        public virtual long Length()
        {
            if (_sizeExpiration > Runtime.CurrentTimeMillis())
            {
                return _size;
            }
            if (GetType() == TypeShare)
            {
                Trans2QueryFsInformationResponse response;
                int level = Trans2QueryFsInformationResponse.SMB_INFO_ALLOCATION;
                response = new Trans2QueryFsInformationResponse(level);
                Send(new Trans2QueryFsInformation(level), response);
                _size = response.Info.GetCapacity();
            }
            else
            {
                if (GetUncPath0().Length > 1 && Type != TypeNamedPipe)
                {
                    IInfo info = QueryPath(GetUncPath0(), Trans2QueryPathInformationResponse.SMB_QUERY_FILE_STANDARD_INFO
                        );
                    _size = info.GetSize();
                }
                else
                {
                    _size = 0L;
                }
            }
            _sizeExpiration = Runtime.CurrentTimeMillis() + AttrExpirationPeriod;
            return _size;
        }

        /// <summary>
        /// This method returns the free disk space in bytes of the drive this share
        /// represents or the drive on which the directory or file resides.
        /// </summary>
        /// <remarks>
        /// This method returns the free disk space in bytes of the drive this share
        /// represents or the drive on which the directory or file resides. Objects
        /// other than <tt>TYPE_SHARE</tt> or <tt>TYPE_FILESYSTEM</tt> will result
        /// in 0L being returned.
        /// </remarks>
        /// <returns>
        /// the free disk space in bytes of the drive on which this file or
        /// directory resides
        /// </returns>
        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        public virtual long GetDiskFreeSpace()
        {
            if (GetType() == TypeShare || Type == TypeFilesystem)
            {
                int level = Trans2QueryFsInformationResponse.SmbFsFullSizeInformation;
                try
                {
                    return QueryFsInformation(level);
                }
                catch (SmbException ex)
                {
                    switch (ex.GetNtStatus())
                    {
                        case NtStatus.NtStatusInvalidInfoClass:
                        case NtStatus.NtStatusUnsuccessful:
                            {
                                // NetApp Filer
                                // SMB_FS_FULL_SIZE_INFORMATION not supported by the server.
                                level = Trans2QueryFsInformationResponse.SMB_INFO_ALLOCATION;
                                return QueryFsInformation(level);
                            }
                    }
                    throw;
                }
            }
            return 0L;
        }

        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        private long QueryFsInformation(int level)
        {
            Trans2QueryFsInformationResponse response;
            response = new Trans2QueryFsInformationResponse(level);
            Send(new Trans2QueryFsInformation(level), response);
            if (Type == TypeShare)
            {
                _size = response.Info.GetCapacity();
                _sizeExpiration = Runtime.CurrentTimeMillis() + AttrExpirationPeriod;
            }
            return response.Info.GetFree();
        }

        /// <summary>
        /// Creates a directory with the path specified by this
        /// <code>SmbFile</code>.
        /// </summary>
        /// <remarks>
        /// Creates a directory with the path specified by this
        /// <code>SmbFile</code>. For this method to be successful, the target
        /// must not already exist. This method will fail when
        /// used with <code>smb://</code>, <code>smb://workgroup/</code>,
        /// <code>smb://server/</code>, or <code>smb://server/share/</code> URLs
        /// because workgroups, servers, and shares cannot be dynamically created
        /// (although in the future it may be possible to create shares).
        /// </remarks>
        /// <exception cref="SmbException">SmbException</exception>
        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        public virtual void Mkdir()
        {
            string path = GetUncPath0();
            if (path.Length == 1)
            {
                throw new SmbException("Invalid operation for workgroups, servers, or shares");
            }
            if (Log.Level >= 3)
            {
                Log.WriteLine("mkdir: " + path);
            }
            Send(new SmbComCreateDirectory(path), Blank_resp());
            _attrExpiration = _sizeExpiration = 0;
        }

        /// <summary>
        /// Creates a directory with the path specified by this <tt>SmbFile</tt>
        /// and any parent directories that do not exist.
        /// </summary>
        /// <remarks>
        /// Creates a directory with the path specified by this <tt>SmbFile</tt>
        /// and any parent directories that do not exist. This method will fail
        /// when used with <code>smb://</code>, <code>smb://workgroup/</code>,
        /// <code>smb://server/</code>, or <code>smb://server/share/</code> URLs
        /// because workgroups, servers, and shares cannot be dynamically created
        /// (although in the future it may be possible to create shares).
        /// </remarks>
        /// <exception cref="SmbException">SmbException</exception>
        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        public virtual void Mkdirs()
        {
            SmbFile parent;
            try
            {
                parent = new SmbFile(GetParent(), Auth);
            }
            catch (IOException)
            {
                return;
            }
            if (parent.Exists() == false)
            {
                parent.Mkdirs();
            }
            Mkdir();
        }

        /// <summary>Create a new file but fail if it already exists.</summary>
        /// <remarks>
        /// Create a new file but fail if it already exists. The check for
        /// existance of the file and it's creation are an atomic operation with
        /// respect to other filesystem activities.
        /// </remarks>
        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        public virtual void CreateNewFile()
        {
            if (GetUncPath0().Length == 1)
            {
                throw new SmbException("Invalid operation for workgroups, servers, or shares");
            }
            Close(Open0(ORdwr | OCreat | OExcl, 0, AttrNormal, 0), 0L);
        }

        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        internal virtual void SetPathInformation(int attrs, long ctime, long mtime)
        {
            int f;
            int dir;
            Exists();
            dir = _attributes & AttrDirectory;
            f = Open0(ORdonly, SmbConstants.FileWriteAttributes, dir, dir != 0 ? 0x0001
                 : 0x0040);
            Send(new Trans2SetFileInformation(f, attrs | dir, ctime, mtime), new Trans2SetFileInformationResponse
                ());
            Close(f, 0L);
            _attrExpiration = 0;
        }

        /// <summary>Set the create time of the file.</summary>
        /// <remarks>
        /// Set the create time of the file. The time is specified as milliseconds
        /// from Jan 1, 1970 which is the same as that which is returned by the
        /// <tt>createTime()</tt> method.
        /// <p/>
        /// This method does not apply to workgroups, servers, or shares.
        /// </remarks>
        /// <param name="time">the create time as milliseconds since Jan 1, 1970</param>
        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        public virtual void SetCreateTime(long time)
        {
            if (GetUncPath0().Length == 1)
            {
                throw new SmbException("Invalid operation for workgroups, servers, or shares");
            }
            SetPathInformation(0, time, 0L);
        }

        /// <summary>Set the last modified time of the file.</summary>
        /// <remarks>
        /// Set the last modified time of the file. The time is specified as milliseconds
        /// from Jan 1, 1970 which is the same as that which is returned by the
        /// <tt>lastModified()</tt>, <tt>getLastModified()</tt>, and <tt>getDate()</tt> methods.
        /// <p/>
        /// This method does not apply to workgroups, servers, or shares.
        /// </remarks>
        /// <param name="time">the last modified time as milliseconds since Jan 1, 1970</param>
        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        public virtual void SetLastModified(long time)
        {
            if (GetUncPath0().Length == 1)
            {
                throw new SmbException("Invalid operation for workgroups, servers, or shares");
            }
            SetPathInformation(0, 0L, time);
        }

        /// <summary>Return the attributes of this file.</summary>
        /// <remarks>
        /// Return the attributes of this file. Attributes are represented as a
        /// bitset that must be masked with <tt>ATTR_*</tt> constants to determine
        /// if they are set or unset. The value returned is suitable for use with
        /// the <tt>setAttributes()</tt> method.
        /// </remarks>
        /// <returns>the <tt>ATTR_*</tt> attributes associated with this file</returns>
        /// <exception cref="SmbException">SmbException</exception>
        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        public virtual int GetAttributes()
        {
            if (GetUncPath0().Length == 1)
            {
                return 0;
            }
            Exists();
            return _attributes & AttrGetMask;
        }

        /// <summary>Set the attributes of this file.</summary>
        /// <remarks>
        /// Set the attributes of this file. Attributes are composed into a
        /// bitset by bitwise ORing the <tt>ATTR_*</tt> constants. Setting the
        /// value returned by <tt>getAttributes</tt> will result in both files
        /// having the same attributes.
        /// </remarks>
        /// <exception cref="SmbException">SmbException</exception>
        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        public virtual void SetAttributes(int attrs)
        {
            if (GetUncPath0().Length == 1)
            {
                throw new SmbException("Invalid operation for workgroups, servers, or shares");
            }
            SetPathInformation(attrs & AttrSetMask, 0L, 0L);
        }

        /// <summary>Make this file read-only.</summary>
        /// <remarks>
        /// Make this file read-only. This is shorthand for <tt>setAttributes(
        /// getAttributes() | ATTR_READ_ONLY )</tt>.
        /// </remarks>
        /// <exception cref="SmbException">SmbException</exception>
        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        public virtual void SetReadOnly()
        {
            SetAttributes(GetAttributes() | AttrReadonly);
        }

        /// <summary>Turn off the read-only attribute of this file.</summary>
        /// <remarks>
        /// Turn off the read-only attribute of this file. This is shorthand for
        /// <tt>setAttributes( getAttributes() & ~ATTR_READONLY )</tt>.
        /// </remarks>
        /// <exception cref="SmbException">SmbException</exception>
        /// <exception cref="SmbException"></exception>
        public virtual void SetReadWrite()
        {
            SetAttributes(GetAttributes() & ~AttrReadonly);
        }

        /// <summary>
        /// Returns a
        /// <see cref="System.Uri">System.Uri</see>
        /// for this <code>SmbFile</code>. The
        /// <code>URL</code> may be used as any other <code>URL</code> might to
        /// access an SMB resource. Currently only retrieving data and information
        /// is supported (i.e. no <tt>doOutput</tt>).
        /// </summary>
        /// <returns>
        /// A new <code>
        /// <see cref="System.Uri">System.Uri</see>
        /// </code> for this <code>SmbFile</code>
        /// </returns>
        /// <exception cref="System.UriFormatException">System.UriFormatException</exception>
        [Obsolete(@"Use getURL() instead")]
        public virtual Uri ToUrl()
        {
            return Url;
        }

        /// <summary>
        /// Computes a hashCode for this file based on the URL string and IP
        /// address if the server.
        /// </summary>
        /// <remarks>
        /// Computes a hashCode for this file based on the URL string and IP
        /// address if the server. The hashing function uses the hashcode of the
        /// server address, the canonical representation of the URL, and does not
        /// compare authentication information. In essance, two
        /// <code>SmbFile</code> objects that refer to
        /// the same file should generate the same hashcode provided it is possible
        /// to make such a determination.
        /// </remarks>
        /// <returns>A hashcode for this abstract file</returns>
        /// <exception cref="SmbException">SmbException</exception>
        public override int GetHashCode()
        {
            int hash;
            try
            {
                hash = GetAddress().GetHashCode();
            }
            catch (UnknownHostException)
            {
                hash = GetServer().ToUpper().GetHashCode();
            }
            GetUncPath0();
            return hash + _canon.ToUpper().GetHashCode();
        }

        protected internal virtual bool PathNamesPossiblyEqual(string path1, string path2
            )
        {
            int p1;
            int p2;
            int l1;
            int l2;
            // if unsure return this method returns true
            p1 = path1.LastIndexOf('/');
            p2 = path2.LastIndexOf('/');
            l1 = path1.Length - p1;
            l2 = path2.Length - p2;
            // anything with dots voids comparison
            if (l1 > 1 && path1[p1 + 1] == '.')
            {
                return true;
            }
            if (l2 > 1 && path2[p2 + 1] == '.')
            {
                return true;
            }
            return l1 == l2 && path1.RegionMatches(true, p1, path2, p2, l1);
        }

        /// <summary>Tests to see if two <code>SmbFile</code> objects are equal.</summary>
        /// <remarks>
        /// Tests to see if two <code>SmbFile</code> objects are equal. Two
        /// SmbFile objects are equal when they reference the same SMB
        /// resource. More specifically, two <code>SmbFile</code> objects are
        /// equals if their server IP addresses are equal and the canonicalized
        /// representation of their URLs, minus authentication parameters, are
        /// case insensitivly and lexographically equal.
        /// <p/>
        /// For example, assuming the server <code>angus</code> resolves to the
        /// <code>192.168.1.15</code> IP address, the below URLs would result in
        /// <code>SmbFile</code>s that are equal.
        /// <p><blockquote><pre>
        /// smb://192.168.1.15/share/DIR/foo.txt
        /// smb://angus/share/data/../dir/foo.txt
        /// </pre></blockquote>
        /// </remarks>
        /// <param name="obj">Another <code>SmbFile</code> object to compare for equality</param>
        /// <returns>
        /// <code>true</code> if the two objects refer to the same SMB resource
        /// and <code>false</code> otherwise
        /// </returns>
        /// <exception cref="SmbException">SmbException</exception>
        public override bool Equals(object obj)
        {
            if (obj is SmbFile)
            {
                SmbFile f = (SmbFile)obj;
                bool ret;
                if (this == f)
                {
                    return true;
                }
                if (PathNamesPossiblyEqual(Url.AbsolutePath, f.Url.AbsolutePath))
                {
                    GetUncPath0();
                    f.GetUncPath0();
                    if (Runtime.EqualsIgnoreCase(_canon, f._canon))
                    {
                        try
                        {
                            ret = GetAddress().Equals(f.GetAddress());
                        }
                        catch (UnknownHostException)
                        {
                            ret = Runtime.EqualsIgnoreCase(GetServer(), f.GetServer());
                        }
                        return ret;
                    }
                }
            }
            return false;
        }

        /// <summary>Returns the string representation of this SmbFile object.</summary>
        /// <remarks>
        /// Returns the string representation of this SmbFile object. This will
        /// be the same as the URL used to construct this <code>SmbFile</code>.
        /// This method will return the same value
        /// as <code>getPath</code>.
        /// </remarks>
        /// <returns>The original URL representation of this SMB resource</returns>
        /// <exception cref="SmbException">SmbException</exception>
        public override string ToString()
        {
            return Url.ToString();
        }

        /// <summary>This URLConnection method just returns the result of <tt>length()</tt>.</summary>
        /// <remarks>This URLConnection method just returns the result of <tt>length()</tt>.</remarks>
        /// <returns>the length of this file or 0 if it refers to a directory</returns>
        public int GetContentLength()
        {
            try
            {
                return (int)(Length() & unchecked(0xFFFFFFFFL));
            }
            catch (SmbException)
            {
            }
            return 0;
        }

        /// <summary>This URLConnection method just returns the result of <tt>lastModified</tt>.
        /// 	</summary>
        /// <remarks>This URLConnection method just returns the result of <tt>lastModified</tt>.
        /// 	</remarks>
        /// <returns>the last modified data as milliseconds since Jan 1, 1970</returns>
        public long GetDate()
        {
            try
            {
                return LastModified();
            }
            catch (SmbException)
            {
            }
            return 0L;
        }

        /// <summary>This URLConnection method just returns the result of <tt>lastModified</tt>.
        /// 	</summary>
        /// <remarks>This URLConnection method just returns the result of <tt>lastModified</tt>.
        /// 	</remarks>
        /// <returns>the last modified data as milliseconds since Jan 1, 1970</returns>
        public long GetLastModified()
        {
            try
            {
                return LastModified();
            }
            catch (SmbException)
            {
            }
            return 0L;
        }

        /// <summary>This URLConnection method just returns a new <tt>SmbFileInputStream</tt> created with this file.
        /// 	</summary>
        /// <remarks>This URLConnection method just returns a new <tt>SmbFileInputStream</tt> created with this file.
        /// 	</remarks>
        /// <exception cref="System.IO.IOException">thrown by <tt>SmbFileInputStream</tt> constructor
        /// 	</exception>
        public InputStream GetInputStream()
        {
            return new SmbFileInputStream(this);
        }

        /// <summary>This URLConnection method just returns a new <tt>SmbFileOutputStream</tt> created with this file.
        /// 	</summary>
        /// <remarks>This URLConnection method just returns a new <tt>SmbFileOutputStream</tt> created with this file.
        /// 	</remarks>
        /// <exception cref="System.IO.IOException">thrown by <tt>SmbFileOutputStream</tt> constructor
        /// 	</exception>
        public OutputStream GetOutputStream()
        {
            return new SmbFileOutputStream(this);
        }

        /// <exception cref="System.IO.IOException"></exception>
        private void ProcessAces(Ace[] aces, bool resolveSids)
        {
            string server = GetServerWithDfs();
            int ai;
            if (resolveSids)
            {
                Sid[] sids = new Sid[aces.Length];
                string[] names = null;
                for (ai = 0; ai < aces.Length; ai++)
                {
                    sids[ai] = aces[ai].Sid;
                }
                for (int off = 0; off < sids.Length; off += 64)
                {
                    int len = sids.Length - off;
                    if (len > 64)
                    {
                        len = 64;
                    }
                    Sid.ResolveSids(server, Auth, sids, off, len);
                }
            }
            else
            {
                for (ai = 0; ai < aces.Length; ai++)
                {
                    aces[ai].Sid.OriginServer = server;
                    aces[ai].Sid.OriginAuth = Auth;
                }
            }
        }

        /// <summary>
        /// Return an array of Access Control Entry (ACE) objects representing
        /// the security descriptor associated with this file or directory.
        /// </summary>
        /// <remarks>
        /// Return an array of Access Control Entry (ACE) objects representing
        /// the security descriptor associated with this file or directory.
        /// If no DACL is present, null is returned. If the DACL is empty, an array with 0 elements is returned.
        /// </remarks>
        /// <param name="resolveSids">
        /// Attempt to resolve the SIDs within each ACE form
        /// their numeric representation to their corresponding account names.
        /// </param>
        /// <exception cref="System.IO.IOException"></exception>
        public virtual Ace[] GetSecurity(bool resolveSids)
        {
            int f;
            Ace[] aces;
            f = Open0(ORdonly, SmbConstants.ReadControl, 0, IsDirectory() ? 1 : 0);
            NtTransQuerySecurityDesc request = new NtTransQuerySecurityDesc(f, 0x04);
            NtTransQuerySecurityDescResponse response = new NtTransQuerySecurityDescResponse(
                );
            try
            {
                Send(request, response);
            }
            finally
            {
                Close(f, 0L);
            }
            aces = response.SecurityDescriptor.Aces;
            if (aces != null)
            {
                ProcessAces(aces, resolveSids);
            }
            return aces;
        }

        /// <summary>
        /// Return an array of Access Control Entry (ACE) objects representing
        /// the share permissions on the share exporting this file or directory.
        /// </summary>
        /// <remarks>
        /// Return an array of Access Control Entry (ACE) objects representing
        /// the share permissions on the share exporting this file or directory.
        /// If no DACL is present, null is returned. If the DACL is empty, an array with 0 elements is returned.
        /// <p>
        /// Note that this is different from calling <tt>getSecurity</tt> on a
        /// share. There are actually two different ACLs for shares - the ACL on
        /// the share and the ACL on the folder being shared.
        /// Go to <i>Computer Management</i>
        /// &gt; <i>System Tools</i> &gt; <i>Shared Folders</i> &gt <i>Shares</i> and
        /// look at the <i>Properties</i> for a share. You will see two tabs - one
        /// for "Share Permissions" and another for "Security". These correspond to
        /// the ACLs returned by <tt>getShareSecurity</tt> and <tt>getSecurity</tt>
        /// respectively.
        /// </remarks>
        /// <param name="resolveSids">
        /// Attempt to resolve the SIDs within each ACE form
        /// their numeric representation to their corresponding account names.
        /// </param>
        /// <exception cref="System.IO.IOException"></exception>
        public virtual Ace[] GetShareSecurity(bool resolveSids)
        {
            string p = Url.AbsolutePath;
            MsrpcShareGetInfo rpc;
            DcerpcHandle handle;
            Ace[] aces;
            ResolveDfs(null);
            string server = GetServerWithDfs();
            rpc = new MsrpcShareGetInfo(server, Tree.Share);
            handle = DcerpcHandle.GetHandle("ncacn_np:" + server + "[\\PIPE\\srvsvc]", Auth);
            try
            {
                handle.Sendrecv(rpc);
                if (rpc.Retval != 0)
                {
                    throw new SmbException(rpc.Retval, true);
                }
                aces = rpc.GetSecurity();
                if (aces != null)
                {
                    ProcessAces(aces, resolveSids);
                }
            }
            finally
            {
                try
                {
                    handle.Close();
                }
                catch (IOException ioe)
                {
                    if (Log.Level >= 1)
                    {
                        Runtime.PrintStackTrace(ioe, Log);
                    }
                }
            }
            return aces;
        }

        /// <summary>
        /// Return an array of Access Control Entry (ACE) objects representing
        /// the security descriptor associated with this file or directory.
        /// </summary>
        /// <remarks>
        /// Return an array of Access Control Entry (ACE) objects representing
        /// the security descriptor associated with this file or directory.
        /// <p>
        /// Initially, the SIDs within each ACE will not be resolved however when
        /// <tt>getType()</tt>, <tt>getDomainName()</tt>, <tt>getAccountName()</tt>,
        /// or <tt>toString()</tt> is called, the names will attempt to be
        /// resolved. If the names cannot be resolved (e.g. due to temporary
        /// network failure), the said methods will return default values (usually
        /// <tt>S-X-Y-Z</tt> strings of fragments of).
        /// <p>
        /// Alternatively <tt>getSecurity(true)</tt> may be used to resolve all
        /// SIDs together and detect network failures.
        /// </remarks>
        /// <exception cref="System.IO.IOException"></exception>
        public virtual Ace[] GetSecurity()
        {
            return GetSecurity(false);
        }
    }
}