* decl.c (compute_array_index_type): Use type_dependent_expression_p.
[official-gcc.git] / gcc / ada / a-direct.adb
blobe166c9f8f32c86b16dbd687c5bb6a371b93f6c75
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- A D A . D I R E C T O R I E S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2004-2012, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
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
56 -- opendir routine.
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
68 Max_Path : Integer;
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;
75 Pattern : Regexp;
76 Filter : Filter_Type;
77 Dir : Dir_Type_Value := No_Dir;
78 Entry_Fetched : Boolean := False;
79 Dir_Entry : Directory_Entry_Type;
80 end record;
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.
97 ---------------
98 -- Base_Name --
99 ---------------
101 function Base_Name (Name : String) return String is
102 Simple : constant String := Simple_Name (Name);
103 -- Simple'First is guaranteed to be 1
105 begin
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);
113 end if;
114 end loop;
116 -- If there is no dot, return the complete file name
118 return Simple;
119 end Base_Name;
121 -----------
122 -- Close --
123 -----------
125 procedure Close (Dir : Dir_Type_Value) is
126 Discard : Integer;
127 pragma Warnings (Off, Discard);
129 function closedir (directory : DIRs) return Integer;
130 pragma Import (C, closedir, "__gnat_closedir");
132 begin
133 Discard := closedir (DIRs (Dir));
134 end Close;
136 -------------
137 -- Compose --
138 -------------
140 function Compose
141 (Containing_Directory : String := "";
142 Name : String;
143 Extension : String := "") return String
145 Result : String (1 .. Containing_Directory'Length +
146 Name'Length + Extension'Length + 2);
147 Last : Natural;
149 begin
150 -- First, deal with the invalid cases
152 if Containing_Directory /= ""
153 and then not Is_Valid_Path_Name (Containing_Directory)
154 then
155 raise Name_Error with
156 "invalid directory path name """ & Containing_Directory & '"';
158 elsif
159 Extension'Length = 0 and then (not Is_Valid_Simple_Name (Name))
160 then
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)
166 then
167 raise Name_Error with
168 "invalid file name """ & Name & '.' & Extension & '"';
170 -- This is not an invalid case so build the path name
172 else
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
179 Last := Last + 1;
180 Result (Last) := Dir_Separator;
181 end if;
183 -- Add the file name
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
191 Last := Last + 1;
192 Result (Last) := '.';
193 Result (Last + 1 .. Last + Extension'Length) := Extension;
194 Last := Last + Extension'Length;
195 end if;
197 return Result (1 .. Last);
198 end if;
199 end Compose;
201 --------------------------
202 -- Containing_Directory --
203 --------------------------
205 function Containing_Directory (Name : String) return String is
206 begin
207 -- First, the invalid case
209 if not Is_Valid_Path_Name (Name) then
210 raise Name_Error with "invalid path name """ & Name & '"';
212 else
213 declare
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 :=
219 Strings.Fixed.Index
220 (Name, Dir_Seps, Going => Strings.Backward);
222 begin
223 if Last_DS = 0 then
225 -- There is no directory separator, returns current working
226 -- directory.
228 return Current_Directory;
230 -- If Name indicates a root directory, raise Use_Error, because
231 -- it has no containing directory.
233 elsif Norm = "/"
234 or else
235 (Windows
236 and then
237 (Norm = "\"
238 or else
239 (Norm'Length = 3
240 and then Norm (Norm'Last - 1 .. Norm'Last) = ":\"
241 and then (Norm (Norm'First) in 'a' .. 'z'
242 or else
243 Norm (Norm'First) in 'A' .. 'Z'))))
244 then
245 raise Use_Error with
246 "directory """ & Name & """ has no containing directory";
248 else
249 declare
250 Last : Positive := Last_DS - Name'First + 1;
251 Result : String (1 .. Last);
253 begin
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.
260 while Last > 1 loop
261 exit when
262 Result (Last) /= '/'
263 and then
264 Result (Last) /= Directory_Separator;
266 exit when Windows
267 and then Last = 3
268 and then Result (2) = ':'
269 and then
270 (Result (1) in 'A' .. 'Z'
271 or else
272 Result (1) in 'a' .. 'z');
274 Last := Last - 1;
275 end loop;
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
283 -- directory.
285 elsif Last = 2 and then Result (1 .. 2) = ".." then
286 return Containing_Directory (Current_Directory);
288 else
289 return Result (1 .. Last);
290 end if;
291 end;
292 end if;
293 end;
294 end if;
295 end Containing_Directory;
297 ---------------
298 -- Copy_File --
299 ---------------
301 procedure Copy_File
302 (Source_Name : String;
303 Target_Name : String;
304 Form : String := "")
306 Success : Boolean;
307 Mode : Copy_Mode := Overwrite;
308 Preserve : Attribute := None;
310 begin
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";
327 else
328 if Form'Length > 0 then
329 declare
330 Formstr : String (1 .. Form'Length + 1);
331 V1, V2 : Natural;
333 begin
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
343 Formstr (J) :=
344 Character'Val (Character'Pos (Formstr (J)) + 32);
345 end if;
346 end loop;
348 -- Check Form
350 Form_Parameter (Formstr, "mode", V1, V2);
352 if V1 = 0 then
353 Mode := Overwrite;
354 elsif Formstr (V1 .. V2) = "copy" then
355 Mode := Copy;
356 elsif Formstr (V1 .. V2) = "overwrite" then
357 Mode := Overwrite;
358 elsif Formstr (V1 .. V2) = "append" then
359 Mode := Append;
360 else
361 raise Use_Error with "invalid Form";
362 end if;
364 Form_Parameter (Formstr, "preserve", V1, V2);
366 if V1 = 0 then
367 Preserve := None;
368 elsif Formstr (V1 .. V2) = "timestamps" then
369 Preserve := Time_Stamps;
370 elsif Formstr (V1 .. V2) = "all_attributes" then
371 Preserve := Full;
372 elsif Formstr (V1 .. V2) = "no_attributes" then
373 Preserve := None;
374 else
375 raise Use_Error with "invalid Form";
376 end if;
377 end;
378 end if;
380 -- Do actual copy using System.OS_Lib.Copy_File
382 Copy_File (Source_Name, Target_Name, Success, Mode, Preserve);
384 if not Success then
385 raise Use_Error with "copy of """ & Source_Name & """ failed";
386 end if;
387 end if;
388 end Copy_File;
390 ----------------------
391 -- Create_Directory --
392 ----------------------
394 procedure Create_Directory
395 (New_Directory : String;
396 Form : String := "")
398 C_Dir_Name : constant String := New_Directory & ASCII.NUL;
400 begin
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 & '"';
407 else
408 -- Acquire setting of encoding parameter
410 declare
411 Formstr : constant String := To_Lower (Form);
413 Encoding : CRTL.Filename_Encoding;
414 -- Filename encoding specified into the form parameter
416 V1, V2 : Natural;
418 begin
419 Form_Parameter (Formstr, "encoding", V1, V2);
421 if V1 = 0 then
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;
427 else
428 raise Use_Error with "invalid Form";
429 end if;
431 if CRTL.mkdir (C_Dir_Name, Encoding) /= 0 then
432 raise Use_Error with
433 "creation of new directory """ & New_Directory & """ failed";
434 end if;
435 end;
436 end if;
437 end Create_Directory;
439 -----------------
440 -- Create_Path --
441 -----------------
443 procedure Create_Path
444 (New_Directory : String;
445 Form : String := "")
447 New_Dir : String (1 .. New_Directory'Length + 1);
448 Last : Positive := 1;
449 Start : Positive := 1;
451 begin
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 & '"';
458 else
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)
472 then
473 Start := 2;
474 loop
475 Start := Start + 1;
476 exit when Start = New_Dir'Last
477 or else Is_In (New_Dir (Start), Dir_Seps);
478 end loop;
479 end if;
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
488 Last := J;
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
498 null;
500 -- It is an error if a file with such a name already exists
502 elsif Is_Regular_File (New_Dir (1 .. Last)) then
503 raise Use_Error with
504 "file """ & New_Dir (1 .. Last) & """ already exists";
506 else
507 Create_Directory
508 (New_Directory => New_Dir (1 .. Last), Form => Form);
509 end if;
510 end if;
511 end loop;
512 end if;
513 end Create_Path;
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");
526 begin
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
540 begin
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
552 else
553 declare
554 C_Dir_Name : constant String := Directory & ASCII.NUL;
555 begin
556 if rmdir (C_Dir_Name) /= 0 then
557 raise Use_Error with
558 "deletion of directory """ & Directory & """ failed";
559 end if;
560 end;
561 end if;
562 end Delete_Directory;
564 -----------------
565 -- Delete_File --
566 -----------------
568 procedure Delete_File (Name : String) is
569 Success : Boolean;
571 begin
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)
579 then
580 raise Name_Error with "file """ & Name & """ does not exist";
582 else
583 -- Do actual deletion using System.OS_Lib.Delete_File
585 Delete_File (Name, Success);
587 if not Success then
588 raise Use_Error with "file """ & Name & """ could not be deleted";
589 end if;
590 end if;
591 end Delete_File;
593 -----------------
594 -- Delete_Tree --
595 -----------------
597 procedure Delete_Tree (Directory : String) is
598 Current_Dir : constant String := Current_Directory;
599 Search : Search_Type;
600 Dir_Ent : Directory_Entry_Type;
601 begin
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";
611 else
612 Set_Directory (Directory);
614 Start_Search (Search, Directory => ".", Pattern => "");
615 while More_Entries (Search) loop
616 Get_Next_Entry (Search, Dir_Ent);
618 declare
619 File_Name : constant String := Simple_Name (Dir_Ent);
621 begin
622 if OS_Lib.Is_Directory (File_Name) then
623 if File_Name /= "." and then File_Name /= ".." then
624 Delete_Tree (File_Name);
625 end if;
627 else
628 Delete_File (File_Name);
629 end if;
630 end;
631 end loop;
633 Set_Directory (Current_Dir);
634 End_Search (Search);
636 declare
637 C_Dir_Name : constant String := Directory & ASCII.NUL;
639 begin
640 if rmdir (C_Dir_Name) /= 0 then
641 raise Use_Error with
642 "directory tree rooted at """ &
643 Directory & """ could not be deleted";
644 end if;
645 end;
646 end if;
647 end Delete_Tree;
649 ------------
650 -- Exists --
651 ------------
653 function Exists (Name : String) return Boolean is
654 begin
655 -- First, the invalid case
657 if not Is_Valid_Path_Name (Name) then
658 raise Name_Error with "invalid path name """ & Name & '"';
660 else
661 -- The implementation is in File_Exists
663 return File_Exists (Name);
664 end if;
665 end Exists;
667 ---------------
668 -- Extension --
669 ---------------
671 function Extension (Name : String) return String is
672 begin
673 -- First, the invalid case
675 if not Is_Valid_Path_Name (Name) then
676 raise Name_Error with "invalid path name """ & Name & '"';
678 else
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
684 -- extension.
686 if Is_In (Name (Pos), Dir_Seps) then
687 return Empty_String;
689 elsif Name (Pos) = '.' then
691 -- We found a dot, build the return value with lower bound 1
693 declare
694 subtype Result_Type is String (1 .. Name'Last - Pos);
695 begin
696 return Result_Type (Name (Pos + 1 .. Name'Last));
697 end;
698 end if;
699 end loop;
701 -- No dot were found, there is no extension
703 return Empty_String;
704 end if;
705 end Extension;
707 ----------------------
708 -- Fetch_Next_Entry --
709 ----------------------
711 procedure Fetch_Next_Entry (Search : Search_Type) is
712 Name : String (1 .. 255);
713 Last : Natural;
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;
727 Buffer : Address;
728 Last : not null access Integer) return Address;
729 pragma Import (C, readdir_gnat, "__gnat_readdir");
731 begin
732 -- Search.Value.Is_Valid is always True when Fetch_Next_Entry is called
734 loop
735 Filename_Addr :=
736 readdir_gnat
737 (Address (Search.Value.Dir),
738 Buffer'Address,
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;
745 exit;
746 end if;
748 declare
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
754 (Source => Address,
755 Target => Path_String_Access);
757 Path_Access : constant Path_String_Access :=
758 Address_To_Access (Filename_Addr);
760 begin
761 Last := Filename_Len;
762 Name (1 .. Last) := Path_Access.all;
763 end;
765 -- Check if the entry matches the pattern
767 if Match (Name (1 .. Last), Search.Value.Pattern) then
768 declare
769 Full_Name : constant String :=
770 Compose
771 (To_String
772 (Search.Value.Name), Name (1 .. Last));
773 Found : Boolean := False;
775 begin
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;
783 Found := True;
784 end if;
786 elsif Is_Directory (Full_Name) then
787 if Search.Value.Filter (Directory) then
788 Kind := Directory;
789 Found := True;
790 end if;
792 elsif Search.Value.Filter (Special_File) then
793 Kind := Special_File;
794 Found := True;
795 end if;
797 -- If it does, update Search and return
799 if Found then
800 Search.Value.Entry_Fetched := True;
801 Search.Value.Dir_Entry :=
802 (Is_Valid => True,
803 Simple => To_Unbounded_String (Name (1 .. Last)),
804 Full => To_Unbounded_String (Full_Name),
805 Kind => Kind);
806 exit;
807 end if;
808 end if;
809 end;
810 end if;
811 end loop;
812 end Fetch_Next_Entry;
814 -----------------
815 -- File_Exists --
816 -----------------
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);
824 begin
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;
828 end File_Exists;
830 --------------
831 -- Finalize --
832 --------------
834 procedure Finalize (Search : in out Search_Type) is
835 begin
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);
842 end if;
844 Free (Search.Value);
845 end if;
846 end Finalize;
848 ---------------
849 -- Full_Name --
850 ---------------
852 function Full_Name (Name : String) return String is
853 begin
854 -- First, the invalid case
856 if not Is_Valid_Path_Name (Name) then
857 raise Name_Error with "invalid path name """ & Name & '"';
859 else
860 -- Build the return value with lower bound 1
862 -- Use System.OS_Lib.Normalize_Pathname
864 declare
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);
871 begin
872 return Result (Value);
873 end;
874 end if;
875 end Full_Name;
877 function Full_Name (Directory_Entry : Directory_Entry_Type) return String is
878 begin
879 -- First, the invalid case
881 if not Directory_Entry.Is_Valid then
882 raise Status_Error with "invalid directory entry";
884 else
885 -- The value to return has already been computed
887 return To_String (Directory_Entry.Full);
888 end if;
889 end Full_Name;
891 --------------------
892 -- Get_Next_Entry --
893 --------------------
895 procedure Get_Next_Entry
896 (Search : in out Search_Type;
897 Directory_Entry : out Directory_Entry_Type)
899 begin
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";
904 end if;
906 -- Fetch the next entry, if needed
908 if not Search.Value.Entry_Fetched then
909 Fetch_Next_Entry (Search);
910 end if;
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";
917 else
918 -- Reset Entry_Fetched and return the entry
920 Search.Value.Entry_Fetched := False;
921 Directory_Entry := Search.Value.Dir_Entry;
922 end if;
923 end Get_Next_Entry;
925 ----------
926 -- Kind --
927 ----------
929 function Kind (Name : String) return File_Kind is
930 begin
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
942 return Directory;
944 else
945 return Special_File;
946 end if;
947 end Kind;
949 function Kind (Directory_Entry : Directory_Entry_Type) return File_Kind is
950 begin
951 -- First, the invalid case
953 if not Directory_Entry.Is_Valid then
954 raise Status_Error with "invalid directory entry";
956 else
957 -- The value to return has already be computed
959 return Directory_Entry.Kind;
960 end if;
961 end Kind;
963 -----------------------
964 -- Modification_Time --
965 -----------------------
967 function Modification_Time (Name : String) return Time is
968 Date : OS_Time;
969 Year : Year_Type;
970 Month : Month_Type;
971 Day : Day_Type;
972 Hour : Hour_Type;
973 Minute : Minute_Type;
974 Second : Second_Type;
975 Result : Time;
977 begin
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";
983 else
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.
997 if OpenVMS then
998 Result :=
999 Ada.Calendar.Time_Of
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.
1006 else
1007 Result := Time_Of (Year, Month, Day, Hour, Minute, Second, 0.0);
1008 end if;
1010 return Result;
1011 end if;
1012 end Modification_Time;
1014 function Modification_Time
1015 (Directory_Entry : Directory_Entry_Type) return Ada.Calendar.Time
1017 begin
1018 -- First, the invalid case
1020 if not Directory_Entry.Is_Valid then
1021 raise Status_Error with "invalid directory entry";
1023 else
1024 -- The value to return has already be computed
1026 return Modification_Time (To_String (Directory_Entry.Full));
1027 end if;
1028 end Modification_Time;
1030 ------------------
1031 -- More_Entries --
1032 ------------------
1034 function More_Entries (Search : Search_Type) return Boolean is
1035 begin
1036 if Search.Value = null then
1037 return False;
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);
1045 end if;
1046 end if;
1048 return Search.Value.Is_Valid;
1049 end More_Entries;
1051 ------------
1052 -- Rename --
1053 ------------
1055 procedure Rename (Old_Name, New_Name : String) is
1056 Success : Boolean;
1058 begin
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)
1069 then
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
1079 else
1080 Rename_File (Old_Name, New_Name, Success);
1082 if not Success then
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";
1094 else
1095 raise Use_Error with
1096 "file """ & Old_Name & """ could not be renamed";
1097 end if;
1098 end if;
1099 end if;
1100 end Rename;
1102 ------------
1103 -- Search --
1104 ------------
1106 procedure Search
1107 (Directory : String;
1108 Pattern : String;
1109 Filter : Filter_Type := (others => True);
1110 Process : not null access procedure
1111 (Directory_Entry : Directory_Entry_Type))
1113 Srch : Search_Type;
1114 Directory_Entry : Directory_Entry_Type;
1116 begin
1117 Start_Search (Srch, Directory, Pattern, Filter);
1118 while More_Entries (Srch) loop
1119 Get_Next_Entry (Srch, Directory_Entry);
1120 Process (Directory_Entry);
1121 end loop;
1123 End_Search (Srch);
1124 end Search;
1126 -------------------
1127 -- Set_Directory --
1128 -------------------
1130 procedure Set_Directory (Directory : String) is
1131 C_Dir_Name : constant String := Directory & ASCII.NUL;
1132 begin
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 & '"';
1144 end if;
1145 end Set_Directory;
1147 -----------------
1148 -- Simple_Name --
1149 -----------------
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 :=
1162 Strings.Fixed.Index
1163 (Path, Dir_Seps, Going => Strings.Backward);
1164 Cut_End : Natural;
1166 begin
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.
1183 begin
1184 if BN = "." or else BN = ".." then
1185 return "";
1187 elsif Has_Drive_Letter
1188 and then BN'Length > 2
1189 and then Characters.Handling.Is_Letter (BN (BN'First))
1190 and then BN (BN'First + 1) = ':'
1191 then
1192 -- We have a DOS drive letter prefix, remove it
1194 return BN (BN'First + 2 .. BN'Last);
1196 else
1197 return BN;
1198 end if;
1199 end Check_For_Standard_Dirs;
1200 end Simple_Name_Internal;
1202 -- Start of processing for Simple_Name
1204 begin
1205 -- First, the invalid case
1207 if not Is_Valid_Path_Name (Name) then
1208 raise Name_Error with "invalid path name """ & Name & '"';
1210 else
1211 -- Build the value to return with lower bound 1
1213 declare
1214 Value : constant String := Simple_Name_Internal (Name);
1215 subtype Result is String (1 .. Value'Length);
1216 begin
1217 return Result (Value);
1218 end;
1219 end if;
1220 end Simple_Name;
1222 function Simple_Name
1223 (Directory_Entry : Directory_Entry_Type) return String is
1224 begin
1225 -- First, the invalid case
1227 if not Directory_Entry.Is_Valid then
1228 raise Status_Error with "invalid directory entry";
1230 else
1231 -- The value to return has already be computed
1233 return To_String (Directory_Entry.Simple);
1234 end if;
1235 end Simple_Name;
1237 ----------
1238 -- Size --
1239 ----------
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");
1247 begin
1248 -- First, the invalid case
1250 if not Is_Regular_File (Name) then
1251 raise Name_Error with "file """ & Name & """ does not exist";
1253 else
1254 C_Name (1 .. Name'Length) := Name;
1255 C_Name (C_Name'Last) := ASCII.NUL;
1256 return File_Size (C_Size (C_Name'Address));
1257 end if;
1258 end Size;
1260 function Size (Directory_Entry : Directory_Entry_Type) return File_Size is
1261 begin
1262 -- First, the invalid case
1264 if not Directory_Entry.Is_Valid then
1265 raise Status_Error with "invalid directory entry";
1267 else
1268 -- The value to return has already be computed
1270 return Size (To_String (Directory_Entry.Full));
1271 end if;
1272 end Size;
1274 ------------------
1275 -- Start_Search --
1276 ------------------
1278 procedure Start_Search
1279 (Search : in out Search_Type;
1280 Directory : String;
1281 Pattern : String;
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;
1288 Pat : Regexp;
1289 Dir : Dir_Type_Value;
1291 begin
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) & '"';
1297 end if;
1299 -- Check the pattern
1301 begin
1302 Pat := Compile
1303 (Pattern,
1304 Glob => True,
1305 Case_Sensitive => Is_Path_Name_Case_Sensitive);
1306 exception
1307 when Error_In_Regexp =>
1308 Free (Search.Value);
1309 raise Name_Error with "invalid pattern """ & Pattern & '"';
1310 end;
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) & '"';
1317 end if;
1319 -- If needed, finalize Search
1321 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;
1334 end Start_Search;
1336 end Ada.Directories;