1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . D I R E C T O R I E S --
9 -- Copyright (C) 2004-2009, Free Software Foundation, Inc. --
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. --
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. --
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/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 with Ada
.Calendar
; use Ada
.Calendar
;
33 with Ada
.Calendar
.Formatting
; use Ada
.Calendar
.Formatting
;
34 with Ada
.Directories
.Validity
; use Ada
.Directories
.Validity
;
35 with Ada
.Strings
.Maps
;
36 with Ada
.Strings
.Fixed
;
37 with Ada
.Strings
.Unbounded
; use Ada
.Strings
.Unbounded
;
38 with Ada
.Unchecked_Conversion
;
39 with Ada
.Unchecked_Deallocation
;
40 with Ada
.Characters
.Handling
; use Ada
.Characters
.Handling
;
42 with System
.CRTL
; use System
.CRTL
;
43 with System
.OS_Lib
; use System
.OS_Lib
;
44 with System
.Regexp
; use System
.Regexp
;
48 package body Ada
.Directories
is
50 Filename_Max
: constant Integer := 1024;
51 -- 1024 is the value of FILENAME_MAX in stdio.h
53 type Dir_Type_Value
is new System
.Address
;
54 -- This is the low-level address directory structure as returned by the C
57 No_Dir
: constant Dir_Type_Value
:= Dir_Type_Value
(System
.Null_Address
);
59 Dir_Separator
: constant Character;
60 pragma Import
(C
, Dir_Separator
, "__gnat_dir_separator");
61 -- Running system default directory separator
63 Dir_Seps
: constant Ada
.Strings
.Maps
.Character_Set
:=
64 Ada
.Strings
.Maps
.To_Set
("/\");
65 -- UNIX and DOS style directory separators
68 pragma Import (C, Max_Path, "__gnat_max_path_len
");
69 -- The maximum length of a path
71 type Search_Data is record
72 Is_Valid : Boolean := False;
73 Name : Unbounded_String;
76 Dir : Dir_Type_Value := No_Dir;
77 Entry_Fetched : Boolean := False;
78 Dir_Entry : Directory_Entry_Type;
80 -- The current state of a search
82 Empty_String : constant String := (1 .. 0 => ASCII.NUL);
83 -- Empty string, returned by function Extension when there is no extension
85 procedure Free is new Ada.Unchecked_Deallocation (Search_Data, Search_Ptr);
87 procedure Close (Dir : Dir_Type_Value);
89 function File_Exists (Name : String) return Boolean;
90 -- Returns True if the named file exists
92 procedure Fetch_Next_Entry (Search : Search_Type);
93 -- Get the next entry in a directory, setting Entry_Fetched if successful
94 -- or resetting Is_Valid if not.
100 function Base_Name (Name : String) return String is
101 Simple : constant String := Simple_Name (Name);
102 -- Simple'First is guaranteed to be 1
105 -- Look for the last dot in the file name and return the part of the
106 -- file name preceding this last dot. If the first dot is the first
107 -- character of the file name, the base name is the empty string.
109 for Pos in reverse Simple'Range loop
110 if Simple (Pos) = '.' then
111 return Simple (1 .. Pos - 1);
115 -- If there is no dot, return the complete file name
124 procedure Close (Dir : Dir_Type_Value) is
126 pragma Warnings (Off, Discard);
128 function closedir (directory : DIRs) return Integer;
129 pragma Import (C, closedir, "__gnat_closedir
");
132 Discard := closedir (DIRs (Dir));
140 (Containing_Directory : String := "";
142 Extension : String := "") return String
144 Result : String (1 .. Containing_Directory'Length +
145 Name'Length + Extension'Length + 2);
149 -- First, deal with the invalid cases
151 if Containing_Directory /= ""
152 and then not Is_Valid_Path_Name (Containing_Directory)
154 raise Name_Error with
155 "invalid directory path name
""" & Containing_Directory & '"';
158 Extension'Length = 0 and then (not Is_Valid_Simple_Name (Name))
160 raise Name_Error with
161 "invalid simple name """ & Name & '"';
163 elsif Extension'Length /= 0
164 and then not Is_Valid_Simple_Name (Name & '.' & Extension)
166 raise Name_Error with
167 "invalid file name
""" & Name & '.' & Extension & '"';
169 -- This is not an invalid case so build the path name
172 Last := Containing_Directory'Length;
173 Result (1 .. Last) := Containing_Directory;
175 -- Add a directory separator if needed
177 if Last /= 0 and then Result (Last) /= Dir_Separator then
179 Result (Last) := Dir_Separator;
184 Result (Last + 1 .. Last + Name'Length) := Name;
185 Last := Last + Name'Length;
187 -- If extension was specified, add dot followed by this extension
189 if Extension'Length /= 0 then
191 Result (Last) := '.';
192 Result (Last + 1 .. Last + Extension'Length) := Extension;
193 Last := Last + Extension'Length;
196 return Result (1 .. Last);
200 --------------------------
201 -- Containing_Directory --
202 --------------------------
204 function Containing_Directory (Name : String) return String is
206 -- First, the invalid case
208 if not Is_Valid_Path_Name (Name) then
209 raise Name_Error with "invalid path name """ & Name & '"';
213 Norm : constant String := Normalize_Pathname (Name);
214 Last_DS : constant Natural :=
216 (Name, Dir_Seps, Going => Strings.Backward);
221 -- There is no directory separator, returns current working
224 return Current_Directory;
226 -- If Name indicates a root directory, raise Use_Error, because
227 -- it has no containing directory.
236 and then Norm
(Norm
'Last - 1 .. Norm
'Last) = ":\"
237 and then (Norm (Norm'First) in 'a' .. 'z'
238 or else Norm (Norm'First) in 'A' .. 'Z'))))
241 "directory
""" & Name & """ has no containing directory
";
245 Last : Positive := Last_DS - Name'First + 1;
246 Result : String (1 .. Last);
249 Result := Name (Name'First .. Last_DS);
251 -- Remove any trailing directory separator, except as the
252 -- first character or the first character following a drive
253 -- number on Windows.
259 Result (Last) /= Directory_Separator;
263 and then Result (2) = ':'
265 (Result (1) in 'A' .. 'Z'
267 Result (1) in 'a' .. 'z');
272 -- Special case of current directory, identified by "."
274 if Last = 1 and then Result (1) = '.' then
275 return Current_Directory;
277 -- Special case of "..": the current directory may be a root
280 elsif Last = 2 and then Result (1 .. 2) = ".." then
281 return Containing_Directory (Current_Directory);
284 return Result (1 .. Last);
290 end Containing_Directory;
297 (Source_Name : String;
298 Target_Name : String;
301 pragma Unreferenced (Form);
305 -- First, the invalid cases
307 if not Is_Valid_Path_Name (Source_Name) then
308 raise Name_Error with
309 "invalid source path name
""" & Source_Name & '"';
311 elsif not Is_Valid_Path_Name (Target_Name) then
312 raise Name_Error with
313 "invalid target path name """ & Target_Name & '"';
315 elsif not Is_Regular_File (Source_Name) then
316 raise Name_Error with '"' & Source_Name & """ is not a file";
318 elsif Is_Directory (Target_Name) then
319 raise Use_Error with "target """ & Target_Name & """ is a directory";
322 -- The implementation uses System.OS_Lib.Copy_File, with parameters
323 -- suitable for all platforms.
325 Copy_File (Source_Name, Target_Name, Success, Overwrite, None);
328 raise Use_Error with "copy of """ & Source_Name & """ failed";
333 ----------------------
334 -- Create_Directory --
335 ----------------------
337 procedure Create_Directory
338 (New_Directory : String;
341 pragma Unreferenced (Form);
343 C_Dir_Name : constant String := New_Directory & ASCII.NUL;
345 function mkdir (Dir_Name : String) return Integer;
346 pragma Import (C, mkdir, "__gnat_mkdir");
349 -- First, the invalid case
351 if not Is_Valid_Path_Name (New_Directory) then
352 raise Name_Error with
353 "invalid new directory path name """ & New_Directory & '"';
356 if mkdir (C_Dir_Name) /= 0 then
358 "creation
of new directory
""" & New_Directory & """ failed
";
361 end Create_Directory;
367 procedure Create_Path
368 (New_Directory : String;
371 pragma Unreferenced (Form);
373 New_Dir : String (1 .. New_Directory'Length + 1);
374 Last : Positive := 1;
377 -- First, the invalid case
379 if not Is_Valid_Path_Name (New_Directory) then
380 raise Name_Error with
381 "invalid
new directory path name
""" & New_Directory & '"';
384 -- Build New_Dir with a directory separator at the end, so that the
385 -- complete path will be found in the loop below.
387 New_Dir (1 .. New_Directory'Length) := New_Directory;
388 New_Dir (New_Dir'Last) := Directory_Separator;
390 -- Create, if necessary, each directory in the path
392 for J in 2 .. New_Dir'Last loop
394 -- Look for the end of an intermediate directory
396 if New_Dir (J) /= Dir_Separator and then
401 -- We have found a new intermediate directory each time we find
402 -- a first directory separator.
404 elsif New_Dir (J - 1) /= Dir_Separator and then
405 New_Dir (J - 1) /= '/'
408 -- No need to create the directory if it already exists
410 if Is_Directory (New_Dir (1 .. Last)) then
413 -- It is an error if a file with such a name already exists
415 elsif Is_Regular_File (New_Dir (1 .. Last)) then
417 "file """ & New_Dir (1 .. Last) & """ already exists";
420 Create_Directory (New_Directory => New_Dir (1 .. Last));
427 -----------------------
428 -- Current_Directory --
429 -----------------------
431 function Current_Directory return String is
432 Path_Len : Natural := Max_Path;
433 Buffer : String (1 .. 1 + Max_Path + 1);
435 procedure Local_Get_Current_Dir
436 (Dir : System.Address;
437 Length : System.Address);
438 pragma Import (C, Local_Get_Current_Dir, "__gnat_get_current_dir");
441 Local_Get_Current_Dir (Buffer'Address, Path_Len'Address);
444 Cur : constant String := Normalize_Pathname (Buffer (1 .. Path_Len));
447 if Cur'Length > 1 and then Cur (Cur'Last) = Dir_Separator then
448 return Cur (1 .. Cur'Last - 1);
453 end Current_Directory;
455 ----------------------
456 -- Delete_Directory --
457 ----------------------
459 procedure Delete_Directory (Directory : String) is
461 -- First, the invalid cases
463 if not Is_Valid_Path_Name (Directory) then
464 raise Name_Error with
465 "invalid directory path name """ & Directory & '"';
467 elsif not Is_Directory (Directory) then
468 raise Name_Error with '"' & Directory & """ not a directory";
472 C_Dir_Name : constant String := Directory & ASCII.NUL;
475 if rmdir (C_Dir_Name) /= 0 then
477 "deletion of directory """ & Directory & """ failed";
481 end Delete_Directory;
487 procedure Delete_File (Name : String) is
491 -- First, the invalid cases
493 if not Is_Valid_Path_Name (Name) then
494 raise Name_Error with "invalid path name """ & Name & '"';
496 elsif not Is_Regular_File (Name) then
497 raise Name_Error with "file
""" & Name & """ does
not exist
";
500 -- The implementation uses System.OS_Lib.Delete_File
502 Delete_File (Name, Success);
505 raise Use_Error with "file
""" & Name & """ could
not be deleted
";
514 procedure Delete_Tree (Directory : String) is
515 Current_Dir : constant String := Current_Directory;
516 Search : Search_Type;
517 Dir_Ent : Directory_Entry_Type;
519 -- First, the invalid cases
521 if not Is_Valid_Path_Name (Directory) then
522 raise Name_Error with
523 "invalid directory path name
""" & Directory & '"';
525 elsif not Is_Directory (Directory) then
526 raise Name_Error with '"' & Directory & """ not a directory
";
529 Set_Directory (Directory);
530 Start_Search (Search, Directory => ".", Pattern => "");
532 while More_Entries (Search) loop
533 Get_Next_Entry (Search, Dir_Ent);
536 File_Name : constant String := Simple_Name (Dir_Ent);
539 if System.OS_Lib.Is_Directory (File_Name) then
540 if File_Name /= "." and then File_Name /= ".." then
541 Delete_Tree (File_Name);
545 Delete_File (File_Name);
550 Set_Directory (Current_Dir);
554 C_Dir_Name : constant String := Directory & ASCII.NUL;
557 if rmdir (C_Dir_Name) /= 0 then
559 "directory tree rooted
at """ &
560 Directory & """ could
not be deleted
";
570 function Exists (Name : String) return Boolean is
572 -- First, the invalid case
574 if not Is_Valid_Path_Name (Name) then
575 raise Name_Error with "invalid path name
""" & Name & '"';
578 -- The implementation is in File_Exists
580 return File_Exists (Name);
588 function Extension (Name : String) return String is
590 -- First, the invalid case
592 if not Is_Valid_Path_Name (Name) then
593 raise Name_Error with "invalid path name """ & Name & '"';
596 -- Look for first dot that is not followed by a directory separator
598 for Pos in reverse Name'Range loop
600 -- If a directory separator is found before a dot, there is no
603 if Name (Pos) = Dir_Separator then
606 elsif Name (Pos) = '.' then
608 -- We found a dot, build the return value with lower bound 1
611 subtype Result_Type is String (1 .. Name'Last - Pos);
613 return Result_Type (Name (Pos + 1 .. Name'Last));
618 -- No dot were found, there is no extension
624 ----------------------
625 -- Fetch_Next_Entry --
626 ----------------------
628 procedure Fetch_Next_Entry (Search : Search_Type) is
629 Name : String (1 .. 255);
632 Kind : File_Kind := Ordinary_File;
633 -- Initialized to avoid a compilation warning
635 Filename_Addr : System.Address;
636 Filename_Len : aliased Integer;
638 Buffer : array (0 .. Filename_Max + 12) of Character;
639 -- 12 is the size of the dirent structure (see dirent.h), without the
640 -- field for the filename.
642 function readdir_gnat
643 (Directory : System.Address;
644 Buffer : System.Address;
645 Last : not null access Integer) return System.Address;
646 pragma Import (C, readdir_gnat, "__gnat_readdir
");
651 -- Search.Value.Is_Valid is always True when Fetch_Next_Entry is called
656 (System.Address (Search.Value.Dir),
658 Filename_Len'Access);
660 -- If no matching entry is found, set Is_Valid to False
662 if Filename_Addr = System.Null_Address then
663 Search.Value.Is_Valid := False;
668 subtype Path_String is String (1 .. Filename_Len);
669 type Path_String_Access is access Path_String;
671 function Address_To_Access is new
672 Ada.Unchecked_Conversion
674 Target => Path_String_Access);
676 Path_Access : constant Path_String_Access :=
677 Address_To_Access (Filename_Addr);
680 Last := Filename_Len;
681 Name (1 .. Last) := Path_Access.all;
684 -- Check if the entry matches the pattern
686 if Match (Name (1 .. Last), Search.Value.Pattern) then
688 Full_Name : constant String :=
691 (Search.Value.Name), Name (1 .. Last));
692 Found : Boolean := False;
695 if File_Exists (Full_Name) then
697 -- Now check if the file kind matches the filter
699 if Is_Regular_File (Full_Name) then
700 if Search.Value.Filter (Ordinary_File) then
701 Kind := Ordinary_File;
705 elsif Is_Directory (Full_Name) then
706 if Search.Value.Filter (Directory) then
711 elsif Search.Value.Filter (Special_File) then
712 Kind := Special_File;
716 -- If it does, update Search and return
719 Search.Value.Entry_Fetched := True;
720 Search.Value.Dir_Entry :=
722 Simple => To_Unbounded_String (Name (1 .. Last)),
723 Full => To_Unbounded_String (Full_Name),
731 end Fetch_Next_Entry;
737 function File_Exists (Name : String) return Boolean is
738 function C_File_Exists (A : System.Address) return Integer;
739 pragma Import (C, C_File_Exists, "__gnat_file_exists
");
741 C_Name : String (1 .. Name'Length + 1);
744 C_Name (1 .. Name'Length) := Name;
745 C_Name (C_Name'Last) := ASCII.NUL;
746 return C_File_Exists (C_Name (1)'Address) = 1;
753 procedure Finalize (Search : in out Search_Type) is
755 if Search.Value /= null then
757 -- Close the directory, if one is open
759 if Search.Value.Dir /= No_Dir then
760 Close (Search.Value.Dir);
771 function Full_Name (Name : String) return String is
773 -- First, the invalid case
775 if not Is_Valid_Path_Name (Name) then
776 raise Name_Error with "invalid path name
""" & Name & '"';
779 -- Build the return value with lower bound 1
781 -- Use System.OS_Lib.Normalize_Pathname
784 Value : constant String := Normalize_Pathname (Name);
785 subtype Result is String (1 .. Value'Length);
787 return Result (Value);
792 function Full_Name (Directory_Entry : Directory_Entry_Type) return String is
794 -- First, the invalid case
796 if not Directory_Entry.Is_Valid then
797 raise Status_Error with "invalid directory entry";
800 -- The value to return has already been computed
802 return To_String (Directory_Entry.Full);
810 procedure Get_Next_Entry
811 (Search : in out Search_Type;
812 Directory_Entry : out Directory_Entry_Type)
815 -- First, the invalid case
817 if Search.Value = null or else not Search.Value.Is_Valid then
818 raise Status_Error with "invalid search";
821 -- Fetch the next entry, if needed
823 if not Search.Value.Entry_Fetched then
824 Fetch_Next_Entry (Search);
827 -- It is an error if no valid entry is found
829 if not Search.Value.Is_Valid then
830 raise Status_Error with "no next entry";
833 -- Reset Entry_Fetched and return the entry
835 Search.Value.Entry_Fetched := False;
836 Directory_Entry := Search.Value.Dir_Entry;
844 function Kind (Name : String) return File_Kind is
846 -- First, the invalid case
848 if not File_Exists (Name) then
849 raise Name_Error with "file """ & Name & """ does not exist";
851 elsif Is_Regular_File (Name) then
852 return Ordinary_File;
854 elsif Is_Directory (Name) then
862 function Kind (Directory_Entry : Directory_Entry_Type) return File_Kind is
864 -- First, the invalid case
866 if not Directory_Entry.Is_Valid then
867 raise Status_Error with "invalid directory entry";
870 -- The value to return has already be computed
872 return Directory_Entry.Kind;
876 -----------------------
877 -- Modification_Time --
878 -----------------------
880 function Modification_Time (Name : String) return Time is
886 Minute : Minute_Type;
887 Second : Second_Type;
891 -- First, the invalid cases
893 if not (Is_Regular_File (Name) or else Is_Directory (Name)) then
894 raise Name_Error with '"' & Name & """ not a file
or directory
";
897 Date := File_Time_Stamp (Name);
899 -- Break down the time stamp into its constituents relative to GMT.
900 -- This version of Split does not recognize leap seconds or buffer
901 -- space for time zone processing.
903 GM_Split (Date, Year, Month, Day, Hour, Minute, Second);
905 -- On OpenVMS, the resulting time value must be in the local time
906 -- zone. Ada.Calendar.Time_Of is exactly what we need. Note that
907 -- in both cases, the sub seconds are set to zero (0.0) because the
908 -- time stamp does not store them in its value.
913 (Year, Month, Day, Seconds_Of (Hour, Minute, Second, 0.0));
915 -- On Unix and Windows, the result must be in GMT. Ada.Calendar.
916 -- Formatting.Time_Of with default time zone of zero (0) is the
917 -- routine of choice.
920 Result := Time_Of (Year, Month, Day, Hour, Minute, Second, 0.0);
925 end Modification_Time;
927 function Modification_Time
928 (Directory_Entry : Directory_Entry_Type) return Ada.Calendar.Time
931 -- First, the invalid case
933 if not Directory_Entry.Is_Valid then
934 raise Status_Error with "invalid directory
entry";
937 -- The value to return has already be computed
939 return Modification_Time (To_String (Directory_Entry.Full));
941 end Modification_Time;
947 function More_Entries (Search : Search_Type) return Boolean is
949 if Search.Value = null then
952 elsif Search.Value.Is_Valid then
954 -- Fetch the next entry, if needed
956 if not Search.Value.Entry_Fetched then
957 Fetch_Next_Entry (Search);
961 return Search.Value.Is_Valid;
968 procedure Rename (Old_Name, New_Name : String) is
972 -- First, the invalid cases
974 if not Is_Valid_Path_Name (Old_Name) then
975 raise Name_Error with "invalid old path name
""" & Old_Name & '"';
977 elsif not Is_Valid_Path_Name (New_Name) then
978 raise Name_Error with "invalid new path name """ & New_Name & '"';
980 elsif not Is_Regular_File (Old_Name)
981 and then not Is_Directory (Old_Name)
983 raise Name_Error with "old file
""" & Old_Name & """ does
not exist
";
985 elsif Is_Regular_File (New_Name) or else Is_Directory (New_Name) then
987 "new name
""" & New_Name
988 & """ designates a file that already exists
";
991 -- The implementation uses System.OS_Lib.Rename_File
993 Rename_File (Old_Name, New_Name, Success);
997 "file
""" & Old_Name & """ could
not be renamed
";
1007 (Directory : String;
1009 Filter : Filter_Type := (others => True);
1010 Process : not null access procedure
1011 (Directory_Entry : Directory_Entry_Type))
1014 Directory_Entry : Directory_Entry_Type;
1017 Start_Search (Srch, Directory, Pattern, Filter);
1019 while More_Entries (Srch) loop
1020 Get_Next_Entry (Srch, Directory_Entry);
1021 Process (Directory_Entry);
1031 procedure Set_Directory (Directory : String) is
1032 C_Dir_Name : constant String := Directory & ASCII.NUL;
1034 if not Is_Valid_Path_Name (Directory) then
1035 raise Name_Error with
1036 "invalid directory path name
& """ & Directory & '"';
1038 elsif not Is_Directory (Directory) then
1039 raise Name_Error with
1040 "directory """ & Directory & """ does not exist";
1042 elsif chdir (C_Dir_Name) /= 0 then
1043 raise Name_Error with
1044 "could not set to designated directory """ & Directory & '"';
1052 function Simple_Name (Name : String) return String is
1054 function Simple_Name_Internal (Path : String) return String;
1055 -- This function does the job
1057 --------------------------
1058 -- Simple_Name_Internal --
1059 --------------------------
1061 function Simple_Name_Internal (Path : String) return String is
1062 Cut_Start : Natural :=
1064 (Path, Dir_Seps, Going => Strings.Backward);
1068 -- Cut_Start pointS to the first simple name character
1070 Cut_Start := (if Cut_Start = 0 then Path'First else Cut_Start + 1);
1072 -- Cut_End point to the last simple name character
1074 Cut_End := Path'Last;
1076 Check_For_Standard_Dirs : declare
1077 BN : constant String := Path (Cut_Start .. Cut_End);
1078 Has_Drive_Letter : constant Boolean :=
1079 System.OS_Lib.Path_Separator /= ':';
1080 -- If Path separator is not ':' then we are on a DOS based OS
1081 -- where this character is used as a drive letter separator.
1084 if BN = "." or else BN = ".." then
1087 elsif Has_Drive_Letter
1088 and then BN'Length > 2
1089 and then Characters.Handling.Is_Letter (BN (BN'First))
1090 and then BN (BN'First + 1) = ':'
1092 -- We have a DOS drive letter prefix, remove it
1094 return BN (BN'First + 2 .. BN'Last);
1099 end Check_For_Standard_Dirs;
1100 end Simple_Name_Internal;
1102 -- Start of processing for Simple_Name
1105 -- First, the invalid case
1107 if not Is_Valid_Path_Name (Name) then
1108 raise Name_Error with "invalid path name
""" & Name & '"';
1111 -- Build the value to return with lower bound 1
1114 Value : constant String := Simple_Name_Internal (Name);
1115 subtype Result is String (1 .. Value'Length);
1117 return Result (Value);
1122 function Simple_Name
1123 (Directory_Entry : Directory_Entry_Type) return String is
1125 -- First, the invalid case
1127 if not Directory_Entry.Is_Valid then
1128 raise Status_Error with "invalid directory entry";
1131 -- The value to return has already be computed
1133 return To_String (Directory_Entry.Simple);
1141 function Size (Name : String) return File_Size is
1142 C_Name : String (1 .. Name'Length + 1);
1144 function C_Size (Name : System.Address) return Long_Integer;
1145 pragma Import (C, C_Size, "__gnat_named_file_length");
1148 -- First, the invalid case
1150 if not Is_Regular_File (Name) then
1151 raise Name_Error with "file """ & Name & """ does not exist";
1154 C_Name (1 .. Name'Length) := Name;
1155 C_Name (C_Name'Last) := ASCII.NUL;
1156 return File_Size (C_Size (C_Name'Address));
1160 function Size (Directory_Entry : Directory_Entry_Type) return File_Size is
1162 -- First, the invalid case
1164 if not Directory_Entry.Is_Valid then
1165 raise Status_Error with "invalid directory entry";
1168 -- The value to return has already be computed
1170 return Size (To_String (Directory_Entry.Full));
1178 procedure Start_Search
1179 (Search : in out Search_Type;
1182 Filter : Filter_Type := (others => True))
1184 function opendir (file_name : String) return DIRs;
1185 pragma Import (C, opendir, "__gnat_opendir");
1187 C_File_Name : constant String := Directory & ASCII.NUL;
1189 Dir : Dir_Type_Value;
1192 -- First, the invalid case Name_Error
1194 if not Is_Directory (Directory) then
1195 raise Name_Error with
1196 "unknown directory """ & Simple_Name (Directory) & '"';
1199 -- Check the pattern
1205 Case_Sensitive => Is_Path_Name_Case_Sensitive);
1207 when Error_In_Regexp =>
1208 Free (Search.Value);
1209 raise Name_Error with "invalid pattern
""" & Pattern & '"';
1212 Dir := Dir_Type_Value (opendir (C_File_Name));
1214 if Dir = No_Dir then
1215 raise Use_Error with
1216 "unreadable directory """ & Simple_Name (Directory) & '"';
1219 -- If needed, finalize Search
1223 -- Allocate the default data
1225 Search.Value := new Search_Data;
1227 -- Initialize some Search components
1229 Search.Value.Filter := Filter;
1230 Search.Value.Name := To_Unbounded_String (Full_Name (Directory));
1231 Search.Value.Pattern := Pat;
1232 Search.Value.Dir := Dir;
1233 Search.Value.Is_Valid := True;
1236 end Ada.Directories;