/usr/share/ada/adainclude/gnatcoll/gnatcoll-vfs.adb is in libgnatcoll1.6-dev 1.6gpl2014-6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | ------------------------------------------------------------------------------
-- G N A T C O L L --
-- --
-- Copyright (C) 2003-2014, AdaCore --
-- --
-- This library is free software; you can redistribute it and/or modify it --
-- under terms of the GNU General Public License as published by the Free --
-- Software Foundation; either version 3, 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 MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. --
-- --
-- --
-- --
-- --
-- --
-- You should have received a copy of the GNU General Public License and --
-- a copy of the GCC Runtime Library Exception along with this program; --
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
-- <http://www.gnu.org/licenses/>. --
-- --
------------------------------------------------------------------------------
with Ada.Calendar; use Ada.Calendar;
with Ada.Characters.Handling; use Ada.Characters.Handling;
with Ada.Exceptions; use Ada.Exceptions;
with Ada.Tags; use Ada.Tags;
with Ada.Strings.Hash;
with Ada.Strings.Hash_Case_Insensitive;
with Ada.Text_IO;
with Ada.Unchecked_Conversion;
with System;
with GNAT.Heap_Sort; use GNAT.Heap_Sort;
with GNATCOLL.Utils; use GNATCOLL.Utils;
with GNATCOLL.IO; use GNATCOLL.IO;
with GNATCOLL.IO.Remote; use GNATCOLL.IO.Remote;
with GNATCOLL.Path; use GNATCOLL.Path;
with GNATCOLL.Remote; use GNATCOLL.Remote;
with GNATCOLL.Remote.Db; use GNATCOLL.Remote.Db;
with GNATCOLL.VFS_Types; use GNATCOLL.VFS_Types;
package body GNATCOLL.VFS is
Empty_String : aliased Filesystem_String := "";
Handle_Symbolic_Links : Boolean := GNAT.OS_Lib.Directory_Separator /= '\';
-- If this variable is False, we assume there is never any symbolic link,
-- and thus we do not spend time resolving them.
function "+" (S : Filesystem_String) return FS_String;
function "+" (S : FS_String) return Filesystem_String;
function "+" (S : FS_String) return String;
pragma Inline ("+");
-- FS_String and Filesystem_String are identical in their intent.
-- We just have a visibility issue as it really should be defined in
-- VFS's spec, so can't be used by underneath packages (GNATCOLL.IO and
-- GNATCOLL.Path)
function "+" is new Ada.Unchecked_Conversion
(FS_String_Access, Filesystem_String_Access);
procedure Ensure_Normalized
(File : Virtual_File'Class;
Resolve_Symlinks : Boolean);
-- Make sure that File.Value.Normalized is filled
---------
-- "+" --
---------
function "+" (S : Filesystem_String) return FS_String is
begin
return FS_String (S);
end "+";
function "+" (S : FS_String) return Filesystem_String is
begin
return Filesystem_String (S);
end "+";
function "+" (S : FS_String) return String is
begin
return String (S);
end "+";
function "+" (S : Filesystem_String) return String is
begin
return String (S);
end "+";
function "+" (S : String) return Filesystem_String is
begin
return Filesystem_String (S);
end "+";
---------
-- "=" --
---------
function Equal (S1, S2 : Filesystem_String) return Boolean is
begin
return Equal (+S1, +S2, Is_Case_Sensitive (Local_FS));
end Equal;
---------
-- "=" --
---------
function "=" (File1, File2 : Virtual_File) return Boolean is
begin
-- Test for the same pointer to actual value (or both null)
if File1.Value = File2.Value then
return True;
-- Test if one of the values is null
elsif File1.Value = null
or else File2.Value = null
or else File1.Value.all'Tag /= File2.Value.all'Tag
then
return False;
-- Finally, we test the normalized paths
else
Ensure_Normalized (File1, Handle_Symbolic_Links);
Ensure_Normalized (File2, Handle_Symbolic_Links);
-- We also take care of potential trailing dir separator by enforcing
-- them
return Equal
(File1.Value.Get_FS,
Ensure_Directory (File1.Value.Get_FS, File1.Value.Normalized.all),
Ensure_Directory (File1.Value.Get_FS, File2.Value.Normalized.all));
end if;
end "=";
---------
-- "<" --
---------
function "<" (File1, File2 : Virtual_File) return Boolean is
C1, C2 : Character;
Ind1, Ind2 : Integer;
Case_Sensitive : Boolean;
begin
if File1 = File2 then
return False;
elsif File1.Value = null then
return True;
elsif File2.Value = null then
return False;
elsif Is_Local (File1.Value.all) /= Is_Local (File2.Value.all) then
return Is_Local (File1.Value.all);
elsif not Is_Local (File1.Value.all)
and then Get_Host (File1) /= Get_Host (File2)
then
return Get_Host (File1) < Get_Host (File2);
else
Case_Sensitive := Is_Case_Sensitive (File1.Value.Get_FS)
and then Is_Case_Sensitive (File2.Value.Get_FS);
Ensure_Normalized (File1, True);
Ensure_Normalized (File2, True);
if Case_Sensitive then
return File1.Value.Normalized.all < File2.Value.Normalized.all;
else
Ind1 := File1.Value.Normalized'First;
Ind2 := File2.Value.Normalized'First;
for C in 1 .. File1.Value.Normalized'Length loop
if Ind2 > File2.Value.Normalized'Last then
return False;
end if;
C1 := To_Lower (File1.Value.Normalized (Ind1));
C2 := To_Lower (File2.Value.Normalized (Ind2));
if C1 < C2 then
return True;
elsif C1 > C2 then
return False;
end if;
Ind1 := Ind1 + 1;
Ind2 := Ind2 + 1;
end loop;
return True;
end if;
end if;
end "<";
------------
-- Create --
------------
function Create
(Full_Filename : Filesystem_String;
Host : String := Local_Host;
Normalize : Boolean := False) return Virtual_File
is
function Internal_Get_Path (FS : FS_Type) return FS_String;
-- Get Full_Filename according to Normalize setting
-----------------------
-- Internal_Get_Path --
-----------------------
function Internal_Get_Path (FS : FS_Type) return FS_String is
begin
if not Normalize then
return +Full_Filename;
end if;
return GNATCOLL.Path.Normalize (FS, +Full_Filename);
end Internal_Get_Path;
begin
if Full_Filename = "" then
return No_File;
end if;
if Host = Local_Host then
return (Ada.Finalization.Controlled with
Value => GNATCOLL.IO.Native.Create
(Internal_Get_Path (GNATCOLL.Path.Local_FS)));
else
return (Ada.Finalization.Controlled with
Value => GNATCOLL.IO.Remote.Create
(Host, +Full_Filename, Normalize));
end if;
end Create;
----------------------
-- Create_From_UTF8 --
----------------------
function Create_From_UTF8
(Full_Filename : String;
Host : String := Local_Host;
Normalize : Boolean := False) return Virtual_File is
begin
if Host = Local_Host then
return Create
(+GNATCOLL.IO.Native.Codec.From_UTF8 (Full_Filename),
Normalize => Normalize);
else
return Create
(+GNATCOLL.IO.Remote.Codec.From_UTF8 (Full_Filename), Host,
Normalize => Normalize);
end if;
end Create_From_UTF8;
----------------------
-- Create_From_Base --
----------------------
function Create_From_Base
(Base_Name : Filesystem_String;
Base_Dir : Filesystem_String := "";
Host : String := Local_Host) return Virtual_File
is
FS : FS_Type;
begin
if Host = Local_Host then
FS := Local_FS;
else
FS := Shell_FS (Get_Server (Host).all);
end if;
declare
The_Name : constant FS_String := Path.From_Unix (FS, +Base_Name);
The_Dir : constant FS_String := Path.From_Unix (FS, +Base_Dir);
begin
if Is_Absolute_Path (FS, The_Name) then
return Create (+The_Name, Host);
elsif The_Dir /= "" then
return Create
(+(Path.Ensure_Directory (FS, The_Dir) & The_Name), Host);
else
return Create_From_Dir
(Get_Current_Dir (Host), +The_Name);
end if;
end;
end Create_From_Base;
---------------------
-- Create_From_Dir --
---------------------
function Create_From_Dir
(Dir : Virtual_File;
Base_Name : Filesystem_String;
Normalize : Boolean := False) return Virtual_File
is
begin
if Dir.Value = null then
raise VFS_Invalid_File_Error;
end if;
Dir.Ensure_Directory;
return
(Ada.Finalization.Controlled with
Dispatching_Create
(Dir.Value,
+Dir.Full_Name (Normalize) &
GNATCOLL.Path.Normalize
(Dir.Value.Get_FS,
From_Unix (Dir.Value.Get_FS, +Base_Name))));
end Create_From_Dir;
--------------------
-- Locate_On_Path --
--------------------
function Locate_On_Path
(Base_Name : Filesystem_String;
Host : String := Local_Host) return Virtual_File
is
Name : GNAT.OS_Lib.String_Access;
Ret : Virtual_File;
use type GNAT.OS_Lib.String_Access;
begin
if Host = Local_Host then
if Is_Absolute_Path (Local_FS, +Base_Name) then
return Create (Base_Name);
end if;
Name := GNAT.OS_Lib.Locate_Exec_On_Path (+Base_Name);
if Name = null then
return No_File;
else
Ret := Create (+Name.all);
GNAT.OS_Lib.Free (Name);
return Ret;
end if;
else
declare
Int : constant GNATCOLL.IO.File_Access :=
GNATCOLL.IO.Remote.Locate_On_Path (Host, +Base_Name);
begin
if Int = null then
return No_File;
else
return (Ada.Finalization.Controlled with Int);
end if;
end;
end if;
end Locate_On_Path;
-----------------------
-- Get_Tmp_Directory --
-----------------------
function Get_Tmp_Directory (Host : String := Local_Host) return Virtual_File
is
begin
if Host = Local_Host then
return (Ada.Finalization.Controlled with
GNATCOLL.IO.Native.Get_Tmp_Directory);
else
return (Ada.Finalization.Controlled with
GNATCOLL.IO.Remote.Get_Tmp_Directory (Host));
end if;
end Get_Tmp_Directory;
------------------------
-- Get_Home_Directory --
------------------------
function Get_Home_Directory
(Host : String := Local_Host) return Virtual_File
is
begin
if Host = Local_Host then
return (Ada.Finalization.Controlled with
GNATCOLL.IO.Native.Home_Dir);
else
return (Ada.Finalization.Controlled with
GNATCOLL.IO.Remote.Home_Dir (Host));
end if;
end Get_Home_Directory;
------------------------
-- Get_Logical_Drives --
------------------------
function Get_Logical_Drives
(Host : String := Local_Host) return File_Array_Access
is
function Get_IO_List return GNATCOLL.IO.File_Array;
-- Get the IO list depending on Host
-----------------
-- Get_IO_List --
-----------------
function Get_IO_List return GNATCOLL.IO.File_Array is
begin
if Host = Local_Host then
return GNATCOLL.IO.Native.Get_Logical_Drives;
else
return GNATCOLL.IO.Remote.Get_Logical_Drives (Host);
end if;
end Get_IO_List;
List : constant GNATCOLL.IO.File_Array := Get_IO_List;
Ret : constant File_Array_Access := new File_Array (1 .. List'Length);
begin
for J in List'Range loop
Ret (J - List'First + 1) :=
(Ada.Finalization.Controlled with
List (J));
Ret (J - List'First + 1).Value.Kind := Directory;
end loop;
return Ret;
end Get_Logical_Drives;
---------------------
-- Get_Current_Dir --
---------------------
function Get_Current_Dir
(Host : String := Local_Host) return Virtual_File
is
Ret : Virtual_File;
begin
if Host = Local_Host then
Ret := (Ada.Finalization.Controlled with
GNATCOLL.IO.Native.Current_Dir);
else
Ret := (Ada.Finalization.Controlled with
GNATCOLL.IO.Remote.Current_Dir (Host));
end if;
Ret.Value.Kind := Directory;
return Ret;
end Get_Current_Dir;
---------------
-- Base_Name --
---------------
function Base_Name
(File : Virtual_File;
Suffix : Filesystem_String := "";
Normalize : Boolean := False)
return Filesystem_String is
begin
if File.Value = null then
return "";
end if;
if Normalize then
Ensure_Normalized (File, Handle_Symbolic_Links);
return +Base_Name
(File.Value.Get_FS, File.Value.Normalized.all, +Suffix);
else
return +Base_Name (File.Value.Get_FS, File.Value.Full.all, +Suffix);
end if;
end Base_Name;
-------------------
-- Base_Dir_Name --
-------------------
function Base_Dir_Name (File : Virtual_File) return Filesystem_String is
begin
if File.Value = null then
return "";
end if;
return +Base_Dir_Name (File.Value.Get_FS, File.Value.Full.all);
end Base_Dir_Name;
---------------
-- Full_Name --
---------------
function Full_Name
(File : Virtual_File; Normalize : Boolean := False)
return Filesystem_String is
begin
return File.Full_Name (Normalize).all;
end Full_Name;
---------------
-- Full_Name --
---------------
function Full_Name
(File : Virtual_File;
Normalize : Boolean := False;
Resolve_Links : Boolean := False)
return Cst_Filesystem_String_Access is
begin
if File.Value = null then
return Empty_String'Access;
elsif File.Value.Full /= null and then Normalize then
Ensure_Normalized (File, Resolve_Links);
return Cst_Filesystem_String_Access
(+File.Value.Normalized.all'Access);
elsif File.Value.Full = null
and then Get_Host (File) /= Local_Host
then
GNATCOLL.IO.Remote.Ensure_Initialized
(GNATCOLL.IO.Remote.Remote_File_Access (File.Value));
return Cst_Filesystem_String_Access (+File.Value.Full.all'Access);
else
return Cst_Filesystem_String_Access (+File.Value.Full.all'Access);
end if;
end Full_Name;
--------------------
-- Full_Name_Hash --
--------------------
function Full_Name_Hash
(Key : Virtual_File) return Ada.Containers.Hash_Type is
begin
if Key.Value = null then
return 0;
end if;
Ensure_Normalized (Key, True);
if Is_Case_Sensitive (Key.Value.Get_FS) then
return Ada.Strings.Hash (+Key.Value.Normalized.all);
else
return Ada.Strings.Hash_Case_Insensitive (+Key.Value.Normalized.all);
end if;
end Full_Name_Hash;
--------------
-- Dir_Name --
--------------
function Dir_Name (File : Virtual_File) return Filesystem_String is
begin
if File.Value = null then
return "";
end if;
return +Dir_Name (File.Value.Get_FS, File.Value.Full.all);
end Dir_Name;
---------
-- Dir --
---------
function Dir (File : Virtual_File) return Virtual_File is
begin
if File.Value = null then
return VFS.No_File;
end if;
if Is_Dir_Name (File.Value.Get_FS, File.Value.Full.all) then
return File;
else
return
(Ada.Finalization.Controlled with
Value => Dispatching_Create
(File.Value,
Dir_Name
(File.Value.Get_FS, File.Value.Full.all)));
end if;
end Dir;
-----------------------
-- Display_Full_Name --
-----------------------
function Display_Full_Name
(File : Virtual_File;
Normalize : Boolean := False) return String is
begin
if File.Value = null then
return "";
else
return File.Value.To_UTF8
(FS_String (File.Full_Name (Normalize).all));
end if;
end Display_Full_Name;
-----------------------
-- Display_Base_Name --
-----------------------
function Display_Base_Name
(File : Virtual_File;
Suffix : Filesystem_String := "") return String is
begin
if File.Value = null then
return "";
else
return File.Value.To_UTF8 (+File.Base_Name (Suffix));
end if;
end Display_Base_Name;
---------------------
-- Locale_Dir_Name --
---------------------
function Display_Dir_Name (File : Virtual_File) return String is
begin
if File.Value = null then
return "";
else
return File.Value.To_UTF8 (+File.Dir_Name);
end if;
end Display_Dir_Name;
---------------------------
-- Display_Base_Dir_Name --
---------------------------
function Display_Base_Dir_Name (File : Virtual_File) return String is
begin
if File.Value = null then
return "";
else
return File.Value.To_UTF8 (+File.Base_Dir_Name);
end if;
end Display_Base_Dir_Name;
--------------------------
-- Unix_Style_Full_Name --
--------------------------
function Unix_Style_Full_Name
(File : Virtual_File;
Cygwin_Style : Boolean := False;
Normalize : Boolean := False;
Casing : Boolean := False) return Filesystem_String
is
FS : FS_Type;
function Auto_Case (Str : FS_String) return Filesystem_String;
function Auto_Case (Str : FS_String) return Filesystem_String is
begin
if not Casing or else Is_Case_Sensitive (FS) then
return +Str;
else
return +To_Lower (+Str);
end if;
end Auto_Case;
begin
if File.Value = null then
return "";
else
FS := File.Value.Get_FS;
if Normalize then
return Auto_Case (To_Unix
(FS,
+Full_Name
(File, Normalize => Normalize, Resolve_Links => True).all,
Cygwin_Style));
else
return Auto_Case (To_Unix (FS, File.Value.Full.all, Cygwin_Style));
end if;
end if;
end Unix_Style_Full_Name;
-------------------
-- Relative_Path --
-------------------
function Relative_Path
(File : Virtual_File;
From : Virtual_File) return Filesystem_String
is
begin
-- Obviously, we need from to be a directory...
if not Is_Directory (From) then
return File.Full_Name;
end if;
if From.Value = null
or else File.Value = null
or else File.Value'Tag /= From.Value'Tag
then
return File.Full_Name;
end if;
return Filesystem_String (Relative_Path
(File.Value.Get_FS,
Ref => FS_String (From.Full_Name.all),
Path => FS_String (File.Full_Name.all)));
end Relative_Path;
----------------
-- Has_Suffix --
----------------
function Has_Suffix
(File : Virtual_File; Suffix : Filesystem_String) return Boolean is
begin
return File.Value /= null
and then File.Full_Name.all'Length >= Suffix'Length
and then Equal
(File.Value.Get_FS,
File.Value.Full
(File.Value.Full'Last - Suffix'Length + 1
.. File.Value.Full'Last),
+Suffix);
end Has_Suffix;
---------------
-- To_Remote --
---------------
function To_Remote
(File : Virtual_File; To_Host : String) return Virtual_File
is
begin
if File.Value = null then
return No_File;
end if;
if File.Get_Host = To_Host then
return File;
end if;
if not Is_Configured (To_Host) then
raise VFS_Remote_Config_Error;
end if;
declare
Server : constant Server_Access := Get_Server (To_Host);
Local : Virtual_File;
Remote : Virtual_File := No_File;
begin
for J in 1 .. Nb_Mount_Points (Server.Nickname) loop
Local := Create (+Get_Mount_Point_Local_Root (Server.Nickname, J));
if Local.Is_Parent (File) then
Remote := Create
(+Get_Mount_Point_Host_Root (Server.Nickname, J),
To_Host);
exit;
end if;
end loop;
if Remote = No_File then
-- Simple conversion
return Convert (File, To_Host);
else
return Convert (File, Local, Remote);
end if;
end;
end To_Remote;
--------------
-- To_Local --
--------------
function To_Local (File : Virtual_File) return Virtual_File is
begin
if File.Value = null then
return No_File;
end if;
if File.Is_Local then
return File;
end if;
if not Is_Configured (File.Get_Host) then
raise VFS_Remote_Config_Error;
end if;
declare
Server : constant Server_Access := Get_Server (File.Get_Host);
Local : Virtual_File := No_File;
Remote : Virtual_File;
begin
for J in 1 .. Nb_Mount_Points (Server.Nickname) loop
Remote := Create
(+Get_Mount_Point_Host_Root (Server.Nickname, J),
File.Get_Host);
if Remote.Is_Parent (File) then
Local := Create
(+Get_Mount_Point_Local_Root (Server.Nickname, J));
exit;
end if;
end loop;
if Local = No_File then
-- Simple conversion
return Convert (File, Local_Host);
else
return Convert (File, Remote, Local);
end if;
end;
end To_Local;
------------
-- To_Arg --
------------
function To_Arg
(File : Virtual_File;
Host : String := Local_Host) return GNAT.Strings.String_Access
is
Host_File : Virtual_File;
begin
if Host /= File.Get_Host then
if File.Get_Host /= Local_Host then
Host_File := File.To_Local;
else
Host_File := File;
end if;
if Host /= Local_Host then
Host_File := Host_File.To_Remote (Host);
end if;
return new String'(String (Host_File.Full_Name.all));
end if;
return new String'(String (File.Full_Name.all));
end To_Arg;
-------------
-- Convert --
-------------
function Convert
(File : Virtual_File; To_Host : String) return Virtual_File
is
begin
if File.Value = null then
return No_File;
end if;
-- Create always handles both paths in Unix form, or paths in Host's
-- form. So we first translate the path to unix (possible whatever the
-- current path format is), then it's up to Create to correctly format
-- the path.
return Create
(+To_Unix
(File.Value.Get_FS, FS_String (File.Full_Name.all)), To_Host);
end Convert;
-------------
-- Convert --
-------------
function Convert
(File : Virtual_File;
From_Dir : Virtual_File;
To_Dir : Virtual_File) return Virtual_File
is
begin
if File.Value = null
or else From_Dir.Value = null
or else To_Dir.Value = null
then
return No_File;
end if;
if From_Dir.Value.Get_FS /= To_Dir.Value.Get_FS then
return Create_From_Dir
(To_Dir,
+To_Unix (File.Value.Get_FS, +File.Relative_Path (From_Dir)));
else
return Create_From_Dir
(To_Dir,
File.Relative_Path (From_Dir));
end if;
end Convert;
--------------------
-- Unchecked_Free --
--------------------
procedure Unchecked_Free (Arr : in out File_Array_Access) is
procedure Internal is new Ada.Unchecked_Deallocation
(File_Array, File_Array_Access);
begin
Internal (Arr);
end Unchecked_Free;
--------------
-- Is_Local --
--------------
function Is_Local (File : Virtual_File) return Boolean is
begin
return File.Value = null
or else File.Value.all in GNATCOLL.IO.Native.Native_File_Record'Class;
end Is_Local;
----------
-- Host --
----------
function Get_Host (File : Virtual_File) return String is
begin
if Is_Local (File) then
return Local_Host;
else
return GNATCOLL.IO.Remote.Get_Host
(Remote_File_Record (File.Value.all)'Access);
end if;
end Get_Host;
---------------------
-- Is_Regular_File --
---------------------
function Is_Regular_File (File : Virtual_File) return Boolean is
Ret : Boolean;
begin
if File.Value = null then
return False;
elsif File.Value.all not in GNATCOLL.IO.Native.Native_File_Record'Class
and then File.Value.Kind /= Unknown
-- Only use cache for remote files
then
return File.Value.Kind = GNATCOLL.IO.File;
else
Ret := File.Value.Is_Regular_File;
if Ret then
File.Value.Kind := GNATCOLL.IO.File;
end if;
return Ret;
end if;
end Is_Regular_File;
----------
-- Size --
----------
function Size (File : Virtual_File) return Long_Integer is
begin
if File.Value = null then
return 0;
else
return File.Value.Size;
end if;
end Size;
------------
-- Rename --
------------
procedure Rename
(File : Virtual_File;
Full_Name : Virtual_File;
Success : out Boolean) is
begin
if File.Value = null
or else Full_Name.Value = null
or else File.Value'Tag /= Full_Name.Value'Tag
then
Success := False;
else
Rename (File.Value, Full_Name.Value, Success);
if Success then
Full_Name.Value.Kind := File.Value.Kind;
File.Value.Kind := Unknown;
end if;
end if;
end Rename;
----------
-- Copy --
----------
procedure Copy
(File : Virtual_File;
Target_Name : Filesystem_String;
Success : out Boolean) is
begin
if File.Value = null then
Success := False;
end if;
if Is_Directory (File) then
File.Value.Copy_Dir (+Target_Name, Success);
else
File.Value.Copy (+Target_Name, Success);
end if;
end Copy;
------------
-- Delete --
------------
procedure Delete
(File : Virtual_File;
Success : out Boolean) is
begin
if File.Value = null then
Success := False;
else
File.Value.Delete (Success);
end if;
end Delete;
-----------------
-- Is_Writable --
-----------------
function Is_Writable (File : Virtual_File) return Boolean is
begin
return File.Value /= null and then File.Value.Is_Writable;
end Is_Writable;
------------------
-- Is_Directory --
------------------
function Is_Directory (VF : Virtual_File) return Boolean is
Ret : Boolean;
begin
if VF.Value = null then
return False;
elsif VF.Value.all not in GNATCOLL.IO.Native.Native_File_Record'Class
and then VF.Value.Kind /= Unknown
-- Only use cache for remote files
then
return VF.Value.Kind = Directory;
else
Ret := VF.Value.Is_Directory;
if Ret then
VF.Ensure_Directory;
VF.Value.Kind := Directory;
end if;
return Ret;
end if;
end Is_Directory;
----------------------
-- Is_Symbolic_Link --
----------------------
function Is_Symbolic_Link (File : Virtual_File) return Boolean is
begin
return File.Value /= null
and then File.Value.Is_Symbolic_Link;
end Is_Symbolic_Link;
----------------------
-- Is_Absolute_Path --
----------------------
function Is_Absolute_Path (File : Virtual_File) return Boolean is
begin
return File.Value /= null
and then Is_Absolute_Path (File.Value.Get_FS, File.Value.Full.all);
end Is_Absolute_Path;
--------------------
-- File_Extension --
--------------------
function File_Extension
(File : Virtual_File;
Normalize : Boolean := False) return Filesystem_String is
begin
if File.Value = null then
return "";
else
return Filesystem_String
(File_Extension
(File.Value.Get_FS, FS_String (File.Full_Name (Normalize).all)));
end if;
end File_Extension;
---------------
-- Read_File --
---------------
function Read_File
(File : Virtual_File) return GNAT.Strings.String_Access
is
begin
if File.Value = null then
return null;
elsif File.Value.Kind = Directory then
return null;
else
return File.Value.Read_Whole_File;
end if;
end Read_File;
----------------
-- Write_File --
----------------
function Write_File
(File : Virtual_File;
Append : Boolean := False) return Writable_File
is
use type GNAT.OS_Lib.File_Descriptor;
W : Writable_File;
begin
if File.Value = null then
return Invalid_File;
end if;
W.File := File;
W.Append := Append;
W.Success := True;
if not Append or else not File.Is_Regular_File then
W.Tmp_File := Create
(File.Full_Name.all & "~",
Host => File.Get_Host);
W.Tmp_File.Value.Open_Write (Append => False, FD => W.FD);
else
W.Tmp_File := No_File;
-- append-mode, and the file already exists.
File.Value.Open_Write (Append => True, FD => W.FD);
end if;
if W.FD = GNAT.OS_Lib.Invalid_FD then
return Invalid_File;
else
return W;
end if;
end Write_File;
-----------
-- Write --
-----------
procedure Write
(File : in out Writable_File;
Str : String)
is
Written : aliased Integer;
begin
if File.Success then
Written := GNAT.OS_Lib.Write (File.FD, Str'Address, Str'Length);
File.Success := Written = Str'Length;
if Written > 0 then
-- File has been overwritten on the disk anyway
if File.Tmp_File /= No_File then
File.Tmp_File.Value.Kind := GNATCOLL.IO.File;
else
File.File.Value.Kind := GNATCOLL.IO.File;
end if;
end if;
end if;
end Write;
-----------
-- Write --
-----------
procedure Write
(File : in out Writable_File;
Str : chars_ptr)
is
Written : aliased Integer;
Len : Integer;
function To_Address is
new Ada.Unchecked_Conversion (chars_ptr, System.Address);
begin
if File.Success then
Len := Integer (Strlen (Str));
Written := GNAT.OS_Lib.Write (File.FD, To_Address (Str), Len);
File.Success := Written = Len;
if Written > 0 then
File.File.Value.Kind := GNATCOLL.IO.File;
end if;
end if;
end Write;
-----------
-- Close --
-----------
procedure Close (File : in out Writable_File) is
Norm : Virtual_File;
Success : Boolean;
pragma Unreferenced (Success);
-- We'll need to force the resolution of symbolic links,
-- since we never want to transform a link into a regular
-- file (which among other things breaks support for CM Synergy)
Save_Handle_Symbolic_Links : constant Boolean := Handle_Symbolic_Links;
begin
if File.Success then
if File.Tmp_File /= No_File then
File.Tmp_File.Value.Close (File.FD, File.Success);
if File.Success then
-- Look past symbolic links
Handle_Symbolic_Links := True;
Norm := Create (File.File.Full_Name
(Normalize => True, Resolve_Links => True).all);
Handle_Symbolic_Links := Save_Handle_Symbolic_Links;
-- We use to delete explicitly Norm. But in fact this is not a
-- good idea, since the directory might allow us to delete the
-- file (or not), but not to recreate it afterwards, as seem to
-- be the case with CM Synergy.
File.Tmp_File.Rename (Norm, File.Success);
if not File.Success then
-- Renaming failed. It might be because it could not
-- remove the original (read-only directory for instance)
-- so let's try with a simple copy instead
File.Tmp_File.Copy
(Norm.Full_Name (Normalize => True).all, File.Success);
if File.Success then
File.Tmp_File.Delete (Success);
-- ignore Success, that's fine if the temp file is
-- still there.
end if;
end if;
end if;
else
File.File.Value.Close (File.FD, File.Success);
end if;
end if;
if not File.Success then
raise Ada.Text_IO.Use_Error with "Error while writing to the file";
end if;
end Close;
------------------
-- Set_Writable --
------------------
procedure Set_Writable (File : VFS.Virtual_File; Writable : Boolean) is
begin
if File.Value = null then
raise VFS_Invalid_File_Error;
end if;
File.Value.Set_Writable (Writable);
end Set_Writable;
------------------
-- Set_Readable --
------------------
procedure Set_Readable (File : VFS.Virtual_File; Readable : Boolean) is
begin
if File.Value = null then
raise VFS_Invalid_File_Error;
end if;
File.Value.Set_Readable (Readable);
end Set_Readable;
---------------------
-- File_Time_Stamp --
---------------------
function File_Time_Stamp
(File : Virtual_File) return Ada.Calendar.Time is
begin
if File.Value = null then
return GNATCOLL.Utils.No_Time;
end if;
return File.Value.File_Time_Stamp;
end File_Time_Stamp;
----------------------
-- Ensure_Directory --
----------------------
procedure Ensure_Directory (Dir : Virtual_File) is
Full : FS_String_Access;
begin
if Dir.Value /= null then
if not Is_Dir_Name (Dir.Value.Get_FS, Dir.Value.Full.all) then
Full := new FS_String'
(Ensure_Directory (Dir.Value.Get_FS, Dir.Value.Full.all));
Free (Dir.Value.Full);
Dir.Value.Full := Full;
end if;
if Dir.Value.Normalized /= null
and then not Is_Dir_Name
(Dir.Value.Get_FS, Dir.Value.Normalized.all)
then
Full := new FS_String'
(Ensure_Directory
(Dir.Value.Get_FS, Dir.Value.Normalized.all));
Free (Dir.Value.Normalized);
Dir.Value.Normalized := Full;
end if;
end if;
end Ensure_Directory;
-----------------------
-- Ensure_Normalized --
-----------------------
procedure Ensure_Normalized
(File : Virtual_File'Class;
Resolve_Symlinks : Boolean) is
begin
if File.Value = null then
return;
end if;
if not File.Value.Resolved
and then Resolve_Symlinks
and then Handle_Symbolic_Links
then
GNATCOLL.IO.Resolve_Symlinks (File.Value);
elsif File.Value.Normalized = null then
File.Value.Normalized := new FS_String'
(Path.Normalize (File.Value.Get_FS, File.Value.Full.all));
end if;
end Ensure_Normalized;
--------------------
-- Normalize_Path --
--------------------
procedure Normalize_Path
(File : Virtual_File;
Resolve_Symlinks : Boolean := False)
is
begin
if File.Value = null then
return;
end if;
Ensure_Normalized (File, Resolve_Symlinks);
Free (File.Value.Full);
File.Value.Full := new FS_String'(File.Value.Normalized.all);
end Normalize_Path;
--------------
-- Get_Root --
--------------
function Get_Root (File : Virtual_File) return Virtual_File is
begin
if File.Value = null then
return No_File;
end if;
return
(Ada.Finalization.Controlled with
Dispatching_Create
(File.Value,
Get_Root (File.Value.Get_FS, File.Value.Full.all)));
end Get_Root;
----------------
-- Get_Parent --
----------------
function Get_Parent (Dir : Virtual_File) return Virtual_File is
begin
if Dir.Value = null then
return No_File;
end if;
declare
Parent : constant FS_String :=
Get_Parent (Dir.Value.Get_FS, Dir.Value.Full.all);
begin
if Parent = "" then
return No_File;
else
return
(Ada.Finalization.Controlled with
Dispatching_Create (Dir.Value, Parent));
end if;
end;
end Get_Parent;
-------------
-- Sub_Dir --
-------------
function Sub_Dir
(Dir : Virtual_File; Name : Filesystem_String) return Virtual_File
is
New_Dir : Virtual_File;
begin
Ensure_Directory (Dir);
New_Dir :=
(Ada.Finalization.Controlled with
Dispatching_Create
(Dir.Value,
GNATCOLL.Path.Path
(Dir.Value.Get_FS, "",
FS_String (Dir.Full_Name.all), FS_String (Name))));
Ensure_Directory (New_Dir);
if Is_Directory (New_Dir) and then True then
return New_Dir;
else
return No_File;
end if;
end Sub_Dir;
----------------
-- Change_Dir --
----------------
procedure Change_Dir (Dir : Virtual_File) is
Success : Boolean;
pragma Unreferenced (Success);
begin
if Dir.Value = null then
Raise_Exception (VFS_Directory_Error'Identity, "Dir is No_File");
end if;
Success := Dir.Value.Change_Dir;
end Change_Dir;
--------------
-- Make_Dir --
--------------
procedure Make_Dir (Dir : Virtual_File; Recursive : Boolean := True) is
Result : Boolean;
begin
if Dir.Value = null then
Raise_Exception (VFS_Directory_Error'Identity, "Dir is No_File");
end if;
-- If Dir already exists and is a directory, then return
if Is_Directory (Dir) then
return;
end if;
Result := Dir.Value.Make_Dir (Recursive);
if not Result then
Dir.Value.Kind := Unknown;
Raise_Exception
(VFS_Directory_Error'Identity, "Dir cannot be created");
else
Dir.Value.Kind := Directory;
end if;
exception
when E : others =>
Raise_Exception
(VFS_Directory_Error'Identity, Exception_Message (E));
end Make_Dir;
----------------
-- Remove_Dir --
----------------
procedure Remove_Dir
(Dir : Virtual_File;
Recursive : Boolean := False;
Success : out Boolean) is
begin
if Dir.Value = null then
raise VFS_Directory_Error;
end if;
Dir.Value.Remove_Dir (Recursive, Success);
if Success then
Dir.Value.Kind := Unknown;
end if;
end Remove_Dir;
--------------
-- Read_Dir --
--------------
function Read_Dir
(Dir : Virtual_File;
Filter : Read_Dir_Filter := All_Files) return File_Array_Access
is
F_Array : File_Array_Access;
Tmp_File : Virtual_File;
begin
if Dir.Value = null then
Raise_Exception (VFS_Directory_Error'Identity, "Dir is No_File");
end if;
Ensure_Directory (Dir);
if not Is_Directory (Dir) then
Raise_Exception
(VFS_Directory_Error'Identity, "Dir is not a directory");
end if;
declare
List : GNAT.Strings.String_List :=
Dir.Value.Read_Dir
(Dirs_Only => Filter = Dirs_Only,
Files_Only => Filter = Files_Only);
begin
F_Array := new File_Array (1 .. List'Length);
for J in List'Range loop
Tmp_File := Dir.Create_From_Dir (+List (J).all);
case Filter is
when Dirs_Only =>
Tmp_File.Value.Kind := Directory;
when Files_Only =>
Tmp_File.Value.Kind := File;
when others =>
null;
end case;
F_Array (F_Array'First + J - List'First) := Tmp_File;
GNAT.Strings.Free (List (J));
end loop;
end;
return F_Array;
exception
when E : others =>
Unchecked_Free (F_Array);
Raise_Exception
(VFS_Directory_Error'Identity,
Exception_Message (E));
end Read_Dir;
------------------------
-- Read_Dir_Recursive --
------------------------
function Read_Dir_Recursive
(Dir : Virtual_File;
Extension : Filesystem_String := "";
Filter : Read_Dir_Filter := All_Files) return File_Array_Access
is
Result : File_Array_Access;
procedure Internal (Directory : Virtual_File);
-- process a directory recursively
procedure Internal (Directory : Virtual_File) is
Files : File_Array_Access := Directory.Read_Dir (Filter => All_Files);
begin
if Files = null then
return;
end if;
for F in Files'Range loop
declare
B : constant Filesystem_String := Files (F).Base_Name;
begin
if B /= "." and then B /= ".." then
if Extension = ""
or else Files (F).File_Extension = Extension
then
case Filter is
when Dirs_Only =>
if Files (F).Is_Directory then
Append (Result, Files (F));
end if;
when Files_Only =>
if Files (F).Is_Regular_File then
Append (Result, Files (F));
end if;
when All_Files =>
Append (Result, Files (F));
end case;
end if;
if Files (F).Is_Directory then
Internal (Files (F));
end if;
end if;
end;
end loop;
Unchecked_Free (Files);
end Internal;
begin
if Dir.Is_Directory then
Internal (Dir);
end if;
return Result;
end Read_Dir_Recursive;
--------------------------
-- Read_Files_From_Dirs --
--------------------------
function Read_Files_From_Dirs
(Dirs : File_Array) return File_Array_Access
is
Ret : File_Array_Access := null;
Files : array (Dirs'Range) of File_Array_Access;
Length : Natural := 0;
Idx : Natural;
begin
for J in Dirs'Range loop
begin
Files (J) := Read_Dir (Dirs (J), Files_Only);
if Files (J) /= null then
Length := Length + Files (J)'Length;
end if;
exception
when VFS_Directory_Error =>
Files (J) := null;
end;
end loop;
if Length = 0 then
return null;
else
Ret := new File_Array (1 .. Length);
Idx := Ret'First;
for J in Files'Range loop
if Files (J) /= null then
Ret (Idx .. Idx + Files (J)'Length - 1) := Files (J).all;
Idx := Idx + Files (J)'Length;
Unchecked_Free (Files (J));
end if;
end loop;
return Ret;
end if;
end Read_Files_From_Dirs;
--------------
-- Open_Dir --
--------------
function Open_Dir (Dir : Virtual_File) return Virtual_Dir is
VDir : Virtual_Dir;
begin
if Dir.Value = null then
return Invalid_Dir;
end if;
VDir.File := Dir;
VDir.Files_List := Read_Dir (Dir);
if VDir.Files_List /= null then
VDir.Current := VDir.Files_List'First - 1;
end if;
Dir.Value.Kind := Directory;
return VDir;
exception
when VFS_Directory_Error =>
return Invalid_Dir;
end Open_Dir;
----------
-- Read --
----------
procedure Read
(VDir : in out Virtual_Dir;
File : out Virtual_File) is
begin
if VDir.Files_List /= null
and then VDir.Current < VDir.Files_List'Last
then
VDir.Current := VDir.Current + 1;
File := VDir.Files_List (VDir.Current);
else
File := No_File;
end if;
end Read;
-----------
-- Close --
-----------
procedure Close (VDir : in out Virtual_Dir) is
begin
if VDir.Files_List /= null then
Unchecked_Free (VDir.Files_List);
end if;
VDir := Invalid_Dir;
end Close;
--------------
-- Finalize --
--------------
procedure Finalize (File : in out Virtual_File) is
Value : GNATCOLL.IO.File_Access := File.Value;
begin
File.Value := null; -- Make Finalize idempotent
if Value /= null then
Unref (Value);
end if;
end Finalize;
------------
-- Adjust --
------------
procedure Adjust (File : in out Virtual_File) is
begin
if File.Value /= null then
Ref (File.Value);
end if;
end Adjust;
---------------
-- Is_Parent --
---------------
function Is_Parent (Parent, Child : Virtual_File) return Boolean is
begin
if Parent.Value = null
or else Child.Value = null
or else Parent.Value'Tag /= Child.Value'Tag
then
return False;
end if;
Ensure_Normalized (Parent, True);
Ensure_Normalized (Child, True);
if Parent.Value.Normalized'Length > Child.Value.Normalized'Length then
return False;
end if;
return Equal
(Parent.Value.Get_FS,
Parent.Value.Normalized.all,
Child.Value.Normalized
(Child.Value.Normalized'First ..
Child.Value.Normalized'First +
Parent.Value.Normalized'Length - 1));
end Is_Parent;
----------
-- Sort --
----------
procedure Sort (Files : in out File_Array) is
-- ??? Right now, this sorts only on the full name. Do we want to
-- provide other choices for sorting ?
procedure Xchg (Op1, Op2 : Natural);
-- Exchanges two items in the array
function Lt (Op1, Op2 : Natural) return Boolean;
-- Return True if the first item is to be sorted before the second
----------
-- Xchg --
----------
procedure Xchg (Op1, Op2 : Natural) is
Buffer : Virtual_File;
begin
Buffer := Files (Files'First - 1 + Op1);
Files (Files'First - 1 + Op1) := Files (Files'First - 1 + Op2);
Files (Files'First - 1 + Op2) := Buffer;
end Xchg;
--------
-- Lt --
--------
function Lt (Op1, Op2 : Natural) return Boolean is
begin
return Files (Files'First - 1 + Op1) <
Files (Files'First - 1 + Op2);
end Lt;
begin
Sort (Files'Length, Xchg'Unrestricted_Access, Lt'Unrestricted_Access);
end Sort;
------------
-- Append --
------------
procedure Append (Files : in out File_Array_Access; F : Virtual_File) is
begin
Append (Files, File_Array'(1 => F));
end Append;
------------
-- Append --
------------
procedure Append (Files : in out File_Array_Access; F : File_Array) is
Tmp : File_Array_Access;
begin
if Files = null then
Files := new File_Array'(F);
else
Tmp := new File_Array (1 .. Files'Length + F'Length);
Tmp (1 .. Files'Length) := Files.all;
Tmp (Files'Length + 1 .. Tmp'Last) := F;
Unchecked_Free (Files);
Files := Tmp;
end if;
end Append;
-------------
-- Prepend --
-------------
procedure Prepend (Files : in out File_Array_Access; F : File_Array) is
Tmp : File_Array_Access;
begin
if Files = null then
Files := new File_Array'(F);
else
Tmp := new File_Array (1 .. Files'Length + F'Length);
Tmp (1 + F'Length .. Tmp'Length) := Files.all;
Tmp (1 .. F'Length) := F;
Unchecked_Free (Files);
Files := Tmp;
end if;
end Prepend;
------------
-- Remove --
------------
procedure Remove (Files : in out File_Array_Access; F : Virtual_File) is
Tmp : File_Array_Access;
begin
for J in Files'Range loop
if Files (J) = F then
for K in J + 1 .. Files'Last loop
Files (K - 1) := Files (K);
end loop;
Tmp := new File_Array'(Files (Files'First .. Files'Last - 1));
Unchecked_Free (Files);
Files := Tmp;
return;
end if;
end loop;
end Remove;
-------------
-- To_Path --
-------------
function To_Path (Paths : File_Array) return Filesystem_String is
Length : Natural := 0;
begin
if Paths'Length = 0 then
return "";
end if;
for J in Paths'Range loop
Length := Length + Paths (J).Full_Name.all'Length;
end loop;
Length := Length + Paths'Length - 1;
declare
Ret : Filesystem_String (1 .. Length);
Idx : Natural := Ret'First;
begin
for J in Paths'Range loop
Ret (Idx .. Idx + Paths (J).Full_Name.all'Length - 1) :=
Paths (J).Full_Name.all;
Idx := Idx + Paths (J).Full_Name.all'Length;
if J /= Paths'Last then
Ret (Idx) := GNAT.OS_Lib.Path_Separator;
Idx := Idx + 1;
end if;
end loop;
return Ret;
end;
end To_Path;
---------------
-- From_Path --
---------------
function From_Path (Path : Filesystem_String) return File_Array is
Ret : File_Array_Access;
Last : Natural := Path'First;
begin
for J in Path'First .. Path'Last loop
-- ??? Should define Path_Separator in FS (system-dependent)
if Path (J) = GNAT.OS_Lib.Path_Separator then
if Last < J then
Append (Ret, Create (Path (Last .. J - 1)));
end if;
Last := J + 1;
end if;
end loop;
if Last <= Path'Last then
Append (Ret, Create (Path (Last .. Path'Last)));
end if;
if Ret = null then
return (1 .. 0 => <>);
end if;
declare
Final : constant File_Array := Ret.all;
begin
Unchecked_Free (Ret);
return Final;
end;
end From_Path;
--------------------
-- Locate_On_Path --
--------------------
function Locate_On_Path
(Base_Name : Filesystem_String;
Path : File_Array) return Virtual_File
is
File : Virtual_File;
begin
for J in Path'Range loop
if Path (J) /= No_File then
File := Create_From_Dir (Path (J), Base_Name);
if Is_Regular_File (File) then
return File;
end if;
File := Create_From_Dir
(Path (J), Base_Name & (+Exe_Extension (Path (J).Value.Get_FS)));
if Is_Regular_File (File) then
return File;
end if;
end if;
end loop;
return No_File;
end Locate_On_Path;
--------------------------
-- Greatest_Common_Path --
--------------------------
function Greatest_Common_Path
(L : GNATCOLL.VFS.File_Array) return Virtual_File
is
begin
if L'Length = 0 then
return GNATCOLL.VFS.No_File;
end if;
declare
Greatest_Prefix : Virtual_File := L (L'First);
Root : constant Virtual_File := Get_Root (Greatest_Prefix);
begin
for J in L'First + 1 .. L'Last loop
-- Loop until GP is a parent of the current File
while not Greatest_Prefix.Is_Parent (L (J)) loop
-- If not a parent, and already at root, then there is no
-- greatest prefix.
if Greatest_Prefix = Root then
return No_File;
end if;
Greatest_Prefix := Get_Parent (Greatest_Prefix);
end loop;
end loop;
return Greatest_Prefix;
end;
end Greatest_Common_Path;
-------------------------
-- Locate_Regular_File --
-------------------------
function Locate_Regular_File
(File_Name : Filesystem_String;
Path : File_Array) return Virtual_File
is
F : Virtual_File;
begin
for J in Path'Range loop
F := Create_From_Dir (Path (J), File_Name);
if F.Is_Regular_File then
return F;
end if;
end loop;
return No_File;
end Locate_Regular_File;
----------------------------
-- Symbolic_Links_Support --
----------------------------
procedure Symbolic_Links_Support (Active : Boolean) is
begin
Handle_Symbolic_Links := Active;
end Symbolic_Links_Support;
end GNATCOLL.VFS;
|