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-2012, 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
.Characters
.Handling
; use Ada
.Characters
.Handling
;
35 with Ada
.Directories
.Validity
; use Ada
.Directories
.Validity
;
36 with Ada
.Strings
.Fixed
;
37 with Ada
.Strings
.Maps
; use Ada
.Strings
.Maps
;
38 with Ada
.Strings
.Unbounded
; use Ada
.Strings
.Unbounded
;
39 with Ada
.Unchecked_Conversion
;
40 with Ada
.Unchecked_Deallocation
;
42 with System
; use System
;
43 with System
.CRTL
; use System
.CRTL
;
44 with System
.File_IO
; use System
.File_IO
;
45 with System
.OS_Constants
; use System
.OS_Constants
;
46 with System
.OS_Lib
; use System
.OS_Lib
;
47 with System
.Regexp
; use System
.Regexp
;
49 package body Ada
.Directories
is
51 Filename_Max
: constant Integer := 1024;
52 -- 1024 is the value of FILENAME_MAX in stdio.h
54 type Dir_Type_Value
is new Address
;
55 -- This is the low-level address directory structure as returned by the C
58 No_Dir
: constant Dir_Type_Value
:= Dir_Type_Value
(Null_Address
);
59 -- Null directory value
61 Dir_Separator
: constant Character;
62 pragma Import
(C
, Dir_Separator
, "__gnat_dir_separator");
63 -- Running system default directory separator
65 Dir_Seps
: constant Character_Set
:= Strings
.Maps
.To_Set
("/\");
66 -- UNIX and DOS style directory separators
69 pragma Import (C, Max_Path, "__gnat_max_path_len
");
70 -- The maximum length of a path
72 type Search_Data is record
73 Is_Valid : Boolean := False;
74 Name : Unbounded_String;
77 Dir : Dir_Type_Value := No_Dir;
78 Entry_Fetched : Boolean := False;
79 Dir_Entry : Directory_Entry_Type;
81 -- The current state of a search
83 Empty_String : constant String := (1 .. 0 => ASCII.NUL);
84 -- Empty string, returned by function Extension when there is no extension
86 procedure Free is new Ada.Unchecked_Deallocation (Search_Data, Search_Ptr);
88 procedure Close (Dir : Dir_Type_Value);
90 function File_Exists (Name : String) return Boolean;
91 -- Returns True if the named file exists
93 procedure Fetch_Next_Entry (Search : Search_Type);
94 -- Get the next entry in a directory, setting Entry_Fetched if successful
95 -- or resetting Is_Valid if not.
101 function Base_Name (Name : String) return String is
102 Simple : constant String := Simple_Name (Name);
103 -- Simple'First is guaranteed to be 1
106 -- Look for the last dot in the file name and return the part of the
107 -- file name preceding this last dot. If the first dot is the first
108 -- character of the file name, the base name is the empty string.
110 for Pos in reverse Simple'Range loop
111 if Simple (Pos) = '.' then
112 return Simple (1 .. Pos - 1);
116 -- If there is no dot, return the complete file name
125 procedure Close (Dir : Dir_Type_Value) is
127 pragma Warnings (Off, Discard);
129 function closedir (directory : DIRs) return Integer;
130 pragma Import (C, closedir, "__gnat_closedir
");
133 Discard := closedir (DIRs (Dir));
141 (Containing_Directory : String := "";
143 Extension : String := "") return String
145 Result : String (1 .. Containing_Directory'Length +
146 Name'Length + Extension'Length + 2);
150 -- First, deal with the invalid cases
152 if Containing_Directory /= ""
153 and then not Is_Valid_Path_Name (Containing_Directory)
155 raise Name_Error with
156 "invalid directory path name
""" & Containing_Directory & '"';
159 Extension'Length = 0 and then (not Is_Valid_Simple_Name (Name))
161 raise Name_Error with
162 "invalid simple name """ & Name & '"';
164 elsif Extension'Length /= 0
165 and then not Is_Valid_Simple_Name (Name & '.' & Extension)
167 raise Name_Error with
168 "invalid file name
""" & Name & '.' & Extension & '"';
170 -- This is not an invalid case so build the path name
173 Last := Containing_Directory'Length;
174 Result (1 .. Last) := Containing_Directory;
176 -- Add a directory separator if needed
178 if Last /= 0 and then not Is_In (Result (Last), Dir_Seps) then
180 Result (Last) := Dir_Separator;
185 Result (Last + 1 .. Last + Name'Length) := Name;
186 Last := Last + Name'Length;
188 -- If extension was specified, add dot followed by this extension
190 if Extension'Length /= 0 then
192 Result (Last) := '.';
193 Result (Last + 1 .. Last + Extension'Length) := Extension;
194 Last := Last + Extension'Length;
197 return Result (1 .. Last);
201 --------------------------
202 -- Containing_Directory --
203 --------------------------
205 function Containing_Directory (Name : String) return String is
207 -- First, the invalid case
209 if not Is_Valid_Path_Name (Name) then
210 raise Name_Error with "invalid path name """ & Name & '"';
214 -- We need to resolve links because of A.16(47), since we must not
215 -- return alternative names for files.
217 Norm : constant String := Normalize_Pathname (Name);
218 Last_DS : constant Natural :=
220 (Name, Dir_Seps, Going => Strings.Backward);
225 -- There is no directory separator, returns current working
228 return Current_Directory;
230 -- If Name indicates a root directory, raise Use_Error, because
231 -- it has no containing directory.
240 and then Norm
(Norm
'Last - 1 .. Norm
'Last) = ":\"
241 and then (Norm (Norm'First) in 'a' .. 'z'
243 Norm (Norm'First) in 'A' .. 'Z'))))
246 "directory
""" & Name & """ has no containing directory
";
250 Last : Positive := Last_DS - Name'First + 1;
251 Result : String (1 .. Last);
254 Result := Name (Name'First .. Last_DS);
256 -- Remove any trailing directory separator, except as the
257 -- first character or the first character following a drive
258 -- number on Windows.
264 Result (Last) /= Directory_Separator;
268 and then Result (2) = ':'
270 (Result (1) in 'A' .. 'Z'
272 Result (1) in 'a' .. 'z');
277 -- Special case of current directory, identified by "."
279 if Last = 1 and then Result (1) = '.' then
280 return Current_Directory;
282 -- Special case of "..": the current directory may be a root
285 elsif Last = 2 and then Result (1 .. 2) = ".." then
286 return Containing_Directory (Current_Directory);
289 return Result (1 .. Last);
295 end Containing_Directory;
302 (Source_Name : String;
303 Target_Name : String;
307 Mode : Copy_Mode := Overwrite;
308 Preserve : Attribute := None;
311 -- First, the invalid cases
313 if not Is_Valid_Path_Name (Source_Name) then
314 raise Name_Error with
315 "invalid source path name
""" & Source_Name & '"';
317 elsif not Is_Valid_Path_Name (Target_Name) then
318 raise Name_Error with
319 "invalid target path name """ & Target_Name & '"';
321 elsif not Is_Regular_File (Source_Name) then
322 raise Name_Error with '"' & Source_Name & """ is not a file";
324 elsif Is_Directory (Target_Name) then
325 raise Use_Error with "target """ & Target_Name & """ is a directory";
328 if Form'Length > 0 then
330 Formstr : String (1 .. Form'Length + 1);
334 -- Acquire form string, setting required NUL terminator
336 Formstr (1 .. Form'Length) := Form;
337 Formstr (Formstr'Last) := ASCII.NUL;
339 -- Convert form string to lower case
341 for J in Formstr'Range loop
342 if Formstr (J) in 'A
' .. 'Z
' then
344 Character'Val (Character'Pos (Formstr (J)) + 32);
350 Form_Parameter (Formstr, "mode", V1, V2);
354 elsif Formstr (V1 .. V2) = "copy" then
356 elsif Formstr (V1 .. V2) = "overwrite" then
358 elsif Formstr (V1 .. V2) = "append" then
361 raise Use_Error with "invalid Form";
364 Form_Parameter (Formstr, "preserve", V1, V2);
368 elsif Formstr (V1 .. V2) = "timestamps" then
369 Preserve := Time_Stamps;
370 elsif Formstr (V1 .. V2) = "all_attributes" then
372 elsif Formstr (V1 .. V2) = "no_attributes" then
375 raise Use_Error with "invalid Form";
380 -- Do actual copy using System.OS_Lib.Copy_File
382 Copy_File (Source_Name, Target_Name, Success, Mode, Preserve);
385 raise Use_Error with "copy of """ & Source_Name & """ failed";
390 ----------------------
391 -- Create_Directory --
392 ----------------------
394 procedure Create_Directory
395 (New_Directory : String;
398 C_Dir_Name : constant String := New_Directory & ASCII.NUL;
401 -- First, the invalid case
403 if not Is_Valid_Path_Name (New_Directory) then
404 raise Name_Error with
405 "invalid new directory path name """ & New_Directory & '"';
408 -- Acquire setting of encoding parameter
411 Formstr : constant String := To_Lower (Form);
413 Encoding : CRTL.Filename_Encoding;
414 -- Filename encoding specified into the form parameter
419 Form_Parameter (Formstr, "encoding
", V1, V2);
422 Encoding := CRTL.Unspecified;
423 elsif Formstr (V1 .. V2) = "utf8
" then
424 Encoding := CRTL.UTF8;
425 elsif Formstr (V1 .. V2) = "8bits
" then
426 Encoding := CRTL.ASCII_8bits;
428 raise Use_Error with "invalid Form
";
431 if CRTL.mkdir (C_Dir_Name, Encoding) /= 0 then
433 "creation
of new directory
""" & New_Directory & """ failed
";
437 end Create_Directory;
443 procedure Create_Path
444 (New_Directory : String;
447 New_Dir : String (1 .. New_Directory'Length + 1);
448 Last : Positive := 1;
449 Start : Positive := 1;
452 -- First, the invalid case
454 if not Is_Valid_Path_Name (New_Directory) then
455 raise Name_Error with
456 "invalid
new directory path name
""" & New_Directory & '"';
459 -- Build New_Dir with a directory separator at the end, so that the
460 -- complete path will be found in the loop below.
462 New_Dir (1 .. New_Directory'Length) := New_Directory;
463 New_Dir (New_Dir'Last) := Directory_Separator;
465 -- If host is windows, and the first two characters are directory
466 -- separators, we have an UNC path. Skip it.
468 if Directory_Separator = '\
'
469 and then New_Dir'Length > 2
470 and then Is_In (New_Dir (1), Dir_Seps)
471 and then Is_In (New_Dir (2), Dir_Seps)
476 exit when Start = New_Dir'Last
477 or else Is_In (New_Dir (Start), Dir_Seps);
481 -- Create, if necessary, each directory in the path
483 for J in Start + 1 .. New_Dir'Last loop
485 -- Look for the end of an intermediate directory
487 if not Is_In (New_Dir (J), Dir_Seps) then
490 -- We have found a new intermediate directory each time we find
491 -- a first directory separator.
493 elsif not Is_In (New_Dir (J - 1), Dir_Seps) then
495 -- No need to create the directory if it already exists
497 if Is_Directory (New_Dir (1 .. Last)) then
500 -- It is an error if a file with such a name already exists
502 elsif Is_Regular_File (New_Dir (1 .. Last)) then
504 "file """ & New_Dir (1 .. Last) & """ already exists";
508 (New_Directory => New_Dir (1 .. Last), Form => Form);
515 -----------------------
516 -- Current_Directory --
517 -----------------------
519 function Current_Directory return String is
520 Path_Len : Natural := Max_Path;
521 Buffer : String (1 .. 1 + Max_Path + 1);
523 procedure Local_Get_Current_Dir (Dir : Address; Length : Address);
524 pragma Import (C, Local_Get_Current_Dir, "__gnat_get_current_dir");
527 Local_Get_Current_Dir (Buffer'Address, Path_Len'Address);
529 -- We need to resolve links because of RM A.16(47), which requires
530 -- that we not return alternative names for files.
532 return Normalize_Pathname (Buffer (1 .. Path_Len));
533 end Current_Directory;
535 ----------------------
536 -- Delete_Directory --
537 ----------------------
539 procedure Delete_Directory (Directory : String) is
541 -- First, the invalid cases
543 if not Is_Valid_Path_Name (Directory) then
544 raise Name_Error with
545 "invalid directory path name """ & Directory & '"';
547 elsif not Is_Directory (Directory) then
548 raise Name_Error with '"' & Directory & """ not a directory";
550 -- Do the deletion, checking for error
554 C_Dir_Name : constant String := Directory & ASCII.NUL;
556 if rmdir (C_Dir_Name) /= 0 then
558 "deletion of directory """ & Directory & """ failed";
562 end Delete_Directory;
568 procedure Delete_File (Name : String) is
572 -- First, the invalid cases
574 if not Is_Valid_Path_Name (Name) then
575 raise Name_Error with "invalid path name """ & Name & '"';
577 elsif not Is_Regular_File (Name)
578 and then not Is_Symbolic_Link (Name)
580 raise Name_Error with "file
""" & Name & """ does
not exist
";
583 -- Do actual deletion using System.OS_Lib.Delete_File
585 Delete_File (Name, Success);
588 raise Use_Error with "file
""" & Name & """ could
not be deleted
";
597 procedure Delete_Tree (Directory : String) is
598 Current_Dir : constant String := Current_Directory;
599 Search : Search_Type;
600 Dir_Ent : Directory_Entry_Type;
602 -- First, the invalid cases
604 if not Is_Valid_Path_Name (Directory) then
605 raise Name_Error with
606 "invalid directory path name
""" & Directory & '"';
608 elsif not Is_Directory (Directory) then
609 raise Name_Error with '"' & Directory & """ not a directory
";
612 Set_Directory (Directory);
614 Start_Search (Search, Directory => ".", Pattern => "");
615 while More_Entries (Search) loop
616 Get_Next_Entry (Search, Dir_Ent);
619 File_Name : constant String := Simple_Name (Dir_Ent);
622 if OS_Lib.Is_Directory (File_Name) then
623 if File_Name /= "." and then File_Name /= ".." then
624 Delete_Tree (File_Name);
628 Delete_File (File_Name);
633 Set_Directory (Current_Dir);
637 C_Dir_Name : constant String := Directory & ASCII.NUL;
640 if rmdir (C_Dir_Name) /= 0 then
642 "directory tree rooted
at """ &
643 Directory & """ could
not be deleted
";
653 function Exists (Name : String) return Boolean is
655 -- First, the invalid case
657 if not Is_Valid_Path_Name (Name) then
658 raise Name_Error with "invalid path name
""" & Name & '"';
661 -- The implementation is in File_Exists
663 return File_Exists (Name);
671 function Extension (Name : String) return String is
673 -- First, the invalid case
675 if not Is_Valid_Path_Name (Name) then
676 raise Name_Error with "invalid path name """ & Name & '"';
679 -- Look for first dot that is not followed by a directory separator
681 for Pos in reverse Name'Range loop
683 -- If a directory separator is found before a dot, there is no
686 if Is_In (Name (Pos), Dir_Seps) then
689 elsif Name (Pos) = '.' then
691 -- We found a dot, build the return value with lower bound 1
694 subtype Result_Type is String (1 .. Name'Last - Pos);
696 return Result_Type (Name (Pos + 1 .. Name'Last));
701 -- No dot were found, there is no extension
707 ----------------------
708 -- Fetch_Next_Entry --
709 ----------------------
711 procedure Fetch_Next_Entry (Search : Search_Type) is
712 Name : String (1 .. 255);
715 Kind : File_Kind := Ordinary_File;
716 -- Initialized to avoid a compilation warning
718 Filename_Addr : Address;
719 Filename_Len : aliased Integer;
721 Buffer : array (0 .. Filename_Max + 12) of Character;
722 -- 12 is the size of the dirent structure (see dirent.h), without the
723 -- field for the filename.
725 function readdir_gnat
726 (Directory : Address;
728 Last : not null access Integer) return Address;
729 pragma Import (C, readdir_gnat, "__gnat_readdir
");
732 -- Search.Value.Is_Valid is always True when Fetch_Next_Entry is called
737 (Address (Search.Value.Dir),
739 Filename_Len'Access);
741 -- If no matching entry is found, set Is_Valid to False
743 if Filename_Addr = Null_Address then
744 Search.Value.Is_Valid := False;
749 subtype Path_String is String (1 .. Filename_Len);
750 type Path_String_Access is access Path_String;
752 function Address_To_Access is new
753 Ada.Unchecked_Conversion
755 Target => Path_String_Access);
757 Path_Access : constant Path_String_Access :=
758 Address_To_Access (Filename_Addr);
761 Last := Filename_Len;
762 Name (1 .. Last) := Path_Access.all;
765 -- Check if the entry matches the pattern
767 if Match (Name (1 .. Last), Search.Value.Pattern) then
769 Full_Name : constant String :=
772 (Search.Value.Name), Name (1 .. Last));
773 Found : Boolean := False;
776 if File_Exists (Full_Name) then
778 -- Now check if the file kind matches the filter
780 if Is_Regular_File (Full_Name) then
781 if Search.Value.Filter (Ordinary_File) then
782 Kind := Ordinary_File;
786 elsif Is_Directory (Full_Name) then
787 if Search.Value.Filter (Directory) then
792 elsif Search.Value.Filter (Special_File) then
793 Kind := Special_File;
797 -- If it does, update Search and return
800 Search.Value.Entry_Fetched := True;
801 Search.Value.Dir_Entry :=
803 Simple => To_Unbounded_String (Name (1 .. Last)),
804 Full => To_Unbounded_String (Full_Name),
812 end Fetch_Next_Entry;
818 function File_Exists (Name : String) return Boolean is
819 function C_File_Exists (A : Address) return Integer;
820 pragma Import (C, C_File_Exists, "__gnat_file_exists
");
822 C_Name : String (1 .. Name'Length + 1);
825 C_Name (1 .. Name'Length) := Name;
826 C_Name (C_Name'Last) := ASCII.NUL;
827 return C_File_Exists (C_Name (1)'Address) = 1;
834 procedure Finalize (Search : in out Search_Type) is
836 if Search.Value /= null then
838 -- Close the directory, if one is open
840 if Search.Value.Dir /= No_Dir then
841 Close (Search.Value.Dir);
852 function Full_Name (Name : String) return String is
854 -- First, the invalid case
856 if not Is_Valid_Path_Name (Name) then
857 raise Name_Error with "invalid path name
""" & Name & '"';
860 -- Build the return value with lower bound 1
862 -- Use System.OS_Lib.Normalize_Pathname
865 -- We need to resolve links because of (RM A.16(47)), which says
866 -- we must not return alternative names for files.
868 Value : constant String := Normalize_Pathname (Name);
869 subtype Result is String (1 .. Value'Length);
872 return Result (Value);
877 function Full_Name (Directory_Entry : Directory_Entry_Type) return String is
879 -- First, the invalid case
881 if not Directory_Entry.Is_Valid then
882 raise Status_Error with "invalid directory entry";
885 -- The value to return has already been computed
887 return To_String (Directory_Entry.Full);
895 procedure Get_Next_Entry
896 (Search : in out Search_Type;
897 Directory_Entry : out Directory_Entry_Type)
900 -- First, the invalid case
902 if Search.Value = null or else not Search.Value.Is_Valid then
903 raise Status_Error with "invalid search";
906 -- Fetch the next entry, if needed
908 if not Search.Value.Entry_Fetched then
909 Fetch_Next_Entry (Search);
912 -- It is an error if no valid entry is found
914 if not Search.Value.Is_Valid then
915 raise Status_Error with "no next entry";
918 -- Reset Entry_Fetched and return the entry
920 Search.Value.Entry_Fetched := False;
921 Directory_Entry := Search.Value.Dir_Entry;
929 function Kind (Name : String) return File_Kind is
931 -- First, the invalid case
933 if not File_Exists (Name) then
934 raise Name_Error with "file """ & Name & """ does not exist";
936 -- If OK, return appropriate kind
938 elsif Is_Regular_File (Name) then
939 return Ordinary_File;
941 elsif Is_Directory (Name) then
949 function Kind (Directory_Entry : Directory_Entry_Type) return File_Kind is
951 -- First, the invalid case
953 if not Directory_Entry.Is_Valid then
954 raise Status_Error with "invalid directory entry";
957 -- The value to return has already be computed
959 return Directory_Entry.Kind;
963 -----------------------
964 -- Modification_Time --
965 -----------------------
967 function Modification_Time (Name : String) return Time is
973 Minute : Minute_Type;
974 Second : Second_Type;
978 -- First, the invalid cases
980 if not (Is_Regular_File (Name) or else Is_Directory (Name)) then
981 raise Name_Error with '"' & Name & """ not a file
or directory
";
984 Date := File_Time_Stamp (Name);
986 -- Break down the time stamp into its constituents relative to GMT.
987 -- This version of Split does not recognize leap seconds or buffer
988 -- space for time zone processing.
990 GM_Split (Date, Year, Month, Day, Hour, Minute, Second);
992 -- On OpenVMS, the resulting time value must be in the local time
993 -- zone. Ada.Calendar.Time_Of is exactly what we need. Note that
994 -- in both cases, the sub seconds are set to zero (0.0) because the
995 -- time stamp does not store them in its value.
1000 (Year, Month, Day, Seconds_Of (Hour, Minute, Second, 0.0));
1002 -- On Unix and Windows, the result must be in GMT. Ada.Calendar.
1003 -- Formatting.Time_Of with default time zone of zero (0) is the
1004 -- routine of choice.
1007 Result := Time_Of (Year, Month, Day, Hour, Minute, Second, 0.0);
1012 end Modification_Time;
1014 function Modification_Time
1015 (Directory_Entry : Directory_Entry_Type) return Ada.Calendar.Time
1018 -- First, the invalid case
1020 if not Directory_Entry.Is_Valid then
1021 raise Status_Error with "invalid directory
entry";
1024 -- The value to return has already be computed
1026 return Modification_Time (To_String (Directory_Entry.Full));
1028 end Modification_Time;
1034 function More_Entries (Search : Search_Type) return Boolean is
1036 if Search.Value = null then
1039 elsif Search.Value.Is_Valid then
1041 -- Fetch the next entry, if needed
1043 if not Search.Value.Entry_Fetched then
1044 Fetch_Next_Entry (Search);
1048 return Search.Value.Is_Valid;
1055 procedure Rename (Old_Name, New_Name : String) is
1059 -- First, the invalid cases
1061 if not Is_Valid_Path_Name (Old_Name) then
1062 raise Name_Error with "invalid old path name
""" & Old_Name & '"';
1064 elsif not Is_Valid_Path_Name (New_Name) then
1065 raise Name_Error with "invalid new path name """ & New_Name & '"';
1067 elsif not Is_Regular_File (Old_Name)
1068 and then not Is_Directory (Old_Name)
1070 raise Name_Error with "old file
""" & Old_Name & """ does
not exist
";
1072 elsif Is_Regular_File (New_Name) or else Is_Directory (New_Name) then
1073 raise Use_Error with
1074 "new name
""" & New_Name
1075 & """ designates a file that already exists
";
1077 -- Do actual rename using System.OS_Lib.Rename_File
1080 Rename_File (Old_Name, New_Name, Success);
1084 -- AI05-0231-1: Name_Error should be raised in case a directory
1085 -- component of New_Name does not exist (as in New_Name =>
1086 -- "/no
-such
-dir
/new-filename
"). ENOENT indicates that. ENOENT
1087 -- also indicate that the Old_Name does not exist, but we already
1088 -- checked for that above. All other errors are Use_Error.
1090 if Errno = ENOENT then
1091 raise Name_Error with
1092 "file
""" & Containing_Directory (New_Name) & """ not found
";
1095 raise Use_Error with
1096 "file
""" & Old_Name & """ could
not be renamed
";
1107 (Directory : String;
1109 Filter : Filter_Type := (others => True);
1110 Process : not null access procedure
1111 (Directory_Entry : Directory_Entry_Type))
1114 Directory_Entry : Directory_Entry_Type;
1117 Start_Search (Srch, Directory, Pattern, Filter);
1118 while More_Entries (Srch) loop
1119 Get_Next_Entry (Srch, Directory_Entry);
1120 Process (Directory_Entry);
1130 procedure Set_Directory (Directory : String) is
1131 C_Dir_Name : constant String := Directory & ASCII.NUL;
1133 if not Is_Valid_Path_Name (Directory) then
1134 raise Name_Error with
1135 "invalid directory path name
& """ & Directory & '"';
1137 elsif not Is_Directory (Directory) then
1138 raise Name_Error with
1139 "directory """ & Directory & """ does not exist";
1141 elsif chdir (C_Dir_Name) /= 0 then
1142 raise Name_Error with
1143 "could not set to designated directory """ & Directory & '"';
1151 function Simple_Name (Name : String) return String is
1153 function Simple_Name_Internal (Path : String) return String;
1154 -- This function does the job
1156 --------------------------
1157 -- Simple_Name_Internal --
1158 --------------------------
1160 function Simple_Name_Internal (Path : String) return String is
1161 Cut_Start : Natural :=
1163 (Path, Dir_Seps, Going => Strings.Backward);
1167 -- Cut_Start pointS to the first simple name character
1169 Cut_Start := (if Cut_Start = 0 then Path'First else Cut_Start + 1);
1171 -- Cut_End point to the last simple name character
1173 Cut_End := Path'Last;
1175 Check_For_Standard_Dirs : declare
1176 BN : constant String := Path (Cut_Start .. Cut_End);
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.
1184 if BN = "." or else BN = ".." then
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) = ':'
1192 -- We have a DOS drive letter prefix, remove it
1194 return BN (BN'First + 2 .. BN'Last);
1199 end Check_For_Standard_Dirs;
1200 end Simple_Name_Internal;
1202 -- Start of processing for Simple_Name
1205 -- First, the invalid case
1207 if not Is_Valid_Path_Name (Name) then
1208 raise Name_Error with "invalid path name
""" & Name & '"';
1211 -- Build the value to return with lower bound 1
1214 Value : constant String := Simple_Name_Internal (Name);
1215 subtype Result is String (1 .. Value'Length);
1217 return Result (Value);
1222 function Simple_Name
1223 (Directory_Entry : Directory_Entry_Type) return String is
1225 -- First, the invalid case
1227 if not Directory_Entry.Is_Valid then
1228 raise Status_Error with "invalid directory entry";
1231 -- The value to return has already be computed
1233 return To_String (Directory_Entry.Simple);
1241 function Size (Name : String) return File_Size is
1242 C_Name : String (1 .. Name'Length + 1);
1244 function C_Size (Name : Address) return Long_Integer;
1245 pragma Import (C, C_Size, "__gnat_named_file_length");
1248 -- First, the invalid case
1250 if not Is_Regular_File (Name) then
1251 raise Name_Error with "file """ & Name & """ does not exist";
1254 C_Name (1 .. Name'Length) := Name;
1255 C_Name (C_Name'Last) := ASCII.NUL;
1256 return File_Size (C_Size (C_Name'Address));
1260 function Size (Directory_Entry : Directory_Entry_Type) return File_Size is
1262 -- First, the invalid case
1264 if not Directory_Entry.Is_Valid then
1265 raise Status_Error with "invalid directory entry";
1268 -- The value to return has already be computed
1270 return Size (To_String (Directory_Entry.Full));
1278 procedure Start_Search
1279 (Search : in out Search_Type;
1282 Filter : Filter_Type := (others => True))
1284 function opendir (file_name : String) return DIRs;
1285 pragma Import (C, opendir, "__gnat_opendir");
1287 C_File_Name : constant String := Directory & ASCII.NUL;
1289 Dir : Dir_Type_Value;
1292 -- First, the invalid case Name_Error
1294 if not Is_Directory (Directory) then
1295 raise Name_Error with
1296 "unknown directory """ & Simple_Name (Directory) & '"';
1299 -- Check the pattern
1305 Case_Sensitive => Is_Path_Name_Case_Sensitive);
1307 when Error_In_Regexp =>
1308 Free (Search.Value);
1309 raise Name_Error with "invalid pattern
""" & Pattern & '"';
1312 Dir := Dir_Type_Value (opendir (C_File_Name));
1314 if Dir = No_Dir then
1315 raise Use_Error with
1316 "unreadable directory """ & Simple_Name (Directory) & '"';
1319 -- If needed, finalize Search
1323 -- Allocate the default data
1325 Search.Value := new Search_Data;
1327 -- Initialize some Search components
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;
1336 end Ada.Directories;