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-2017, 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_Deallocation
;
41 with System
; use System
;
42 with System
.CRTL
; use System
.CRTL
;
43 with System
.File_Attributes
; use System
.File_Attributes
;
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 type Dir_Type_Value
is new Address
;
52 -- This is the low-level address directory structure as returned by the C
55 No_Dir
: constant Dir_Type_Value
:= Dir_Type_Value
(Null_Address
);
56 -- Null directory value
58 Dir_Separator
: constant Character;
59 pragma Import
(C
, Dir_Separator
, "__gnat_dir_separator");
60 -- Running system default directory separator
62 Dir_Seps
: constant Character_Set
:= Strings
.Maps
.To_Set
("/\");
63 -- UNIX and DOS style directory separators
66 pragma Import (C, Max_Path, "__gnat_max_path_len
");
67 -- The maximum length of a path
69 type Search_Data is record
70 Is_Valid : Boolean := False;
71 Name : Unbounded_String;
74 Dir : Dir_Type_Value := No_Dir;
75 Entry_Fetched : Boolean := False;
76 Dir_Entry : Directory_Entry_Type;
78 -- The current state of a search
80 Empty_String : constant String := (1 .. 0 => ASCII.NUL);
81 -- Empty string, returned by function Extension when there is no extension
83 procedure Free is new Ada.Unchecked_Deallocation (Search_Data, Search_Ptr);
85 procedure Close (Dir : Dir_Type_Value);
87 function File_Exists (Name : String) return Boolean;
88 -- Returns True if the named file exists
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.
98 function Base_Name (Name : String) return String is
99 Simple : constant String := Simple_Name (Name);
100 -- Simple'First is guaranteed to be 1
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.
107 for Pos in reverse Simple'Range loop
108 if Simple (Pos) = '.' then
109 return Simple (1 .. Pos - 1);
113 -- If there is no dot, return the complete file name
122 procedure Close (Dir : Dir_Type_Value) is
124 pragma Warnings (Off, Discard);
126 function closedir (directory : DIRs) return Integer;
127 pragma Import (C, closedir, "__gnat_closedir
");
130 Discard := closedir (DIRs (Dir));
138 (Containing_Directory : String := "";
140 Extension : String := "") return String
142 Result : String (1 .. Containing_Directory'Length +
143 Name'Length + Extension'Length + 2);
147 -- First, deal with the invalid cases
149 if Containing_Directory /= ""
150 and then not Is_Valid_Path_Name (Containing_Directory)
152 raise Name_Error with
153 "invalid directory path name
""" & Containing_Directory & '"';
156 Extension'Length = 0 and then (not Is_Valid_Simple_Name (Name))
158 raise Name_Error with
159 "invalid simple name """ & Name & '"';
161 elsif Extension'Length /= 0
162 and then not Is_Valid_Simple_Name (Name & '.' & Extension)
164 raise Name_Error with
165 "invalid file name
""" & Name & '.' & Extension & '"';
167 -- This is not an invalid case so build the path name
170 Last := Containing_Directory'Length;
171 Result (1 .. Last) := Containing_Directory;
173 -- Add a directory separator if needed
175 if Last /= 0 and then not Is_In (Result (Last), Dir_Seps) then
177 Result (Last) := Dir_Separator;
182 Result (Last + 1 .. Last + Name'Length) := Name;
183 Last := Last + Name'Length;
185 -- If extension was specified, add dot followed by this extension
187 if Extension'Length /= 0 then
189 Result (Last) := '.';
190 Result (Last + 1 .. Last + Extension'Length) := Extension;
191 Last := Last + Extension'Length;
194 return Result (1 .. Last);
198 --------------------------
199 -- Containing_Directory --
200 --------------------------
202 function Containing_Directory (Name : String) return String is
204 -- First, the invalid case
206 if not Is_Valid_Path_Name (Name) then
207 raise Name_Error with "invalid path name """ & Name & '"';
211 Last_DS : constant Natural :=
212 Strings.Fixed.Index (Name, Dir_Seps, Going => Strings.Backward);
217 -- There is no directory separator, returns "." representing
218 -- the current working directory.
222 -- If Name indicates a root directory, raise Use_Error, because
223 -- it has no containing directory.
232 and then Name
(Name
'Last - 1 .. Name
'Last) = ":\"
233 and then (Name (Name'First) in 'a' .. 'z'
235 Name (Name'First) in 'A' .. 'Z'))))
238 "directory
""" & Name & """ has no containing directory
";
242 Last : Positive := Last_DS - Name'First + 1;
243 Result : String (1 .. Last);
246 Result := Name (Name'First .. Last_DS);
248 -- Remove any trailing directory separator, except as the
249 -- first character or the first character following a drive
250 -- number on Windows.
256 Result (Last) /= Directory_Separator;
260 and then Result (2) = ':'
262 (Result (1) in 'A' .. 'Z'
264 Result (1) in 'a' .. 'z');
269 -- Special case of "..": the current directory may be a root
272 if Last = 2 and then Result (1 .. 2) = ".." then
273 return Containing_Directory (Current_Directory);
276 return Result (1 .. Last);
282 end Containing_Directory;
289 (Source_Name : String;
290 Target_Name : String;
294 Mode : Copy_Mode := Overwrite;
295 Preserve : Attribute := None;
298 -- First, the invalid cases
300 if not Is_Valid_Path_Name (Source_Name) then
301 raise Name_Error with
302 "invalid source path name
""" & Source_Name & '"';
304 elsif not Is_Valid_Path_Name (Target_Name) then
305 raise Name_Error with
306 "invalid target path name """ & Target_Name & '"';
308 elsif not Is_Regular_File (Source_Name) then
309 raise Name_Error with '"' & Source_Name & """ is not a file";
311 elsif Is_Directory (Target_Name) then
312 raise Use_Error with "target """ & Target_Name & """ is a directory";
315 if Form'Length > 0 then
317 Formstr : String (1 .. Form'Length + 1);
321 -- Acquire form string, setting required NUL terminator
323 Formstr (1 .. Form'Length) := Form;
324 Formstr (Formstr'Last) := ASCII.NUL;
326 -- Convert form string to lower case
328 for J in Formstr'Range loop
329 if Formstr (J) in 'A
' .. 'Z
' then
331 Character'Val (Character'Pos (Formstr (J)) + 32);
337 Form_Parameter (Formstr, "mode", V1, V2);
341 elsif Formstr (V1 .. V2) = "copy" then
343 elsif Formstr (V1 .. V2) = "overwrite" then
345 elsif Formstr (V1 .. V2) = "append" then
348 raise Use_Error with "invalid Form";
351 Form_Parameter (Formstr, "preserve", V1, V2);
355 elsif Formstr (V1 .. V2) = "timestamps" then
356 Preserve := Time_Stamps;
357 elsif Formstr (V1 .. V2) = "all_attributes" then
359 elsif Formstr (V1 .. V2) = "no_attributes" then
362 raise Use_Error with "invalid Form";
367 -- Do actual copy using System.OS_Lib.Copy_File
369 Copy_File (Source_Name, Target_Name, Success, Mode, Preserve);
372 raise Use_Error with "copy of """ & Source_Name & """ failed";
377 ----------------------
378 -- Create_Directory --
379 ----------------------
381 procedure Create_Directory
382 (New_Directory : String;
385 C_Dir_Name : constant String := New_Directory & ASCII.NUL;
388 -- First, the invalid case
390 if not Is_Valid_Path_Name (New_Directory) then
391 raise Name_Error with
392 "invalid new directory path name """ & New_Directory & '"';
395 -- Acquire setting of encoding parameter
398 Formstr : constant String := To_Lower (Form);
400 Encoding : CRTL.Filename_Encoding;
401 -- Filename encoding specified into the form parameter
406 Form_Parameter (Formstr, "encoding
", V1, V2);
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;
415 raise Use_Error with "invalid Form
";
418 if CRTL.mkdir (C_Dir_Name, Encoding) /= 0 then
420 "creation
of new directory
""" & New_Directory & """ failed
";
424 end Create_Directory;
430 procedure Create_Path
431 (New_Directory : String;
434 New_Dir : String (1 .. New_Directory'Length + 1);
435 Last : Positive := 1;
436 Start : Positive := 1;
439 -- First, the invalid case
441 if not Is_Valid_Path_Name (New_Directory) then
442 raise Name_Error with
443 "invalid
new directory path name
""" & New_Directory & '"';
446 -- Build New_Dir with a directory separator at the end, so that the
447 -- complete path will be found in the loop below.
449 New_Dir (1 .. New_Directory'Length) := New_Directory;
450 New_Dir (New_Dir'Last) := Directory_Separator;
452 -- If host is windows, and the first two characters are directory
453 -- separators, we have an UNC path. Skip it.
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)
463 exit when Start = New_Dir'Last
464 or else Is_In (New_Dir (Start), Dir_Seps);
468 -- Create, if necessary, each directory in the path
470 for J in Start + 1 .. New_Dir'Last loop
472 -- Look for the end of an intermediate directory
474 if not Is_In (New_Dir (J), Dir_Seps) then
477 -- We have found a new intermediate directory each time we find
478 -- a first directory separator.
480 elsif not Is_In (New_Dir (J - 1), Dir_Seps) then
482 -- No need to create the directory if it already exists
484 if not Is_Directory (New_Dir (1 .. Last)) then
487 (New_Directory => New_Dir (1 .. Last), Form => Form);
491 if File_Exists (New_Dir (1 .. Last)) then
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.
498 if not Is_Directory (New_Dir (1 .. Last)) then
500 "file """ & New_Dir (1 .. Last) &
501 """ already exists and is not a directory";
505 -- Create_Directory failed for some other reason:
506 -- propagate the exception.
517 -----------------------
518 -- Current_Directory --
519 -----------------------
521 function Current_Directory return String is
522 Path_Len : Natural := Max_Path;
523 Buffer : String (1 .. 1 + Max_Path + 1);
525 procedure Local_Get_Current_Dir (Dir : Address; Length : Address);
526 pragma Import (C, Local_Get_Current_Dir, "__gnat_get_current_dir");
529 Local_Get_Current_Dir (Buffer'Address, Path_Len'Address);
532 raise Use_Error with "current directory does not exist";
535 -- We need to resolve links because of RM A.16(47), which requires
536 -- that we not return alternative names for files.
538 return Normalize_Pathname (Buffer (1 .. Path_Len));
539 end Current_Directory;
541 ----------------------
542 -- Delete_Directory --
543 ----------------------
545 procedure Delete_Directory (Directory : String) is
547 -- First, the invalid cases
549 if not Is_Valid_Path_Name (Directory) then
550 raise Name_Error with
551 "invalid directory path name """ & Directory & '"';
553 elsif not Is_Directory (Directory) then
554 raise Name_Error with '"' & Directory & """ not a directory";
556 -- Do the deletion, checking for error
560 C_Dir_Name : constant String := Directory & ASCII.NUL;
562 if rmdir (C_Dir_Name) /= 0 then
564 "deletion of directory """ & Directory & """ failed";
568 end Delete_Directory;
574 procedure Delete_File (Name : String) is
578 -- First, the invalid cases
580 if not Is_Valid_Path_Name (Name) then
581 raise Name_Error with "invalid path name """ & Name & '"';
583 elsif not Is_Regular_File (Name)
584 and then not Is_Symbolic_Link (Name)
586 raise Name_Error with "file
""" & Name & """ does
not exist
";
589 -- Do actual deletion using System.OS_Lib.Delete_File
591 Delete_File (Name, Success);
594 raise Use_Error with "file
""" & Name & """ could
not be deleted
";
603 procedure Delete_Tree (Directory : String) is
604 Search : Search_Type;
605 Dir_Ent : Directory_Entry_Type;
607 -- First, the invalid cases
609 if not Is_Valid_Path_Name (Directory) then
610 raise Name_Error with
611 "invalid directory path name
""" & Directory & '"';
613 elsif not Is_Directory (Directory) then
614 raise Name_Error with '"' & Directory & """ not a directory
";
618 -- We used to change the current directory to Directory here,
619 -- allowing the use of a local Simple_Name for all references. This
620 -- turned out unfriendly to multitasking programs, where tasks
621 -- running in parallel of this Delete_Tree could see their current
622 -- directory change unpredictably. We now resort to Full_Name
623 -- computations to reach files and subdirs instead.
625 Start_Search (Search, Directory => Directory, Pattern => "");
626 while More_Entries (Search) loop
627 Get_Next_Entry (Search, Dir_Ent);
630 Fname : constant String := Full_Name (Dir_Ent);
631 Sname : constant String := Simple_Name (Dir_Ent);
634 if OS_Lib.Is_Directory (Fname) then
635 if Sname /= "." and then Sname /= ".." then
647 C_Dir_Name : constant String := Directory & ASCII.NUL;
650 if rmdir (C_Dir_Name) /= 0 then
652 "directory tree rooted
at """ &
653 Directory & """ could
not be deleted
";
663 function Exists (Name : String) return Boolean is
665 -- First, the invalid case
667 if not Is_Valid_Path_Name (Name) then
668 raise Name_Error with "invalid path name
""" & Name & '"';
671 -- The implementation is in File_Exists
673 return File_Exists (Name);
681 function Extension (Name : String) return String is
683 -- First, the invalid case
685 if not Is_Valid_Path_Name (Name) then
686 raise Name_Error with "invalid path name """ & Name & '"';
689 -- Look for first dot that is not followed by a directory separator
691 for Pos in reverse Name'Range loop
693 -- If a directory separator is found before a dot, there is no
696 if Is_In (Name (Pos), Dir_Seps) then
699 elsif Name (Pos) = '.' then
701 -- We found a dot, build the return value with lower bound 1
704 subtype Result_Type is String (1 .. Name'Last - Pos);
706 return Result_Type (Name (Pos + 1 .. Name'Last));
711 -- No dot were found, there is no extension
717 ----------------------
718 -- Fetch_Next_Entry --
719 ----------------------
721 procedure Fetch_Next_Entry (Search : Search_Type) is
722 Name : String (1 .. NAME_MAX);
725 Kind : File_Kind := Ordinary_File;
726 -- Initialized to avoid a compilation warning
728 Filename_Addr : Address;
729 Filename_Len : aliased Integer;
731 Buffer : array (1 .. SIZEOF_struct_dirent_alloc) of Character;
733 function readdir_gnat
734 (Directory : Address;
736 Last : not null access Integer) return Address;
737 pragma Import (C, readdir_gnat, "__gnat_readdir
");
740 -- Search.Value.Is_Valid is always True when Fetch_Next_Entry is called
745 (Address (Search.Value.Dir),
747 Filename_Len'Access);
749 -- If no matching entry is found, set Is_Valid to False
751 if Filename_Addr = Null_Address then
752 Search.Value.Is_Valid := False;
756 if Filename_Len > Name'Length then
757 raise Use_Error with "file name too long
";
761 subtype Name_String is String (1 .. Filename_Len);
762 Dent_Name : Name_String;
763 for Dent_Name'Address use Filename_Addr;
764 pragma Import (Ada, Dent_Name);
767 Last := Filename_Len;
768 Name (1 .. Last) := Dent_Name;
771 -- Check if the entry matches the pattern
773 if Match (Name (1 .. Last), Search.Value.Pattern) then
775 C_Full_Name : constant String :=
776 Compose (To_String (Search.Value.Name),
777 Name (1 .. Last)) & ASCII.NUL;
778 Full_Name : String renames
780 (C_Full_Name'First .. C_Full_Name'Last - 1);
781 Found : Boolean := False;
782 Attr : aliased File_Attributes;
787 Reset_Attributes (Attr'Access);
788 Exists := File_Exists_Attr (C_Full_Name'Address, Attr'Access);
789 Error := Error_Attributes (Attr'Access);
793 with Full_Name & ": " & Errno_Message (Err => Error);
798 -- Now check if the file kind matches the filter
800 if Is_Regular_File_Attr
801 (C_Full_Name'Address, Attr'Access) = 1
803 if Search.Value.Filter (Ordinary_File) then
804 Kind := Ordinary_File;
808 elsif Is_Directory_Attr
809 (C_Full_Name'Address, Attr'Access) = 1
811 if Search.Value.Filter (Directory) then
816 elsif Search.Value.Filter (Special_File) then
817 Kind := Special_File;
821 -- If it does, update Search and return
824 Search.Value.Entry_Fetched := True;
825 Search.Value.Dir_Entry :=
827 Simple => To_Unbounded_String (Name (1 .. Last)),
828 Full => To_Unbounded_String (Full_Name),
836 end Fetch_Next_Entry;
842 function File_Exists (Name : String) return Boolean is
843 function C_File_Exists (A : Address) return Integer;
844 pragma Import (C, C_File_Exists, "__gnat_file_exists
");
846 C_Name : String (1 .. Name'Length + 1);
849 C_Name (1 .. Name'Length) := Name;
850 C_Name (C_Name'Last) := ASCII.NUL;
851 return C_File_Exists (C_Name'Address) = 1;
858 procedure Finalize (Search : in out Search_Type) is
860 if Search.Value /= null then
862 -- Close the directory, if one is open
864 if Search.Value.Dir /= No_Dir then
865 Close (Search.Value.Dir);
876 function Full_Name (Name : String) return String is
878 -- First, the invalid case
880 if not Is_Valid_Path_Name (Name) then
881 raise Name_Error with "invalid path name
""" & Name & '"';
884 -- Build the return value with lower bound 1
886 -- Use System.OS_Lib.Normalize_Pathname
889 -- We need to resolve links because of (RM A.16(47)), which says
890 -- we must not return alternative names for files.
892 Value : constant String := Normalize_Pathname (Name);
893 subtype Result is String (1 .. Value'Length);
896 return Result (Value);
901 function Full_Name (Directory_Entry : Directory_Entry_Type) return String is
903 -- First, the invalid case
905 if not Directory_Entry.Is_Valid then
906 raise Status_Error with "invalid directory entry";
909 -- The value to return has already been computed
911 return To_String (Directory_Entry.Full);
919 procedure Get_Next_Entry
920 (Search : in out Search_Type;
921 Directory_Entry : out Directory_Entry_Type)
924 -- First, the invalid case
926 if Search.Value = null or else not Search.Value.Is_Valid then
927 raise Status_Error with "invalid search";
930 -- Fetch the next entry, if needed
932 if not Search.Value.Entry_Fetched then
933 Fetch_Next_Entry (Search);
936 -- It is an error if no valid entry is found
938 if not Search.Value.Is_Valid then
939 raise Status_Error with "no next entry";
942 -- Reset Entry_Fetched and return the entry
944 Search.Value.Entry_Fetched := False;
945 Directory_Entry := Search.Value.Dir_Entry;
953 function Kind (Name : String) return File_Kind is
955 -- First, the invalid case
957 if not File_Exists (Name) then
958 raise Name_Error with "file """ & Name & """ does not exist";
960 -- If OK, return appropriate kind
962 elsif Is_Regular_File (Name) then
963 return Ordinary_File;
965 elsif Is_Directory (Name) then
973 function Kind (Directory_Entry : Directory_Entry_Type) return File_Kind is
975 -- First, the invalid case
977 if not Directory_Entry.Is_Valid then
978 raise Status_Error with "invalid directory entry";
981 -- The value to return has already be computed
983 return Directory_Entry.Kind;
987 -----------------------
988 -- Modification_Time --
989 -----------------------
991 function Modification_Time (Name : String) return Time is
997 Minute : Minute_Type;
998 Second : Second_Type;
1001 -- First, the invalid cases
1003 if not (Is_Regular_File (Name) or else Is_Directory (Name)) then
1004 raise Name_Error with '"' & Name & """ not a file
or directory
";
1007 Date := File_Time_Stamp (Name);
1009 -- Break down the time stamp into its constituents relative to GMT.
1010 -- This version of Split does not recognize leap seconds or buffer
1011 -- space for time zone processing.
1013 GM_Split (Date, Year, Month, Day, Hour, Minute, Second);
1015 -- The result must be in GMT. Ada.Calendar.
1016 -- Formatting.Time_Of with default time zone of zero (0) is the
1017 -- routine of choice.
1019 return Time_Of (Year, Month, Day, Hour, Minute, Second, 0.0);
1021 end Modification_Time;
1023 function Modification_Time
1024 (Directory_Entry : Directory_Entry_Type) return Ada.Calendar.Time
1027 -- First, the invalid case
1029 if not Directory_Entry.Is_Valid then
1030 raise Status_Error with "invalid directory
entry";
1033 -- The value to return has already be computed
1035 return Modification_Time (To_String (Directory_Entry.Full));
1037 end Modification_Time;
1043 function More_Entries (Search : Search_Type) return Boolean is
1045 if Search.Value = null then
1048 elsif Search.Value.Is_Valid then
1050 -- Fetch the next entry, if needed
1052 if not Search.Value.Entry_Fetched then
1053 Fetch_Next_Entry (Search);
1057 return Search.Value.Is_Valid;
1064 procedure Rename (Old_Name, New_Name : String) is
1068 -- First, the invalid cases
1070 if not Is_Valid_Path_Name (Old_Name) then
1071 raise Name_Error with "invalid old path name
""" & Old_Name & '"';
1073 elsif not Is_Valid_Path_Name (New_Name) then
1074 raise Name_Error with "invalid new path name """ & New_Name & '"';
1076 elsif not Is_Regular_File (Old_Name)
1077 and then not Is_Directory (Old_Name)
1079 raise Name_Error with "old file
""" & Old_Name & """ does
not exist
";
1081 elsif Is_Regular_File (New_Name) or else Is_Directory (New_Name) then
1082 raise Use_Error with
1083 "new name
""" & New_Name
1084 & """ designates a file that already exists
";
1086 -- Do actual rename using System.OS_Lib.Rename_File
1089 Rename_File (Old_Name, New_Name, Success);
1093 -- AI05-0231-1: Name_Error should be raised in case a directory
1094 -- component of New_Name does not exist (as in New_Name =>
1095 -- "/no
-such
-dir
/new-filename
"). ENOENT indicates that. ENOENT
1096 -- also indicate that the Old_Name does not exist, but we already
1097 -- checked for that above. All other errors are Use_Error.
1099 if Errno = ENOENT then
1100 raise Name_Error with
1101 "file
""" & Containing_Directory (New_Name) & """ not found
";
1104 raise Use_Error with
1105 "file
""" & Old_Name & """ could
not be renamed
";
1116 (Directory : String;
1118 Filter : Filter_Type := (others => True);
1119 Process : not null access procedure
1120 (Directory_Entry : Directory_Entry_Type))
1123 Directory_Entry : Directory_Entry_Type;
1126 Start_Search (Srch, Directory, Pattern, Filter);
1127 while More_Entries (Srch) loop
1128 Get_Next_Entry (Srch, Directory_Entry);
1129 Process (Directory_Entry);
1139 procedure Set_Directory (Directory : String) is
1140 C_Dir_Name : constant String := Directory & ASCII.NUL;
1142 if not Is_Valid_Path_Name (Directory) then
1143 raise Name_Error with
1144 "invalid directory path name
& """ & Directory & '"';
1146 elsif not Is_Directory (Directory) then
1147 raise Name_Error with
1148 "directory """ & Directory & """ does not exist";
1150 elsif chdir (C_Dir_Name) /= 0 then
1151 raise Name_Error with
1152 "could not set to designated directory """ & Directory & '"';
1160 function Simple_Name (Name : String) return String is
1162 function Simple_Name_Internal (Path : String) return String;
1163 -- This function does the job
1165 --------------------------
1166 -- Simple_Name_Internal --
1167 --------------------------
1169 function Simple_Name_Internal (Path : String) return String is
1170 Cut_Start : Natural :=
1171 Strings.Fixed.Index (Path, Dir_Seps, Going => Strings.Backward);
1175 -- Cut_Start pointS to the first simple name character
1177 Cut_Start := (if Cut_Start = 0 then Path'First else Cut_Start + 1);
1179 -- Cut_End point to the last simple name character
1181 Cut_End := Path'Last;
1183 Check_For_Standard_Dirs : declare
1184 BN : constant String := Path (Cut_Start .. Cut_End);
1186 Has_Drive_Letter : constant Boolean :=
1187 OS_Lib.Path_Separator /= ':';
1188 -- If Path separator is not ':' then we are on a DOS based OS
1189 -- where this character is used as a drive letter separator.
1192 if BN = "." or else BN = ".." then
1195 elsif Has_Drive_Letter
1196 and then BN'Length > 2
1197 and then Characters.Handling.Is_Letter (BN (BN'First))
1198 and then BN (BN'First + 1) = ':'
1200 -- We have a DOS drive letter prefix, remove it
1202 return BN (BN'First + 2 .. BN'Last);
1207 end Check_For_Standard_Dirs;
1208 end Simple_Name_Internal;
1210 -- Start of processing for Simple_Name
1213 -- First, the invalid case
1215 if not Is_Valid_Path_Name (Name) then
1216 raise Name_Error with "invalid path name
""" & Name & '"';
1219 -- Build the value to return with lower bound 1
1222 Value : constant String := Simple_Name_Internal (Name);
1223 subtype Result is String (1 .. Value'Length);
1225 return Result (Value);
1230 function Simple_Name
1231 (Directory_Entry : Directory_Entry_Type) return String is
1233 -- First, the invalid case
1235 if not Directory_Entry.Is_Valid then
1236 raise Status_Error with "invalid directory entry";
1239 -- The value to return has already be computed
1241 return To_String (Directory_Entry.Simple);
1249 function Size (Name : String) return File_Size is
1250 C_Name : String (1 .. Name'Length + 1);
1252 function C_Size (Name : Address) return int64;
1253 pragma Import (C, C_Size, "__gnat_named_file_length");
1256 -- First, the invalid case
1258 if not Is_Regular_File (Name) then
1259 raise Name_Error with "file """ & Name & """ does not exist";
1262 C_Name (1 .. Name'Length) := Name;
1263 C_Name (C_Name'Last) := ASCII.NUL;
1264 return File_Size (C_Size (C_Name'Address));
1268 function Size (Directory_Entry : Directory_Entry_Type) return File_Size is
1270 -- First, the invalid case
1272 if not Directory_Entry.Is_Valid then
1273 raise Status_Error with "invalid directory entry";
1276 -- The value to return has already be computed
1278 return Size (To_String (Directory_Entry.Full));
1286 procedure Start_Search
1287 (Search : in out Search_Type;
1290 Filter : Filter_Type := (others => True))
1292 function opendir (file_name : String) return DIRs;
1293 pragma Import (C, opendir, "__gnat_opendir");
1295 C_File_Name : constant String := Directory & ASCII.NUL;
1297 Dir : Dir_Type_Value;
1300 -- First, the invalid case Name_Error
1302 if not Is_Directory (Directory) then
1303 raise Name_Error with
1304 "unknown directory """ & Simple_Name (Directory) & '"';
1307 -- Check the pattern
1313 Case_Sensitive => Is_Path_Name_Case_Sensitive);
1315 when Error_In_Regexp =>
1316 Free (Search.Value);
1317 raise Name_Error with "invalid pattern
""" & Pattern & '"';
1320 Dir := Dir_Type_Value (opendir (C_File_Name));
1322 if Dir = No_Dir then
1323 raise Use_Error with
1324 "unreadable directory """ & Simple_Name (Directory) & '"';
1327 -- If needed, finalize Search
1331 -- Allocate the default data
1333 Search.Value := new Search_Data;
1335 -- Initialize some Search components
1337 Search.Value.Filter := Filter;
1338 Search.Value.Name := To_Unbounded_String (Full_Name (Directory));
1339 Search.Value.Pattern := Pat;
1340 Search.Value.Dir := Dir;
1341 Search.Value.Is_Valid := True;
1344 end Ada.Directories;