1------------------------------------------------------------------------------ 2-- -- 3-- GNAT RUN-TIME COMPONENTS -- 4-- -- 5-- A D A . D I R E C T O R I E S -- 6-- -- 7-- B o d y -- 8-- -- 9-- Copyright (C) 2004-2015, Free Software Foundation, Inc. -- 10-- -- 11-- GNAT is free software; you can redistribute it and/or modify it under -- 12-- terms of the GNU General Public License as published by the Free Soft- -- 13-- ware Foundation; either version 3, or (at your option) any later ver- -- 14-- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- 15-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- 16-- or FITNESS FOR A PARTICULAR PURPOSE. -- 17-- -- 18-- As a special exception under Section 7 of GPL version 3, you are granted -- 19-- additional permissions described in the GCC Runtime Library Exception, -- 20-- version 3.1, as published by the Free Software Foundation. -- 21-- -- 22-- You should have received a copy of the GNU General Public License and -- 23-- a copy of the GCC Runtime Library Exception along with this program; -- 24-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see -- 25-- <http://www.gnu.org/licenses/>. -- 26-- -- 27-- GNAT was originally developed by the GNAT team at New York University. -- 28-- Extensive contributions were provided by Ada Core Technologies Inc. -- 29-- -- 30------------------------------------------------------------------------------ 31 32with Ada.Calendar; use Ada.Calendar; 33with Ada.Calendar.Formatting; use Ada.Calendar.Formatting; 34with Ada.Characters.Handling; use Ada.Characters.Handling; 35with Ada.Directories.Validity; use Ada.Directories.Validity; 36with Ada.Strings.Fixed; 37with Ada.Strings.Maps; use Ada.Strings.Maps; 38with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; 39with Ada.Unchecked_Deallocation; 40 41with System; use System; 42with System.CRTL; use System.CRTL; 43with System.File_Attributes; use System.File_Attributes; 44with System.File_IO; use System.File_IO; 45with System.OS_Constants; use System.OS_Constants; 46with System.OS_Lib; use System.OS_Lib; 47with System.Regexp; use System.Regexp; 48 49package body Ada.Directories is 50 51 type Dir_Type_Value is new Address; 52 -- This is the low-level address directory structure as returned by the C 53 -- opendir routine. 54 55 No_Dir : constant Dir_Type_Value := Dir_Type_Value (Null_Address); 56 -- Null directory value 57 58 Dir_Separator : constant Character; 59 pragma Import (C, Dir_Separator, "__gnat_dir_separator"); 60 -- Running system default directory separator 61 62 Dir_Seps : constant Character_Set := Strings.Maps.To_Set ("/\"); 63 -- UNIX and DOS style directory separators 64 65 Max_Path : Integer; 66 pragma Import (C, Max_Path, "__gnat_max_path_len"); 67 -- The maximum length of a path 68 69 type Search_Data is record 70 Is_Valid : Boolean := False; 71 Name : Unbounded_String; 72 Pattern : Regexp; 73 Filter : Filter_Type; 74 Dir : Dir_Type_Value := No_Dir; 75 Entry_Fetched : Boolean := False; 76 Dir_Entry : Directory_Entry_Type; 77 end record; 78 -- The current state of a search 79 80 Empty_String : constant String := (1 .. 0 => ASCII.NUL); 81 -- Empty string, returned by function Extension when there is no extension 82 83 procedure Free is new Ada.Unchecked_Deallocation (Search_Data, Search_Ptr); 84 85 procedure Close (Dir : Dir_Type_Value); 86 87 function File_Exists (Name : String) return Boolean; 88 -- Returns True if the named file exists 89 90 procedure Fetch_Next_Entry (Search : Search_Type); 91 -- Get the next entry in a directory, setting Entry_Fetched if successful 92 -- or resetting Is_Valid if not. 93 94 --------------- 95 -- Base_Name -- 96 --------------- 97 98 function Base_Name (Name : String) return String is 99 Simple : constant String := Simple_Name (Name); 100 -- Simple'First is guaranteed to be 1 101 102 begin 103 -- Look for the last dot in the file name and return the part of the 104 -- file name preceding this last dot. If the first dot is the first 105 -- character of the file name, the base name is the empty string. 106 107 for Pos in reverse Simple'Range loop 108 if Simple (Pos) = '.' then 109 return Simple (1 .. Pos - 1); 110 end if; 111 end loop; 112 113 -- If there is no dot, return the complete file name 114 115 return Simple; 116 end Base_Name; 117 118 ----------- 119 -- Close -- 120 ----------- 121 122 procedure Close (Dir : Dir_Type_Value) is 123 Discard : Integer; 124 pragma Warnings (Off, Discard); 125 126 function closedir (directory : DIRs) return Integer; 127 pragma Import (C, closedir, "__gnat_closedir"); 128 129 begin 130 Discard := closedir (DIRs (Dir)); 131 end Close; 132 133 ------------- 134 -- Compose -- 135 ------------- 136 137 function Compose 138 (Containing_Directory : String := ""; 139 Name : String; 140 Extension : String := "") return String 141 is 142 Result : String (1 .. Containing_Directory'Length + 143 Name'Length + Extension'Length + 2); 144 Last : Natural; 145 146 begin 147 -- First, deal with the invalid cases 148 149 if Containing_Directory /= "" 150 and then not Is_Valid_Path_Name (Containing_Directory) 151 then 152 raise Name_Error with 153 "invalid directory path name """ & Containing_Directory & '"'; 154 155 elsif 156 Extension'Length = 0 and then (not Is_Valid_Simple_Name (Name)) 157 then 158 raise Name_Error with 159 "invalid simple name """ & Name & '"'; 160 161 elsif Extension'Length /= 0 162 and then not Is_Valid_Simple_Name (Name & '.' & Extension) 163 then 164 raise Name_Error with 165 "invalid file name """ & Name & '.' & Extension & '"'; 166 167 -- This is not an invalid case so build the path name 168 169 else 170 Last := Containing_Directory'Length; 171 Result (1 .. Last) := Containing_Directory; 172 173 -- Add a directory separator if needed 174 175 if Last /= 0 and then not Is_In (Result (Last), Dir_Seps) then 176 Last := Last + 1; 177 Result (Last) := Dir_Separator; 178 end if; 179 180 -- Add the file name 181 182 Result (Last + 1 .. Last + Name'Length) := Name; 183 Last := Last + Name'Length; 184 185 -- If extension was specified, add dot followed by this extension 186 187 if Extension'Length /= 0 then 188 Last := Last + 1; 189 Result (Last) := '.'; 190 Result (Last + 1 .. Last + Extension'Length) := Extension; 191 Last := Last + Extension'Length; 192 end if; 193 194 return Result (1 .. Last); 195 end if; 196 end Compose; 197 198 -------------------------- 199 -- Containing_Directory -- 200 -------------------------- 201 202 function Containing_Directory (Name : String) return String is 203 begin 204 -- First, the invalid case 205 206 if not Is_Valid_Path_Name (Name) then 207 raise Name_Error with "invalid path name """ & Name & '"'; 208 209 else 210 declare 211 Last_DS : constant Natural := 212 Strings.Fixed.Index (Name, Dir_Seps, Going => Strings.Backward); 213 214 begin 215 if Last_DS = 0 then 216 217 -- There is no directory separator, returns "." representing 218 -- the current working directory. 219 220 return "."; 221 222 -- If Name indicates a root directory, raise Use_Error, because 223 -- it has no containing directory. 224 225 elsif Name = "/" 226 or else 227 (Windows 228 and then 229 (Name = "\" 230 or else 231 (Name'Length = 3 232 and then Name (Name'Last - 1 .. Name'Last) = ":\" 233 and then (Name (Name'First) in 'a' .. 'z' 234 or else 235 Name (Name'First) in 'A' .. 'Z')))) 236 then 237 raise Use_Error with 238 "directory """ & Name & """ has no containing directory"; 239 240 else 241 declare 242 Last : Positive := Last_DS - Name'First + 1; 243 Result : String (1 .. Last); 244 245 begin 246 Result := Name (Name'First .. Last_DS); 247 248 -- Remove any trailing directory separator, except as the 249 -- first character or the first character following a drive 250 -- number on Windows. 251 252 while Last > 1 loop 253 exit when 254 Result (Last) /= '/' 255 and then 256 Result (Last) /= Directory_Separator; 257 258 exit when Windows 259 and then Last = 3 260 and then Result (2) = ':' 261 and then 262 (Result (1) in 'A' .. 'Z' 263 or else 264 Result (1) in 'a' .. 'z'); 265 266 Last := Last - 1; 267 end loop; 268 269 -- Special case of "..": the current directory may be a root 270 -- directory. 271 272 if Last = 2 and then Result (1 .. 2) = ".." then 273 return Containing_Directory (Current_Directory); 274 275 else 276 return Result (1 .. Last); 277 end if; 278 end; 279 end if; 280 end; 281 end if; 282 end Containing_Directory; 283 284 --------------- 285 -- Copy_File -- 286 --------------- 287 288 procedure Copy_File 289 (Source_Name : String; 290 Target_Name : String; 291 Form : String := "") 292 is 293 Success : Boolean; 294 Mode : Copy_Mode := Overwrite; 295 Preserve : Attribute := None; 296 297 begin 298 -- First, the invalid cases 299 300 if not Is_Valid_Path_Name (Source_Name) then 301 raise Name_Error with 302 "invalid source path name """ & Source_Name & '"'; 303 304 elsif not Is_Valid_Path_Name (Target_Name) then 305 raise Name_Error with 306 "invalid target path name """ & Target_Name & '"'; 307 308 elsif not Is_Regular_File (Source_Name) then 309 raise Name_Error with '"' & Source_Name & """ is not a file"; 310 311 elsif Is_Directory (Target_Name) then 312 raise Use_Error with "target """ & Target_Name & """ is a directory"; 313 314 else 315 if Form'Length > 0 then 316 declare 317 Formstr : String (1 .. Form'Length + 1); 318 V1, V2 : Natural; 319 320 begin 321 -- Acquire form string, setting required NUL terminator 322 323 Formstr (1 .. Form'Length) := Form; 324 Formstr (Formstr'Last) := ASCII.NUL; 325 326 -- Convert form string to lower case 327 328 for J in Formstr'Range loop 329 if Formstr (J) in 'A' .. 'Z' then 330 Formstr (J) := 331 Character'Val (Character'Pos (Formstr (J)) + 32); 332 end if; 333 end loop; 334 335 -- Check Form 336 337 Form_Parameter (Formstr, "mode", V1, V2); 338 339 if V1 = 0 then 340 Mode := Overwrite; 341 elsif Formstr (V1 .. V2) = "copy" then 342 Mode := Copy; 343 elsif Formstr (V1 .. V2) = "overwrite" then 344 Mode := Overwrite; 345 elsif Formstr (V1 .. V2) = "append" then 346 Mode := Append; 347 else 348 raise Use_Error with "invalid Form"; 349 end if; 350 351 Form_Parameter (Formstr, "preserve", V1, V2); 352 353 if V1 = 0 then 354 Preserve := None; 355 elsif Formstr (V1 .. V2) = "timestamps" then 356 Preserve := Time_Stamps; 357 elsif Formstr (V1 .. V2) = "all_attributes" then 358 Preserve := Full; 359 elsif Formstr (V1 .. V2) = "no_attributes" then 360 Preserve := None; 361 else 362 raise Use_Error with "invalid Form"; 363 end if; 364 end; 365 end if; 366 367 -- Do actual copy using System.OS_Lib.Copy_File 368 369 Copy_File (Source_Name, Target_Name, Success, Mode, Preserve); 370 371 if not Success then 372 raise Use_Error with "copy of """ & Source_Name & """ failed"; 373 end if; 374 end if; 375 end Copy_File; 376 377 ---------------------- 378 -- Create_Directory -- 379 ---------------------- 380 381 procedure Create_Directory 382 (New_Directory : String; 383 Form : String := "") 384 is 385 C_Dir_Name : constant String := New_Directory & ASCII.NUL; 386 387 begin 388 -- First, the invalid case 389 390 if not Is_Valid_Path_Name (New_Directory) then 391 raise Name_Error with 392 "invalid new directory path name """ & New_Directory & '"'; 393 394 else 395 -- Acquire setting of encoding parameter 396 397 declare 398 Formstr : constant String := To_Lower (Form); 399 400 Encoding : CRTL.Filename_Encoding; 401 -- Filename encoding specified into the form parameter 402 403 V1, V2 : Natural; 404 405 begin 406 Form_Parameter (Formstr, "encoding", V1, V2); 407 408 if V1 = 0 then 409 Encoding := CRTL.Unspecified; 410 elsif Formstr (V1 .. V2) = "utf8" then 411 Encoding := CRTL.UTF8; 412 elsif Formstr (V1 .. V2) = "8bits" then 413 Encoding := CRTL.ASCII_8bits; 414 else 415 raise Use_Error with "invalid Form"; 416 end if; 417 418 if CRTL.mkdir (C_Dir_Name, Encoding) /= 0 then 419 raise Use_Error with 420 "creation of new directory """ & New_Directory & """ failed"; 421 end if; 422 end; 423 end if; 424 end Create_Directory; 425 426 ----------------- 427 -- Create_Path -- 428 ----------------- 429 430 procedure Create_Path 431 (New_Directory : String; 432 Form : String := "") 433 is 434 New_Dir : String (1 .. New_Directory'Length + 1); 435 Last : Positive := 1; 436 Start : Positive := 1; 437 438 begin 439 -- First, the invalid case 440 441 if not Is_Valid_Path_Name (New_Directory) then 442 raise Name_Error with 443 "invalid new directory path name """ & New_Directory & '"'; 444 445 else 446 -- Build New_Dir with a directory separator at the end, so that the 447 -- complete path will be found in the loop below. 448 449 New_Dir (1 .. New_Directory'Length) := New_Directory; 450 New_Dir (New_Dir'Last) := Directory_Separator; 451 452 -- If host is windows, and the first two characters are directory 453 -- separators, we have an UNC path. Skip it. 454 455 if Directory_Separator = '\' 456 and then New_Dir'Length > 2 457 and then Is_In (New_Dir (1), Dir_Seps) 458 and then Is_In (New_Dir (2), Dir_Seps) 459 then 460 Start := 2; 461 loop 462 Start := Start + 1; 463 exit when Start = New_Dir'Last 464 or else Is_In (New_Dir (Start), Dir_Seps); 465 end loop; 466 end if; 467 468 -- Create, if necessary, each directory in the path 469 470 for J in Start + 1 .. New_Dir'Last loop 471 472 -- Look for the end of an intermediate directory 473 474 if not Is_In (New_Dir (J), Dir_Seps) then 475 Last := J; 476 477 -- We have found a new intermediate directory each time we find 478 -- a first directory separator. 479 480 elsif not Is_In (New_Dir (J - 1), Dir_Seps) then 481 482 -- No need to create the directory if it already exists 483 484 if not Is_Directory (New_Dir (1 .. Last)) then 485 begin 486 Create_Directory 487 (New_Directory => New_Dir (1 .. Last), Form => Form); 488 489 exception 490 when Use_Error => 491 if File_Exists (New_Dir (1 .. Last)) then 492 493 -- A file with such a name already exists. If it is 494 -- a directory, then it was apparently just created 495 -- by another process or thread, and all is well. 496 -- If it is of some other kind, report an error. 497 498 if not Is_Directory (New_Dir (1 .. Last)) then 499 raise Use_Error with 500 "file """ & New_Dir (1 .. Last) & 501 """ already exists and is not a directory"; 502 end if; 503 504 else 505 -- Create_Directory failed for some other reason: 506 -- propagate the exception. 507 508 raise; 509 end if; 510 end; 511 end if; 512 end if; 513 end loop; 514 end if; 515 end Create_Path; 516 517 ----------------------- 518 -- Current_Directory -- 519 ----------------------- 520 521 function Current_Directory return String is 522 Path_Len : Natural := Max_Path; 523 Buffer : String (1 .. 1 + Max_Path + 1); 524 525 procedure Local_Get_Current_Dir (Dir : Address; Length : Address); 526 pragma Import (C, Local_Get_Current_Dir, "__gnat_get_current_dir"); 527 528 begin 529 Local_Get_Current_Dir (Buffer'Address, Path_Len'Address); 530 531 -- We need to resolve links because of RM A.16(47), which requires 532 -- that we not return alternative names for files. 533 534 return Normalize_Pathname (Buffer (1 .. Path_Len)); 535 end Current_Directory; 536 537 ---------------------- 538 -- Delete_Directory -- 539 ---------------------- 540 541 procedure Delete_Directory (Directory : String) is 542 begin 543 -- First, the invalid cases 544 545 if not Is_Valid_Path_Name (Directory) then 546 raise Name_Error with 547 "invalid directory path name """ & Directory & '"'; 548 549 elsif not Is_Directory (Directory) then 550 raise Name_Error with '"' & Directory & """ not a directory"; 551 552 -- Do the deletion, checking for error 553 554 else 555 declare 556 C_Dir_Name : constant String := Directory & ASCII.NUL; 557 begin 558 if rmdir (C_Dir_Name) /= 0 then 559 raise Use_Error with 560 "deletion of directory """ & Directory & """ failed"; 561 end if; 562 end; 563 end if; 564 end Delete_Directory; 565 566 ----------------- 567 -- Delete_File -- 568 ----------------- 569 570 procedure Delete_File (Name : String) is 571 Success : Boolean; 572 573 begin 574 -- First, the invalid cases 575 576 if not Is_Valid_Path_Name (Name) then 577 raise Name_Error with "invalid path name """ & Name & '"'; 578 579 elsif not Is_Regular_File (Name) 580 and then not Is_Symbolic_Link (Name) 581 then 582 raise Name_Error with "file """ & Name & """ does not exist"; 583 584 else 585 -- Do actual deletion using System.OS_Lib.Delete_File 586 587 Delete_File (Name, Success); 588 589 if not Success then 590 raise Use_Error with "file """ & Name & """ could not be deleted"; 591 end if; 592 end if; 593 end Delete_File; 594 595 ----------------- 596 -- Delete_Tree -- 597 ----------------- 598 599 procedure Delete_Tree (Directory : String) is 600 Current_Dir : constant String := Current_Directory; 601 Search : Search_Type; 602 Dir_Ent : Directory_Entry_Type; 603 begin 604 -- First, the invalid cases 605 606 if not Is_Valid_Path_Name (Directory) then 607 raise Name_Error with 608 "invalid directory path name """ & Directory & '"'; 609 610 elsif not Is_Directory (Directory) then 611 raise Name_Error with '"' & Directory & """ not a directory"; 612 613 else 614 Set_Directory (Directory); 615 616 Start_Search (Search, Directory => ".", Pattern => ""); 617 while More_Entries (Search) loop 618 Get_Next_Entry (Search, Dir_Ent); 619 620 declare 621 File_Name : constant String := Simple_Name (Dir_Ent); 622 623 begin 624 if OS_Lib.Is_Directory (File_Name) then 625 if File_Name /= "." and then File_Name /= ".." then 626 Delete_Tree (File_Name); 627 end if; 628 629 else 630 Delete_File (File_Name); 631 end if; 632 end; 633 end loop; 634 635 Set_Directory (Current_Dir); 636 End_Search (Search); 637 638 declare 639 C_Dir_Name : constant String := Directory & ASCII.NUL; 640 641 begin 642 if rmdir (C_Dir_Name) /= 0 then 643 raise Use_Error with 644 "directory tree rooted at """ & 645 Directory & """ could not be deleted"; 646 end if; 647 end; 648 end if; 649 end Delete_Tree; 650 651 ------------ 652 -- Exists -- 653 ------------ 654 655 function Exists (Name : String) return Boolean is 656 begin 657 -- First, the invalid case 658 659 if not Is_Valid_Path_Name (Name) then 660 raise Name_Error with "invalid path name """ & Name & '"'; 661 662 else 663 -- The implementation is in File_Exists 664 665 return File_Exists (Name); 666 end if; 667 end Exists; 668 669 --------------- 670 -- Extension -- 671 --------------- 672 673 function Extension (Name : String) return String is 674 begin 675 -- First, the invalid case 676 677 if not Is_Valid_Path_Name (Name) then 678 raise Name_Error with "invalid path name """ & Name & '"'; 679 680 else 681 -- Look for first dot that is not followed by a directory separator 682 683 for Pos in reverse Name'Range loop 684 685 -- If a directory separator is found before a dot, there is no 686 -- extension. 687 688 if Is_In (Name (Pos), Dir_Seps) then 689 return Empty_String; 690 691 elsif Name (Pos) = '.' then 692 693 -- We found a dot, build the return value with lower bound 1 694 695 declare 696 subtype Result_Type is String (1 .. Name'Last - Pos); 697 begin 698 return Result_Type (Name (Pos + 1 .. Name'Last)); 699 end; 700 end if; 701 end loop; 702 703 -- No dot were found, there is no extension 704 705 return Empty_String; 706 end if; 707 end Extension; 708 709 ---------------------- 710 -- Fetch_Next_Entry -- 711 ---------------------- 712 713 procedure Fetch_Next_Entry (Search : Search_Type) is 714 Name : String (1 .. NAME_MAX); 715 Last : Natural; 716 717 Kind : File_Kind := Ordinary_File; 718 -- Initialized to avoid a compilation warning 719 720 Filename_Addr : Address; 721 Filename_Len : aliased Integer; 722 723 Buffer : array (1 .. SIZEOF_struct_dirent_alloc) of Character; 724 725 function readdir_gnat 726 (Directory : Address; 727 Buffer : Address; 728 Last : not null access Integer) return Address; 729 pragma Import (C, readdir_gnat, "__gnat_readdir"); 730 731 begin 732 -- Search.Value.Is_Valid is always True when Fetch_Next_Entry is called 733 734 loop 735 Filename_Addr := 736 readdir_gnat 737 (Address (Search.Value.Dir), 738 Buffer'Address, 739 Filename_Len'Access); 740 741 -- If no matching entry is found, set Is_Valid to False 742 743 if Filename_Addr = Null_Address then 744 Search.Value.Is_Valid := False; 745 exit; 746 end if; 747 748 if Filename_Len > Name'Length then 749 raise Use_Error with "file name too long"; 750 end if; 751 752 declare 753 subtype Name_String is String (1 .. Filename_Len); 754 Dent_Name : Name_String; 755 for Dent_Name'Address use Filename_Addr; 756 pragma Import (Ada, Dent_Name); 757 758 begin 759 Last := Filename_Len; 760 Name (1 .. Last) := Dent_Name; 761 end; 762 763 -- Check if the entry matches the pattern 764 765 if Match (Name (1 .. Last), Search.Value.Pattern) then 766 declare 767 C_Full_Name : constant String := 768 Compose (To_String (Search.Value.Name), 769 Name (1 .. Last)) & ASCII.NUL; 770 Full_Name : String renames 771 C_Full_Name 772 (C_Full_Name'First .. C_Full_Name'Last - 1); 773 Found : Boolean := False; 774 Attr : aliased File_Attributes; 775 Exists : Integer; 776 Error : Integer; 777 778 begin 779 Reset_Attributes (Attr'Access); 780 Exists := File_Exists_Attr (C_Full_Name'Address, Attr'Access); 781 Error := Error_Attributes (Attr'Access); 782 783 if Error /= 0 then 784 raise Use_Error 785 with Full_Name & ": " & Errno_Message (Err => Error); 786 end if; 787 788 if Exists = 1 then 789 790 -- Now check if the file kind matches the filter 791 792 if Is_Regular_File_Attr 793 (C_Full_Name'Address, Attr'Access) = 1 794 then 795 if Search.Value.Filter (Ordinary_File) then 796 Kind := Ordinary_File; 797 Found := True; 798 end if; 799 800 elsif Is_Directory_Attr 801 (C_Full_Name'Address, Attr'Access) = 1 802 then 803 if Search.Value.Filter (Directory) then 804 Kind := Directory; 805 Found := True; 806 end if; 807 808 elsif Search.Value.Filter (Special_File) then 809 Kind := Special_File; 810 Found := True; 811 end if; 812 813 -- If it does, update Search and return 814 815 if Found then 816 Search.Value.Entry_Fetched := True; 817 Search.Value.Dir_Entry := 818 (Is_Valid => True, 819 Simple => To_Unbounded_String (Name (1 .. Last)), 820 Full => To_Unbounded_String (Full_Name), 821 Kind => Kind); 822 exit; 823 end if; 824 end if; 825 end; 826 end if; 827 end loop; 828 end Fetch_Next_Entry; 829 830 ----------------- 831 -- File_Exists -- 832 ----------------- 833 834 function File_Exists (Name : String) return Boolean is 835 function C_File_Exists (A : Address) return Integer; 836 pragma Import (C, C_File_Exists, "__gnat_file_exists"); 837 838 C_Name : String (1 .. Name'Length + 1); 839 840 begin 841 C_Name (1 .. Name'Length) := Name; 842 C_Name (C_Name'Last) := ASCII.NUL; 843 return C_File_Exists (C_Name'Address) = 1; 844 end File_Exists; 845 846 -------------- 847 -- Finalize -- 848 -------------- 849 850 procedure Finalize (Search : in out Search_Type) is 851 begin 852 if Search.Value /= null then 853 854 -- Close the directory, if one is open 855 856 if Search.Value.Dir /= No_Dir then 857 Close (Search.Value.Dir); 858 end if; 859 860 Free (Search.Value); 861 end if; 862 end Finalize; 863 864 --------------- 865 -- Full_Name -- 866 --------------- 867 868 function Full_Name (Name : String) return String is 869 begin 870 -- First, the invalid case 871 872 if not Is_Valid_Path_Name (Name) then 873 raise Name_Error with "invalid path name """ & Name & '"'; 874 875 else 876 -- Build the return value with lower bound 1 877 878 -- Use System.OS_Lib.Normalize_Pathname 879 880 declare 881 -- We need to resolve links because of (RM A.16(47)), which says 882 -- we must not return alternative names for files. 883 884 Value : constant String := Normalize_Pathname (Name); 885 subtype Result is String (1 .. Value'Length); 886 887 begin 888 return Result (Value); 889 end; 890 end if; 891 end Full_Name; 892 893 function Full_Name (Directory_Entry : Directory_Entry_Type) return String is 894 begin 895 -- First, the invalid case 896 897 if not Directory_Entry.Is_Valid then 898 raise Status_Error with "invalid directory entry"; 899 900 else 901 -- The value to return has already been computed 902 903 return To_String (Directory_Entry.Full); 904 end if; 905 end Full_Name; 906 907 -------------------- 908 -- Get_Next_Entry -- 909 -------------------- 910 911 procedure Get_Next_Entry 912 (Search : in out Search_Type; 913 Directory_Entry : out Directory_Entry_Type) 914 is 915 begin 916 -- First, the invalid case 917 918 if Search.Value = null or else not Search.Value.Is_Valid then 919 raise Status_Error with "invalid search"; 920 end if; 921 922 -- Fetch the next entry, if needed 923 924 if not Search.Value.Entry_Fetched then 925 Fetch_Next_Entry (Search); 926 end if; 927 928 -- It is an error if no valid entry is found 929 930 if not Search.Value.Is_Valid then 931 raise Status_Error with "no next entry"; 932 933 else 934 -- Reset Entry_Fetched and return the entry 935 936 Search.Value.Entry_Fetched := False; 937 Directory_Entry := Search.Value.Dir_Entry; 938 end if; 939 end Get_Next_Entry; 940 941 ---------- 942 -- Kind -- 943 ---------- 944 945 function Kind (Name : String) return File_Kind is 946 begin 947 -- First, the invalid case 948 949 if not File_Exists (Name) then 950 raise Name_Error with "file """ & Name & """ does not exist"; 951 952 -- If OK, return appropriate kind 953 954 elsif Is_Regular_File (Name) then 955 return Ordinary_File; 956 957 elsif Is_Directory (Name) then 958 return Directory; 959 960 else 961 return Special_File; 962 end if; 963 end Kind; 964 965 function Kind (Directory_Entry : Directory_Entry_Type) return File_Kind is 966 begin 967 -- First, the invalid case 968 969 if not Directory_Entry.Is_Valid then 970 raise Status_Error with "invalid directory entry"; 971 972 else 973 -- The value to return has already be computed 974 975 return Directory_Entry.Kind; 976 end if; 977 end Kind; 978 979 ----------------------- 980 -- Modification_Time -- 981 ----------------------- 982 983 function Modification_Time (Name : String) return Time is 984 Date : OS_Time; 985 Year : Year_Type; 986 Month : Month_Type; 987 Day : Day_Type; 988 Hour : Hour_Type; 989 Minute : Minute_Type; 990 Second : Second_Type; 991 992 begin 993 -- First, the invalid cases 994 995 if not (Is_Regular_File (Name) or else Is_Directory (Name)) then 996 raise Name_Error with '"' & Name & """ not a file or directory"; 997 998 else 999 Date := File_Time_Stamp (Name); 1000 1001 -- Break down the time stamp into its constituents relative to GMT. 1002 -- This version of Split does not recognize leap seconds or buffer 1003 -- space for time zone processing. 1004 1005 GM_Split (Date, Year, Month, Day, Hour, Minute, Second); 1006 1007 -- The result must be in GMT. Ada.Calendar. 1008 -- Formatting.Time_Of with default time zone of zero (0) is the 1009 -- routine of choice. 1010 1011 return Time_Of (Year, Month, Day, Hour, Minute, Second, 0.0); 1012 end if; 1013 end Modification_Time; 1014 1015 function Modification_Time 1016 (Directory_Entry : Directory_Entry_Type) return Ada.Calendar.Time 1017 is 1018 begin 1019 -- First, the invalid case 1020 1021 if not Directory_Entry.Is_Valid then 1022 raise Status_Error with "invalid directory entry"; 1023 1024 else 1025 -- The value to return has already be computed 1026 1027 return Modification_Time (To_String (Directory_Entry.Full)); 1028 end if; 1029 end Modification_Time; 1030 1031 ------------------ 1032 -- More_Entries -- 1033 ------------------ 1034 1035 function More_Entries (Search : Search_Type) return Boolean is 1036 begin 1037 if Search.Value = null then 1038 return False; 1039 1040 elsif Search.Value.Is_Valid then 1041 1042 -- Fetch the next entry, if needed 1043 1044 if not Search.Value.Entry_Fetched then 1045 Fetch_Next_Entry (Search); 1046 end if; 1047 end if; 1048 1049 return Search.Value.Is_Valid; 1050 end More_Entries; 1051 1052 ------------ 1053 -- Rename -- 1054 ------------ 1055 1056 procedure Rename (Old_Name, New_Name : String) is 1057 Success : Boolean; 1058 1059 begin 1060 -- First, the invalid cases 1061 1062 if not Is_Valid_Path_Name (Old_Name) then 1063 raise Name_Error with "invalid old path name """ & Old_Name & '"'; 1064 1065 elsif not Is_Valid_Path_Name (New_Name) then 1066 raise Name_Error with "invalid new path name """ & New_Name & '"'; 1067 1068 elsif not Is_Regular_File (Old_Name) 1069 and then not Is_Directory (Old_Name) 1070 then 1071 raise Name_Error with "old file """ & Old_Name & """ does not exist"; 1072 1073 elsif Is_Regular_File (New_Name) or else Is_Directory (New_Name) then 1074 raise Use_Error with 1075 "new name """ & New_Name 1076 & """ designates a file that already exists"; 1077 1078 -- Do actual rename using System.OS_Lib.Rename_File 1079 1080 else 1081 Rename_File (Old_Name, New_Name, Success); 1082 1083 if not Success then 1084 1085 -- AI05-0231-1: Name_Error should be raised in case a directory 1086 -- component of New_Name does not exist (as in New_Name => 1087 -- "/no-such-dir/new-filename"). ENOENT indicates that. ENOENT 1088 -- also indicate that the Old_Name does not exist, but we already 1089 -- checked for that above. All other errors are Use_Error. 1090 1091 if Errno = ENOENT then 1092 raise Name_Error with 1093 "file """ & Containing_Directory (New_Name) & """ not found"; 1094 1095 else 1096 raise Use_Error with 1097 "file """ & Old_Name & """ could not be renamed"; 1098 end if; 1099 end if; 1100 end if; 1101 end Rename; 1102 1103 ------------ 1104 -- Search -- 1105 ------------ 1106 1107 procedure Search 1108 (Directory : String; 1109 Pattern : String; 1110 Filter : Filter_Type := (others => True); 1111 Process : not null access procedure 1112 (Directory_Entry : Directory_Entry_Type)) 1113 is 1114 Srch : Search_Type; 1115 Directory_Entry : Directory_Entry_Type; 1116 1117 begin 1118 Start_Search (Srch, Directory, Pattern, Filter); 1119 while More_Entries (Srch) loop 1120 Get_Next_Entry (Srch, Directory_Entry); 1121 Process (Directory_Entry); 1122 end loop; 1123 1124 End_Search (Srch); 1125 end Search; 1126 1127 ------------------- 1128 -- Set_Directory -- 1129 ------------------- 1130 1131 procedure Set_Directory (Directory : String) is 1132 C_Dir_Name : constant String := Directory & ASCII.NUL; 1133 begin 1134 if not Is_Valid_Path_Name (Directory) then 1135 raise Name_Error with 1136 "invalid directory path name & """ & Directory & '"'; 1137 1138 elsif not Is_Directory (Directory) then 1139 raise Name_Error with 1140 "directory """ & Directory & """ does not exist"; 1141 1142 elsif chdir (C_Dir_Name) /= 0 then 1143 raise Name_Error with 1144 "could not set to designated directory """ & Directory & '"'; 1145 end if; 1146 end Set_Directory; 1147 1148 ----------------- 1149 -- Simple_Name -- 1150 ----------------- 1151 1152 function Simple_Name (Name : String) return String is 1153 1154 function Simple_Name_Internal (Path : String) return String; 1155 -- This function does the job 1156 1157 -------------------------- 1158 -- Simple_Name_Internal -- 1159 -------------------------- 1160 1161 function Simple_Name_Internal (Path : String) return String is 1162 Cut_Start : Natural := 1163 Strings.Fixed.Index (Path, Dir_Seps, Going => Strings.Backward); 1164 Cut_End : Natural; 1165 1166 begin 1167 -- Cut_Start pointS to the first simple name character 1168 1169 Cut_Start := (if Cut_Start = 0 then Path'First else Cut_Start + 1); 1170 1171 -- Cut_End point to the last simple name character 1172 1173 Cut_End := Path'Last; 1174 1175 Check_For_Standard_Dirs : declare 1176 BN : constant String := Path (Cut_Start .. Cut_End); 1177 1178 Has_Drive_Letter : constant Boolean := 1179 OS_Lib.Path_Separator /= ':'; 1180 -- If Path separator is not ':' then we are on a DOS based OS 1181 -- where this character is used as a drive letter separator. 1182 1183 begin 1184 if BN = "." or else BN = ".." then 1185 return ""; 1186 1187 elsif Has_Drive_Letter 1188 and then BN'Length > 2 1189 and then Characters.Handling.Is_Letter (BN (BN'First)) 1190 and then BN (BN'First + 1) = ':' 1191 then 1192 -- We have a DOS drive letter prefix, remove it 1193 1194 return BN (BN'First + 2 .. BN'Last); 1195 1196 else 1197 return BN; 1198 end if; 1199 end Check_For_Standard_Dirs; 1200 end Simple_Name_Internal; 1201 1202 -- Start of processing for Simple_Name 1203 1204 begin 1205 -- First, the invalid case 1206 1207 if not Is_Valid_Path_Name (Name) then 1208 raise Name_Error with "invalid path name """ & Name & '"'; 1209 1210 else 1211 -- Build the value to return with lower bound 1 1212 1213 declare 1214 Value : constant String := Simple_Name_Internal (Name); 1215 subtype Result is String (1 .. Value'Length); 1216 begin 1217 return Result (Value); 1218 end; 1219 end if; 1220 end Simple_Name; 1221 1222 function Simple_Name 1223 (Directory_Entry : Directory_Entry_Type) return String is 1224 begin 1225 -- First, the invalid case 1226 1227 if not Directory_Entry.Is_Valid then 1228 raise Status_Error with "invalid directory entry"; 1229 1230 else 1231 -- The value to return has already be computed 1232 1233 return To_String (Directory_Entry.Simple); 1234 end if; 1235 end Simple_Name; 1236 1237 ---------- 1238 -- Size -- 1239 ---------- 1240 1241 function Size (Name : String) return File_Size is 1242 C_Name : String (1 .. Name'Length + 1); 1243 1244 function C_Size (Name : Address) return int64; 1245 pragma Import (C, C_Size, "__gnat_named_file_length"); 1246 1247 begin 1248 -- First, the invalid case 1249 1250 if not Is_Regular_File (Name) then 1251 raise Name_Error with "file """ & Name & """ does not exist"; 1252 1253 else 1254 C_Name (1 .. Name'Length) := Name; 1255 C_Name (C_Name'Last) := ASCII.NUL; 1256 return File_Size (C_Size (C_Name'Address)); 1257 end if; 1258 end Size; 1259 1260 function Size (Directory_Entry : Directory_Entry_Type) return File_Size is 1261 begin 1262 -- First, the invalid case 1263 1264 if not Directory_Entry.Is_Valid then 1265 raise Status_Error with "invalid directory entry"; 1266 1267 else 1268 -- The value to return has already be computed 1269 1270 return Size (To_String (Directory_Entry.Full)); 1271 end if; 1272 end Size; 1273 1274 ------------------ 1275 -- Start_Search -- 1276 ------------------ 1277 1278 procedure Start_Search 1279 (Search : in out Search_Type; 1280 Directory : String; 1281 Pattern : String; 1282 Filter : Filter_Type := (others => True)) 1283 is 1284 function opendir (file_name : String) return DIRs; 1285 pragma Import (C, opendir, "__gnat_opendir"); 1286 1287 C_File_Name : constant String := Directory & ASCII.NUL; 1288 Pat : Regexp; 1289 Dir : Dir_Type_Value; 1290 1291 begin 1292 -- First, the invalid case Name_Error 1293 1294 if not Is_Directory (Directory) then 1295 raise Name_Error with 1296 "unknown directory """ & Simple_Name (Directory) & '"'; 1297 end if; 1298 1299 -- Check the pattern 1300 1301 begin 1302 Pat := Compile 1303 (Pattern, 1304 Glob => True, 1305 Case_Sensitive => Is_Path_Name_Case_Sensitive); 1306 exception 1307 when Error_In_Regexp => 1308 Free (Search.Value); 1309 raise Name_Error with "invalid pattern """ & Pattern & '"'; 1310 end; 1311 1312 Dir := Dir_Type_Value (opendir (C_File_Name)); 1313 1314 if Dir = No_Dir then 1315 raise Use_Error with 1316 "unreadable directory """ & Simple_Name (Directory) & '"'; 1317 end if; 1318 1319 -- If needed, finalize Search 1320 1321 Finalize (Search); 1322 1323 -- Allocate the default data 1324 1325 Search.Value := new Search_Data; 1326 1327 -- Initialize some Search components 1328 1329 Search.Value.Filter := Filter; 1330 Search.Value.Name := To_Unbounded_String (Full_Name (Directory)); 1331 Search.Value.Pattern := Pat; 1332 Search.Value.Dir := Dir; 1333 Search.Value.Is_Valid := True; 1334 end Start_Search; 1335 1336end Ada.Directories; 1337