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-2014, 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 -- We need to resolve links because of A.16(47), since we must not
212 -- return alternative names for files.
214 Norm : constant String := Normalize_Pathname (Name);
215 Last_DS : constant Natural :=
216 Strings.Fixed.Index (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'
239 Norm (Norm'First) in 'A' .. 'Z'))))
242 "directory
""" & Name & """ has no containing directory
";
246 Last : Positive := Last_DS - Name'First + 1;
247 Result : String (1 .. Last);
250 Result := Name (Name'First .. Last_DS);
252 -- Remove any trailing directory separator, except as the
253 -- first character or the first character following a drive
254 -- number on Windows.
260 Result (Last) /= Directory_Separator;
264 and then Result (2) = ':'
266 (Result (1) in 'A' .. 'Z'
268 Result (1) in 'a' .. 'z');
273 -- Special case of current directory, identified by "."
275 if Last = 1 and then Result (1) = '.' then
276 return Current_Directory;
278 -- Special case of "..": the current directory may be a root
281 elsif Last = 2 and then Result (1 .. 2) = ".." then
282 return Containing_Directory (Current_Directory);
285 return Result (1 .. Last);
291 end Containing_Directory;
298 (Source_Name : String;
299 Target_Name : String;
303 Mode : Copy_Mode := Overwrite;
304 Preserve : Attribute := None;
307 -- First, the invalid cases
309 if not Is_Valid_Path_Name (Source_Name) then
310 raise Name_Error with
311 "invalid source path name
""" & Source_Name & '"';
313 elsif not Is_Valid_Path_Name (Target_Name) then
314 raise Name_Error with
315 "invalid target path name """ & Target_Name & '"';
317 elsif not Is_Regular_File (Source_Name) then
318 raise Name_Error with '"' & Source_Name & """ is not a file";
320 elsif Is_Directory (Target_Name) then
321 raise Use_Error with "target """ & Target_Name & """ is a directory";
324 if Form'Length > 0 then
326 Formstr : String (1 .. Form'Length + 1);
330 -- Acquire form string, setting required NUL terminator
332 Formstr (1 .. Form'Length) := Form;
333 Formstr (Formstr'Last) := ASCII.NUL;
335 -- Convert form string to lower case
337 for J in Formstr'Range loop
338 if Formstr (J) in 'A
' .. 'Z
' then
340 Character'Val (Character'Pos (Formstr (J)) + 32);
346 Form_Parameter (Formstr, "mode", V1, V2);
350 elsif Formstr (V1 .. V2) = "copy" then
352 elsif Formstr (V1 .. V2) = "overwrite" then
354 elsif Formstr (V1 .. V2) = "append" then
357 raise Use_Error with "invalid Form";
360 Form_Parameter (Formstr, "preserve", V1, V2);
364 elsif Formstr (V1 .. V2) = "timestamps" then
365 Preserve := Time_Stamps;
366 elsif Formstr (V1 .. V2) = "all_attributes" then
368 elsif Formstr (V1 .. V2) = "no_attributes" then
371 raise Use_Error with "invalid Form";
376 -- Do actual copy using System.OS_Lib.Copy_File
378 Copy_File (Source_Name, Target_Name, Success, Mode, Preserve);
381 raise Use_Error with "copy of """ & Source_Name & """ failed";
386 ----------------------
387 -- Create_Directory --
388 ----------------------
390 procedure Create_Directory
391 (New_Directory : String;
394 C_Dir_Name : constant String := New_Directory & ASCII.NUL;
397 -- First, the invalid case
399 if not Is_Valid_Path_Name (New_Directory) then
400 raise Name_Error with
401 "invalid new directory path name """ & New_Directory & '"';
404 -- Acquire setting of encoding parameter
407 Formstr : constant String := To_Lower (Form);
409 Encoding : CRTL.Filename_Encoding;
410 -- Filename encoding specified into the form parameter
415 Form_Parameter (Formstr, "encoding
", V1, V2);
418 Encoding := CRTL.Unspecified;
419 elsif Formstr (V1 .. V2) = "utf8
" then
420 Encoding := CRTL.UTF8;
421 elsif Formstr (V1 .. V2) = "8bits
" then
422 Encoding := CRTL.ASCII_8bits;
424 raise Use_Error with "invalid Form
";
427 if CRTL.mkdir (C_Dir_Name, Encoding) /= 0 then
429 "creation
of new directory
""" & New_Directory & """ failed
";
433 end Create_Directory;
439 procedure Create_Path
440 (New_Directory : String;
443 New_Dir : String (1 .. New_Directory'Length + 1);
444 Last : Positive := 1;
445 Start : Positive := 1;
448 -- First, the invalid case
450 if not Is_Valid_Path_Name (New_Directory) then
451 raise Name_Error with
452 "invalid
new directory path name
""" & New_Directory & '"';
455 -- Build New_Dir with a directory separator at the end, so that the
456 -- complete path will be found in the loop below.
458 New_Dir (1 .. New_Directory'Length) := New_Directory;
459 New_Dir (New_Dir'Last) := Directory_Separator;
461 -- If host is windows, and the first two characters are directory
462 -- separators, we have an UNC path. Skip it.
464 if Directory_Separator = '\
'
465 and then New_Dir'Length > 2
466 and then Is_In (New_Dir (1), Dir_Seps)
467 and then Is_In (New_Dir (2), Dir_Seps)
472 exit when Start = New_Dir'Last
473 or else Is_In (New_Dir (Start), Dir_Seps);
477 -- Create, if necessary, each directory in the path
479 for J in Start + 1 .. New_Dir'Last loop
481 -- Look for the end of an intermediate directory
483 if not Is_In (New_Dir (J), Dir_Seps) then
486 -- We have found a new intermediate directory each time we find
487 -- a first directory separator.
489 elsif not Is_In (New_Dir (J - 1), Dir_Seps) then
491 -- No need to create the directory if it already exists
493 if not Is_Directory (New_Dir (1 .. Last)) then
496 (New_Directory => New_Dir (1 .. Last), Form => Form);
500 if File_Exists (New_Dir (1 .. Last)) then
502 -- A file with such a name already exists. If it is
503 -- a directory, then it was apparently just created
504 -- by another process or thread, and all is well.
505 -- If it is of some other kind, report an error.
507 if not Is_Directory (New_Dir (1 .. Last)) then
509 "file """ & New_Dir (1 .. Last) &
510 """ already exists and is not a directory";
514 -- Create_Directory failed for some other reason:
515 -- propagate the exception.
526 -----------------------
527 -- Current_Directory --
528 -----------------------
530 function Current_Directory return String is
531 Path_Len : Natural := Max_Path;
532 Buffer : String (1 .. 1 + Max_Path + 1);
534 procedure Local_Get_Current_Dir (Dir : Address; Length : Address);
535 pragma Import (C, Local_Get_Current_Dir, "__gnat_get_current_dir");
538 Local_Get_Current_Dir (Buffer'Address, Path_Len'Address);
540 -- We need to resolve links because of RM A.16(47), which requires
541 -- that we not return alternative names for files.
543 return Normalize_Pathname (Buffer (1 .. Path_Len));
544 end Current_Directory;
546 ----------------------
547 -- Delete_Directory --
548 ----------------------
550 procedure Delete_Directory (Directory : String) is
552 -- First, the invalid cases
554 if not Is_Valid_Path_Name (Directory) then
555 raise Name_Error with
556 "invalid directory path name """ & Directory & '"';
558 elsif not Is_Directory (Directory) then
559 raise Name_Error with '"' & Directory & """ not a directory";
561 -- Do the deletion, checking for error
565 C_Dir_Name : constant String := Directory & ASCII.NUL;
567 if rmdir (C_Dir_Name) /= 0 then
569 "deletion of directory """ & Directory & """ failed";
573 end Delete_Directory;
579 procedure Delete_File (Name : String) is
583 -- First, the invalid cases
585 if not Is_Valid_Path_Name (Name) then
586 raise Name_Error with "invalid path name """ & Name & '"';
588 elsif not Is_Regular_File (Name)
589 and then not Is_Symbolic_Link (Name)
591 raise Name_Error with "file
""" & Name & """ does
not exist
";
594 -- Do actual deletion using System.OS_Lib.Delete_File
596 Delete_File (Name, Success);
599 raise Use_Error with "file
""" & Name & """ could
not be deleted
";
608 procedure Delete_Tree (Directory : String) is
609 Current_Dir : constant String := Current_Directory;
610 Search : Search_Type;
611 Dir_Ent : Directory_Entry_Type;
613 -- First, the invalid cases
615 if not Is_Valid_Path_Name (Directory) then
616 raise Name_Error with
617 "invalid directory path name
""" & Directory & '"';
619 elsif not Is_Directory (Directory) then
620 raise Name_Error with '"' & Directory & """ not a directory
";
623 Set_Directory (Directory);
625 Start_Search (Search, Directory => ".", Pattern => "");
626 while More_Entries (Search) loop
627 Get_Next_Entry (Search, Dir_Ent);
630 File_Name : constant String := Simple_Name (Dir_Ent);
633 if OS_Lib.Is_Directory (File_Name) then
634 if File_Name /= "." and then File_Name /= ".." then
635 Delete_Tree (File_Name);
639 Delete_File (File_Name);
644 Set_Directory (Current_Dir);
648 C_Dir_Name : constant String := Directory & ASCII.NUL;
651 if rmdir (C_Dir_Name) /= 0 then
653 "directory tree rooted
at """ &
654 Directory & """ could
not be deleted
";
664 function Exists (Name : String) return Boolean is
666 -- First, the invalid case
668 if not Is_Valid_Path_Name (Name) then
669 raise Name_Error with "invalid path name
""" & Name & '"';
672 -- The implementation is in File_Exists
674 return File_Exists (Name);
682 function Extension (Name : String) return String is
684 -- First, the invalid case
686 if not Is_Valid_Path_Name (Name) then
687 raise Name_Error with "invalid path name """ & Name & '"';
690 -- Look for first dot that is not followed by a directory separator
692 for Pos in reverse Name'Range loop
694 -- If a directory separator is found before a dot, there is no
697 if Is_In (Name (Pos), Dir_Seps) then
700 elsif Name (Pos) = '.' then
702 -- We found a dot, build the return value with lower bound 1
705 subtype Result_Type is String (1 .. Name'Last - Pos);
707 return Result_Type (Name (Pos + 1 .. Name'Last));
712 -- No dot were found, there is no extension
718 ----------------------
719 -- Fetch_Next_Entry --
720 ----------------------
722 procedure Fetch_Next_Entry (Search : Search_Type) is
723 Name : String (1 .. NAME_MAX);
726 Kind : File_Kind := Ordinary_File;
727 -- Initialized to avoid a compilation warning
729 Filename_Addr : Address;
730 Filename_Len : aliased Integer;
732 Buffer : array (1 .. SIZEOF_struct_dirent_alloc) of Character;
734 function readdir_gnat
735 (Directory : Address;
737 Last : not null access Integer) return Address;
738 pragma Import (C, readdir_gnat, "__gnat_readdir
");
741 -- Search.Value.Is_Valid is always True when Fetch_Next_Entry is called
746 (Address (Search.Value.Dir),
748 Filename_Len'Access);
750 -- If no matching entry is found, set Is_Valid to False
752 if Filename_Addr = Null_Address then
753 Search.Value.Is_Valid := False;
757 if Filename_Len > Name'Length then
758 raise Use_Error with "file name too long
";
762 subtype Name_String is String (1 .. Filename_Len);
763 Dent_Name : Name_String;
764 for Dent_Name'Address use Filename_Addr;
765 pragma Import (Ada, Dent_Name);
768 Last := Filename_Len;
769 Name (1 .. Last) := Dent_Name;
772 -- Check if the entry matches the pattern
774 if Match (Name (1 .. Last), Search.Value.Pattern) then
776 C_Full_Name : constant String :=
777 Compose (To_String (Search.Value.Name),
778 Name (1 .. Last)) & ASCII.NUL;
779 Full_Name : String renames
781 (C_Full_Name'First .. C_Full_Name'Last - 1);
782 Found : Boolean := False;
783 Attr : aliased File_Attributes;
788 Reset_Attributes (Attr'Access);
789 Exists := File_Exists_Attr (C_Full_Name'Address, Attr'Access);
790 Error := Error_Attributes (Attr'Access);
794 with Full_Name & ": " & Errno_Message (Err => Error);
799 -- Now check if the file kind matches the filter
801 if Is_Regular_File_Attr
802 (C_Full_Name'Address, Attr'Access) = 1
804 if Search.Value.Filter (Ordinary_File) then
805 Kind := Ordinary_File;
809 elsif Is_Directory_Attr
810 (C_Full_Name'Address, Attr'Access) = 1
812 if Search.Value.Filter (Directory) then
817 elsif Search.Value.Filter (Special_File) then
818 Kind := Special_File;
822 -- If it does, update Search and return
825 Search.Value.Entry_Fetched := True;
826 Search.Value.Dir_Entry :=
828 Simple => To_Unbounded_String (Name (1 .. Last)),
829 Full => To_Unbounded_String (Full_Name),
837 end Fetch_Next_Entry;
843 function File_Exists (Name : String) return Boolean is
844 function C_File_Exists (A : Address) return Integer;
845 pragma Import (C, C_File_Exists, "__gnat_file_exists
");
847 C_Name : String (1 .. Name'Length + 1);
850 C_Name (1 .. Name'Length) := Name;
851 C_Name (C_Name'Last) := ASCII.NUL;
852 return C_File_Exists (C_Name'Address) = 1;
859 procedure Finalize (Search : in out Search_Type) is
861 if Search.Value /= null then
863 -- Close the directory, if one is open
865 if Search.Value.Dir /= No_Dir then
866 Close (Search.Value.Dir);
877 function Full_Name (Name : String) return String is
879 -- First, the invalid case
881 if not Is_Valid_Path_Name (Name) then
882 raise Name_Error with "invalid path name
""" & Name & '"';
885 -- Build the return value with lower bound 1
887 -- Use System.OS_Lib.Normalize_Pathname
890 -- We need to resolve links because of (RM A.16(47)), which says
891 -- we must not return alternative names for files.
893 Value : constant String := Normalize_Pathname (Name);
894 subtype Result is String (1 .. Value'Length);
897 return Result (Value);
902 function Full_Name (Directory_Entry : Directory_Entry_Type) return String is
904 -- First, the invalid case
906 if not Directory_Entry.Is_Valid then
907 raise Status_Error with "invalid directory entry";
910 -- The value to return has already been computed
912 return To_String (Directory_Entry.Full);
920 procedure Get_Next_Entry
921 (Search : in out Search_Type;
922 Directory_Entry : out Directory_Entry_Type)
925 -- First, the invalid case
927 if Search.Value = null or else not Search.Value.Is_Valid then
928 raise Status_Error with "invalid search";
931 -- Fetch the next entry, if needed
933 if not Search.Value.Entry_Fetched then
934 Fetch_Next_Entry (Search);
937 -- It is an error if no valid entry is found
939 if not Search.Value.Is_Valid then
940 raise Status_Error with "no next entry";
943 -- Reset Entry_Fetched and return the entry
945 Search.Value.Entry_Fetched := False;
946 Directory_Entry := Search.Value.Dir_Entry;
954 function Kind (Name : String) return File_Kind is
956 -- First, the invalid case
958 if not File_Exists (Name) then
959 raise Name_Error with "file """ & Name & """ does not exist";
961 -- If OK, return appropriate kind
963 elsif Is_Regular_File (Name) then
964 return Ordinary_File;
966 elsif Is_Directory (Name) then
974 function Kind (Directory_Entry : Directory_Entry_Type) return File_Kind is
976 -- First, the invalid case
978 if not Directory_Entry.Is_Valid then
979 raise Status_Error with "invalid directory entry";
982 -- The value to return has already be computed
984 return Directory_Entry.Kind;
988 -----------------------
989 -- Modification_Time --
990 -----------------------
992 function Modification_Time (Name : String) return Time is
998 Minute : Minute_Type;
999 Second : Second_Type;
1002 -- First, the invalid cases
1004 if not (Is_Regular_File (Name) or else Is_Directory (Name)) then
1005 raise Name_Error with '"' & Name & """ not a file
or directory
";
1008 Date := File_Time_Stamp (Name);
1010 -- Break down the time stamp into its constituents relative to GMT.
1011 -- This version of Split does not recognize leap seconds or buffer
1012 -- space for time zone processing.
1014 GM_Split (Date, Year, Month, Day, Hour, Minute, Second);
1016 -- The result must be in GMT. Ada.Calendar.
1017 -- Formatting.Time_Of with default time zone of zero (0) is the
1018 -- routine of choice.
1020 return Time_Of (Year, Month, Day, Hour, Minute, Second, 0.0);
1022 end Modification_Time;
1024 function Modification_Time
1025 (Directory_Entry : Directory_Entry_Type) return Ada.Calendar.Time
1028 -- First, the invalid case
1030 if not Directory_Entry.Is_Valid then
1031 raise Status_Error with "invalid directory
entry";
1034 -- The value to return has already be computed
1036 return Modification_Time (To_String (Directory_Entry.Full));
1038 end Modification_Time;
1044 function More_Entries (Search : Search_Type) return Boolean is
1046 if Search.Value = null then
1049 elsif Search.Value.Is_Valid then
1051 -- Fetch the next entry, if needed
1053 if not Search.Value.Entry_Fetched then
1054 Fetch_Next_Entry (Search);
1058 return Search.Value.Is_Valid;
1065 procedure Rename (Old_Name, New_Name : String) is
1069 -- First, the invalid cases
1071 if not Is_Valid_Path_Name (Old_Name) then
1072 raise Name_Error with "invalid old path name
""" & Old_Name & '"';
1074 elsif not Is_Valid_Path_Name (New_Name) then
1075 raise Name_Error with "invalid new path name """ & New_Name & '"';
1077 elsif not Is_Regular_File (Old_Name)
1078 and then not Is_Directory (Old_Name)
1080 raise Name_Error with "old file
""" & Old_Name & """ does
not exist
";
1082 elsif Is_Regular_File (New_Name) or else Is_Directory (New_Name) then
1083 raise Use_Error with
1084 "new name
""" & New_Name
1085 & """ designates a file that already exists
";
1087 -- Do actual rename using System.OS_Lib.Rename_File
1090 Rename_File (Old_Name, New_Name, Success);
1094 -- AI05-0231-1: Name_Error should be raised in case a directory
1095 -- component of New_Name does not exist (as in New_Name =>
1096 -- "/no
-such
-dir
/new-filename
"). ENOENT indicates that. ENOENT
1097 -- also indicate that the Old_Name does not exist, but we already
1098 -- checked for that above. All other errors are Use_Error.
1100 if Errno = ENOENT then
1101 raise Name_Error with
1102 "file
""" & Containing_Directory (New_Name) & """ not found
";
1105 raise Use_Error with
1106 "file
""" & Old_Name & """ could
not be renamed
";
1117 (Directory : String;
1119 Filter : Filter_Type := (others => True);
1120 Process : not null access procedure
1121 (Directory_Entry : Directory_Entry_Type))
1124 Directory_Entry : Directory_Entry_Type;
1127 Start_Search (Srch, Directory, Pattern, Filter);
1128 while More_Entries (Srch) loop
1129 Get_Next_Entry (Srch, Directory_Entry);
1130 Process (Directory_Entry);
1140 procedure Set_Directory (Directory : String) is
1141 C_Dir_Name : constant String := Directory & ASCII.NUL;
1143 if not Is_Valid_Path_Name (Directory) then
1144 raise Name_Error with
1145 "invalid directory path name
& """ & Directory & '"';
1147 elsif not Is_Directory (Directory) then
1148 raise Name_Error with
1149 "directory """ & Directory & """ does not exist";
1151 elsif chdir (C_Dir_Name) /= 0 then
1152 raise Name_Error with
1153 "could not set to designated directory """ & Directory & '"';
1161 function Simple_Name (Name : String) return String is
1163 function Simple_Name_Internal (Path : String) return String;
1164 -- This function does the job
1166 --------------------------
1167 -- Simple_Name_Internal --
1168 --------------------------
1170 function Simple_Name_Internal (Path : String) return String is
1171 Cut_Start : Natural :=
1172 Strings.Fixed.Index (Path, Dir_Seps, Going => Strings.Backward);
1176 -- Cut_Start pointS to the first simple name character
1178 Cut_Start := (if Cut_Start = 0 then Path'First else Cut_Start + 1);
1180 -- Cut_End point to the last simple name character
1182 Cut_End := Path'Last;
1184 Check_For_Standard_Dirs : declare
1185 BN : constant String := Path (Cut_Start .. Cut_End);
1187 Has_Drive_Letter : constant Boolean :=
1188 OS_Lib.Path_Separator /= ':';
1189 -- If Path separator is not ':' then we are on a DOS based OS
1190 -- where this character is used as a drive letter separator.
1193 if BN = "." or else BN = ".." then
1196 elsif Has_Drive_Letter
1197 and then BN'Length > 2
1198 and then Characters.Handling.Is_Letter (BN (BN'First))
1199 and then BN (BN'First + 1) = ':'
1201 -- We have a DOS drive letter prefix, remove it
1203 return BN (BN'First + 2 .. BN'Last);
1208 end Check_For_Standard_Dirs;
1209 end Simple_Name_Internal;
1211 -- Start of processing for Simple_Name
1214 -- First, the invalid case
1216 if not Is_Valid_Path_Name (Name) then
1217 raise Name_Error with "invalid path name
""" & Name & '"';
1220 -- Build the value to return with lower bound 1
1223 Value : constant String := Simple_Name_Internal (Name);
1224 subtype Result is String (1 .. Value'Length);
1226 return Result (Value);
1231 function Simple_Name
1232 (Directory_Entry : Directory_Entry_Type) return String is
1234 -- First, the invalid case
1236 if not Directory_Entry.Is_Valid then
1237 raise Status_Error with "invalid directory entry";
1240 -- The value to return has already be computed
1242 return To_String (Directory_Entry.Simple);
1250 function Size (Name : String) return File_Size is
1251 C_Name : String (1 .. Name'Length + 1);
1253 function C_Size (Name : Address) return int64;
1254 pragma Import (C, C_Size, "__gnat_named_file_length");
1257 -- First, the invalid case
1259 if not Is_Regular_File (Name) then
1260 raise Name_Error with "file """ & Name & """ does not exist";
1263 C_Name (1 .. Name'Length) := Name;
1264 C_Name (C_Name'Last) := ASCII.NUL;
1265 return File_Size (C_Size (C_Name'Address));
1269 function Size (Directory_Entry : Directory_Entry_Type) return File_Size is
1271 -- First, the invalid case
1273 if not Directory_Entry.Is_Valid then
1274 raise Status_Error with "invalid directory entry";
1277 -- The value to return has already be computed
1279 return Size (To_String (Directory_Entry.Full));
1287 procedure Start_Search
1288 (Search : in out Search_Type;
1291 Filter : Filter_Type := (others => True))
1293 function opendir (file_name : String) return DIRs;
1294 pragma Import (C, opendir, "__gnat_opendir");
1296 C_File_Name : constant String := Directory & ASCII.NUL;
1298 Dir : Dir_Type_Value;
1301 -- First, the invalid case Name_Error
1303 if not Is_Directory (Directory) then
1304 raise Name_Error with
1305 "unknown directory """ & Simple_Name (Directory) & '"';
1308 -- Check the pattern
1314 Case_Sensitive => Is_Path_Name_Case_Sensitive);
1316 when Error_In_Regexp =>
1317 Free (Search.Value);
1318 raise Name_Error with "invalid pattern
""" & Pattern & '"';
1321 Dir := Dir_Type_Value (opendir (C_File_Name));
1323 if Dir = No_Dir then
1324 raise Use_Error with
1325 "unreadable directory """ & Simple_Name (Directory) & '"';
1328 -- If needed, finalize Search
1332 -- Allocate the default data
1334 Search.Value := new Search_Data;
1336 -- Initialize some Search components
1338 Search.Value.Filter := Filter;
1339 Search.Value.Name := To_Unbounded_String (Full_Name (Directory));
1340 Search.Value.Pattern := Pat;
1341 Search.Value.Dir := Dir;
1342 Search.Value.Is_Valid := True;
1345 end Ada.Directories;