Daily bump.
[official-gcc.git] / gcc / ada / a-direct.adb
blobf0182c68e7a159841d06f5aae309dd1573de998a
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-2009, 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.Directories.Validity; use Ada.Directories.Validity;
35 with Ada.Strings.Maps;
36 with Ada.Strings.Fixed;
37 with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
38 with Ada.Unchecked_Conversion;
39 with Ada.Unchecked_Deallocation;
40 with Ada.Characters.Handling; use Ada.Characters.Handling;
42 with System.CRTL; use System.CRTL;
43 with System.OS_Lib; use System.OS_Lib;
44 with System.Regexp; use System.Regexp;
46 with System;
48 package body Ada.Directories is
50 Filename_Max : constant Integer := 1024;
51 -- 1024 is the value of FILENAME_MAX in stdio.h
53 type Dir_Type_Value is new System.Address;
54 -- This is the low-level address directory structure as returned by the C
55 -- opendir routine.
57 No_Dir : constant Dir_Type_Value := Dir_Type_Value (System.Null_Address);
59 Dir_Separator : constant Character;
60 pragma Import (C, Dir_Separator, "__gnat_dir_separator");
61 -- Running system default directory separator
63 Dir_Seps : constant Ada.Strings.Maps.Character_Set :=
64 Ada.Strings.Maps.To_Set ("/\");
65 -- UNIX and DOS style directory separators
67 Max_Path : Integer;
68 pragma Import (C, Max_Path, "__gnat_max_path_len");
69 -- The maximum length of a path
71 type Search_Data is record
72 Is_Valid : Boolean := False;
73 Name : Unbounded_String;
74 Pattern : Regexp;
75 Filter : Filter_Type;
76 Dir : Dir_Type_Value := No_Dir;
77 Entry_Fetched : Boolean := False;
78 Dir_Entry : Directory_Entry_Type;
79 end record;
80 -- The current state of a search
82 Empty_String : constant String := (1 .. 0 => ASCII.NUL);
83 -- Empty string, returned by function Extension when there is no extension
85 procedure Free is new Ada.Unchecked_Deallocation (Search_Data, Search_Ptr);
87 procedure Close (Dir : Dir_Type_Value);
89 function File_Exists (Name : String) return Boolean;
90 -- Returns True if the named file exists
92 procedure Fetch_Next_Entry (Search : Search_Type);
93 -- Get the next entry in a directory, setting Entry_Fetched if successful
94 -- or resetting Is_Valid if not.
96 ---------------
97 -- Base_Name --
98 ---------------
100 function Base_Name (Name : String) return String is
101 Simple : constant String := Simple_Name (Name);
102 -- Simple'First is guaranteed to be 1
104 begin
105 -- Look for the last dot in the file name and return the part of the
106 -- file name preceding this last dot. If the first dot is the first
107 -- character of the file name, the base name is the empty string.
109 for Pos in reverse Simple'Range loop
110 if Simple (Pos) = '.' then
111 return Simple (1 .. Pos - 1);
112 end if;
113 end loop;
115 -- If there is no dot, return the complete file name
117 return Simple;
118 end Base_Name;
120 -----------
121 -- Close --
122 -----------
124 procedure Close (Dir : Dir_Type_Value) is
125 Discard : Integer;
126 pragma Warnings (Off, Discard);
128 function closedir (directory : DIRs) return Integer;
129 pragma Import (C, closedir, "__gnat_closedir");
131 begin
132 Discard := closedir (DIRs (Dir));
133 end Close;
135 -------------
136 -- Compose --
137 -------------
139 function Compose
140 (Containing_Directory : String := "";
141 Name : String;
142 Extension : String := "") return String
144 Result : String (1 .. Containing_Directory'Length +
145 Name'Length + Extension'Length + 2);
146 Last : Natural;
148 begin
149 -- First, deal with the invalid cases
151 if Containing_Directory /= ""
152 and then not Is_Valid_Path_Name (Containing_Directory)
153 then
154 raise Name_Error with
155 "invalid directory path name """ & Containing_Directory & '"';
157 elsif
158 Extension'Length = 0 and then (not Is_Valid_Simple_Name (Name))
159 then
160 raise Name_Error with
161 "invalid simple name """ & Name & '"';
163 elsif Extension'Length /= 0
164 and then not Is_Valid_Simple_Name (Name & '.' & Extension)
165 then
166 raise Name_Error with
167 "invalid file name """ & Name & '.' & Extension & '"';
169 -- This is not an invalid case so build the path name
171 else
172 Last := Containing_Directory'Length;
173 Result (1 .. Last) := Containing_Directory;
175 -- Add a directory separator if needed
177 if Last /= 0 and then Result (Last) /= Dir_Separator then
178 Last := Last + 1;
179 Result (Last) := Dir_Separator;
180 end if;
182 -- Add the file name
184 Result (Last + 1 .. Last + Name'Length) := Name;
185 Last := Last + Name'Length;
187 -- If extension was specified, add dot followed by this extension
189 if Extension'Length /= 0 then
190 Last := Last + 1;
191 Result (Last) := '.';
192 Result (Last + 1 .. Last + Extension'Length) := Extension;
193 Last := Last + Extension'Length;
194 end if;
196 return Result (1 .. Last);
197 end if;
198 end Compose;
200 --------------------------
201 -- Containing_Directory --
202 --------------------------
204 function Containing_Directory (Name : String) return String is
205 begin
206 -- First, the invalid case
208 if not Is_Valid_Path_Name (Name) then
209 raise Name_Error with "invalid path name """ & Name & '"';
211 else
212 declare
213 Norm : constant String := Normalize_Pathname (Name);
214 Last_DS : constant Natural :=
215 Strings.Fixed.Index
216 (Name, Dir_Seps, Going => Strings.Backward);
218 begin
219 if Last_DS = 0 then
221 -- There is no directory separator, returns current working
222 -- directory.
224 return Current_Directory;
226 -- If Name indicates a root directory, raise Use_Error, because
227 -- it has no containing directory.
229 elsif Norm = "/"
230 or else
231 (Windows
232 and then
233 (Norm = "\"
234 or else
235 (Norm'Length = 3
236 and then Norm (Norm'Last - 1 .. Norm'Last) = ":\"
237 and then (Norm (Norm'First) in 'a' .. 'z'
238 or else Norm (Norm'First) in 'A' .. 'Z'))))
239 then
240 raise Use_Error with
241 "directory """ & Name & """ has no containing directory";
243 else
244 declare
245 Last : Positive := Last_DS - Name'First + 1;
246 Result : String (1 .. Last);
248 begin
249 Result := Name (Name'First .. Last_DS);
251 -- Remove any trailing directory separator, except as the
252 -- first character or the first character following a drive
253 -- number on Windows.
255 while Last > 1 loop
256 exit when
257 Result (Last) /= '/'
258 and then
259 Result (Last) /= Directory_Separator;
261 exit when Windows
262 and then Last = 3
263 and then Result (2) = ':'
264 and then
265 (Result (1) in 'A' .. 'Z'
266 or else
267 Result (1) in 'a' .. 'z');
269 Last := Last - 1;
270 end loop;
272 -- Special case of current directory, identified by "."
274 if Last = 1 and then Result (1) = '.' then
275 return Current_Directory;
277 -- Special case of "..": the current directory may be a root
278 -- directory.
280 elsif Last = 2 and then Result (1 .. 2) = ".." then
281 return Containing_Directory (Current_Directory);
283 else
284 return Result (1 .. Last);
285 end if;
286 end;
287 end if;
288 end;
289 end if;
290 end Containing_Directory;
292 ---------------
293 -- Copy_File --
294 ---------------
296 procedure Copy_File
297 (Source_Name : String;
298 Target_Name : String;
299 Form : String := "")
301 pragma Unreferenced (Form);
302 Success : Boolean;
304 begin
305 -- First, the invalid cases
307 if not Is_Valid_Path_Name (Source_Name) then
308 raise Name_Error with
309 "invalid source path name """ & Source_Name & '"';
311 elsif not Is_Valid_Path_Name (Target_Name) then
312 raise Name_Error with
313 "invalid target path name """ & Target_Name & '"';
315 elsif not Is_Regular_File (Source_Name) then
316 raise Name_Error with '"' & Source_Name & """ is not a file";
318 elsif Is_Directory (Target_Name) then
319 raise Use_Error with "target """ & Target_Name & """ is a directory";
321 else
322 -- The implementation uses System.OS_Lib.Copy_File, with parameters
323 -- suitable for all platforms.
325 Copy_File (Source_Name, Target_Name, Success, Overwrite, None);
327 if not Success then
328 raise Use_Error with "copy of """ & Source_Name & """ failed";
329 end if;
330 end if;
331 end Copy_File;
333 ----------------------
334 -- Create_Directory --
335 ----------------------
337 procedure Create_Directory
338 (New_Directory : String;
339 Form : String := "")
341 pragma Unreferenced (Form);
343 C_Dir_Name : constant String := New_Directory & ASCII.NUL;
345 function mkdir (Dir_Name : String) return Integer;
346 pragma Import (C, mkdir, "__gnat_mkdir");
348 begin
349 -- First, the invalid case
351 if not Is_Valid_Path_Name (New_Directory) then
352 raise Name_Error with
353 "invalid new directory path name """ & New_Directory & '"';
355 else
356 if mkdir (C_Dir_Name) /= 0 then
357 raise Use_Error with
358 "creation of new directory """ & New_Directory & """ failed";
359 end if;
360 end if;
361 end Create_Directory;
363 -----------------
364 -- Create_Path --
365 -----------------
367 procedure Create_Path
368 (New_Directory : String;
369 Form : String := "")
371 pragma Unreferenced (Form);
373 New_Dir : String (1 .. New_Directory'Length + 1);
374 Last : Positive := 1;
376 begin
377 -- First, the invalid case
379 if not Is_Valid_Path_Name (New_Directory) then
380 raise Name_Error with
381 "invalid new directory path name """ & New_Directory & '"';
383 else
384 -- Build New_Dir with a directory separator at the end, so that the
385 -- complete path will be found in the loop below.
387 New_Dir (1 .. New_Directory'Length) := New_Directory;
388 New_Dir (New_Dir'Last) := Directory_Separator;
390 -- Create, if necessary, each directory in the path
392 for J in 2 .. New_Dir'Last loop
394 -- Look for the end of an intermediate directory
396 if New_Dir (J) /= Dir_Separator and then
397 New_Dir (J) /= '/'
398 then
399 Last := J;
401 -- We have found a new intermediate directory each time we find
402 -- a first directory separator.
404 elsif New_Dir (J - 1) /= Dir_Separator and then
405 New_Dir (J - 1) /= '/'
406 then
408 -- No need to create the directory if it already exists
410 if Is_Directory (New_Dir (1 .. Last)) then
411 null;
413 -- It is an error if a file with such a name already exists
415 elsif Is_Regular_File (New_Dir (1 .. Last)) then
416 raise Use_Error with
417 "file """ & New_Dir (1 .. Last) & """ already exists";
419 else
420 Create_Directory (New_Directory => New_Dir (1 .. Last));
421 end if;
422 end if;
423 end loop;
424 end if;
425 end Create_Path;
427 -----------------------
428 -- Current_Directory --
429 -----------------------
431 function Current_Directory return String is
432 Path_Len : Natural := Max_Path;
433 Buffer : String (1 .. 1 + Max_Path + 1);
435 procedure Local_Get_Current_Dir
436 (Dir : System.Address;
437 Length : System.Address);
438 pragma Import (C, Local_Get_Current_Dir, "__gnat_get_current_dir");
440 begin
441 Local_Get_Current_Dir (Buffer'Address, Path_Len'Address);
443 declare
444 Cur : constant String := Normalize_Pathname (Buffer (1 .. Path_Len));
446 begin
447 if Cur'Length > 1 and then Cur (Cur'Last) = Dir_Separator then
448 return Cur (1 .. Cur'Last - 1);
449 else
450 return Cur;
451 end if;
452 end;
453 end Current_Directory;
455 ----------------------
456 -- Delete_Directory --
457 ----------------------
459 procedure Delete_Directory (Directory : String) is
460 begin
461 -- First, the invalid cases
463 if not Is_Valid_Path_Name (Directory) then
464 raise Name_Error with
465 "invalid directory path name """ & Directory & '"';
467 elsif not Is_Directory (Directory) then
468 raise Name_Error with '"' & Directory & """ not a directory";
470 else
471 declare
472 C_Dir_Name : constant String := Directory & ASCII.NUL;
474 begin
475 if rmdir (C_Dir_Name) /= 0 then
476 raise Use_Error with
477 "deletion of directory """ & Directory & """ failed";
478 end if;
479 end;
480 end if;
481 end Delete_Directory;
483 -----------------
484 -- Delete_File --
485 -----------------
487 procedure Delete_File (Name : String) is
488 Success : Boolean;
490 begin
491 -- First, the invalid cases
493 if not Is_Valid_Path_Name (Name) then
494 raise Name_Error with "invalid path name """ & Name & '"';
496 elsif not Is_Regular_File (Name) then
497 raise Name_Error with "file """ & Name & """ does not exist";
499 else
500 -- The implementation uses System.OS_Lib.Delete_File
502 Delete_File (Name, Success);
504 if not Success then
505 raise Use_Error with "file """ & Name & """ could not be deleted";
506 end if;
507 end if;
508 end Delete_File;
510 -----------------
511 -- Delete_Tree --
512 -----------------
514 procedure Delete_Tree (Directory : String) is
515 Current_Dir : constant String := Current_Directory;
516 Search : Search_Type;
517 Dir_Ent : Directory_Entry_Type;
518 begin
519 -- First, the invalid cases
521 if not Is_Valid_Path_Name (Directory) then
522 raise Name_Error with
523 "invalid directory path name """ & Directory & '"';
525 elsif not Is_Directory (Directory) then
526 raise Name_Error with '"' & Directory & """ not a directory";
528 else
529 Set_Directory (Directory);
530 Start_Search (Search, Directory => ".", Pattern => "");
532 while More_Entries (Search) loop
533 Get_Next_Entry (Search, Dir_Ent);
535 declare
536 File_Name : constant String := Simple_Name (Dir_Ent);
538 begin
539 if System.OS_Lib.Is_Directory (File_Name) then
540 if File_Name /= "." and then File_Name /= ".." then
541 Delete_Tree (File_Name);
542 end if;
544 else
545 Delete_File (File_Name);
546 end if;
547 end;
548 end loop;
550 Set_Directory (Current_Dir);
551 End_Search (Search);
553 declare
554 C_Dir_Name : constant String := Directory & ASCII.NUL;
556 begin
557 if rmdir (C_Dir_Name) /= 0 then
558 raise Use_Error with
559 "directory tree rooted at """ &
560 Directory & """ could not be deleted";
561 end if;
562 end;
563 end if;
564 end Delete_Tree;
566 ------------
567 -- Exists --
568 ------------
570 function Exists (Name : String) return Boolean is
571 begin
572 -- First, the invalid case
574 if not Is_Valid_Path_Name (Name) then
575 raise Name_Error with "invalid path name """ & Name & '"';
577 else
578 -- The implementation is in File_Exists
580 return File_Exists (Name);
581 end if;
582 end Exists;
584 ---------------
585 -- Extension --
586 ---------------
588 function Extension (Name : String) return String is
589 begin
590 -- First, the invalid case
592 if not Is_Valid_Path_Name (Name) then
593 raise Name_Error with "invalid path name """ & Name & '"';
595 else
596 -- Look for first dot that is not followed by a directory separator
598 for Pos in reverse Name'Range loop
600 -- If a directory separator is found before a dot, there is no
601 -- extension.
603 if Name (Pos) = Dir_Separator then
604 return Empty_String;
606 elsif Name (Pos) = '.' then
608 -- We found a dot, build the return value with lower bound 1
610 declare
611 subtype Result_Type is String (1 .. Name'Last - Pos);
612 begin
613 return Result_Type (Name (Pos + 1 .. Name'Last));
614 end;
615 end if;
616 end loop;
618 -- No dot were found, there is no extension
620 return Empty_String;
621 end if;
622 end Extension;
624 ----------------------
625 -- Fetch_Next_Entry --
626 ----------------------
628 procedure Fetch_Next_Entry (Search : Search_Type) is
629 Name : String (1 .. 255);
630 Last : Natural;
632 Kind : File_Kind := Ordinary_File;
633 -- Initialized to avoid a compilation warning
635 Filename_Addr : System.Address;
636 Filename_Len : aliased Integer;
638 Buffer : array (0 .. Filename_Max + 12) of Character;
639 -- 12 is the size of the dirent structure (see dirent.h), without the
640 -- field for the filename.
642 function readdir_gnat
643 (Directory : System.Address;
644 Buffer : System.Address;
645 Last : not null access Integer) return System.Address;
646 pragma Import (C, readdir_gnat, "__gnat_readdir");
648 use System;
650 begin
651 -- Search.Value.Is_Valid is always True when Fetch_Next_Entry is called
653 loop
654 Filename_Addr :=
655 readdir_gnat
656 (System.Address (Search.Value.Dir),
657 Buffer'Address,
658 Filename_Len'Access);
660 -- If no matching entry is found, set Is_Valid to False
662 if Filename_Addr = System.Null_Address then
663 Search.Value.Is_Valid := False;
664 exit;
665 end if;
667 declare
668 subtype Path_String is String (1 .. Filename_Len);
669 type Path_String_Access is access Path_String;
671 function Address_To_Access is new
672 Ada.Unchecked_Conversion
673 (Source => Address,
674 Target => Path_String_Access);
676 Path_Access : constant Path_String_Access :=
677 Address_To_Access (Filename_Addr);
679 begin
680 Last := Filename_Len;
681 Name (1 .. Last) := Path_Access.all;
682 end;
684 -- Check if the entry matches the pattern
686 if Match (Name (1 .. Last), Search.Value.Pattern) then
687 declare
688 Full_Name : constant String :=
689 Compose
690 (To_String
691 (Search.Value.Name), Name (1 .. Last));
692 Found : Boolean := False;
694 begin
695 if File_Exists (Full_Name) then
697 -- Now check if the file kind matches the filter
699 if Is_Regular_File (Full_Name) then
700 if Search.Value.Filter (Ordinary_File) then
701 Kind := Ordinary_File;
702 Found := True;
703 end if;
705 elsif Is_Directory (Full_Name) then
706 if Search.Value.Filter (Directory) then
707 Kind := Directory;
708 Found := True;
709 end if;
711 elsif Search.Value.Filter (Special_File) then
712 Kind := Special_File;
713 Found := True;
714 end if;
716 -- If it does, update Search and return
718 if Found then
719 Search.Value.Entry_Fetched := True;
720 Search.Value.Dir_Entry :=
721 (Is_Valid => True,
722 Simple => To_Unbounded_String (Name (1 .. Last)),
723 Full => To_Unbounded_String (Full_Name),
724 Kind => Kind);
725 exit;
726 end if;
727 end if;
728 end;
729 end if;
730 end loop;
731 end Fetch_Next_Entry;
733 -----------------
734 -- File_Exists --
735 -----------------
737 function File_Exists (Name : String) return Boolean is
738 function C_File_Exists (A : System.Address) return Integer;
739 pragma Import (C, C_File_Exists, "__gnat_file_exists");
741 C_Name : String (1 .. Name'Length + 1);
743 begin
744 C_Name (1 .. Name'Length) := Name;
745 C_Name (C_Name'Last) := ASCII.NUL;
746 return C_File_Exists (C_Name (1)'Address) = 1;
747 end File_Exists;
749 --------------
750 -- Finalize --
751 --------------
753 procedure Finalize (Search : in out Search_Type) is
754 begin
755 if Search.Value /= null then
757 -- Close the directory, if one is open
759 if Search.Value.Dir /= No_Dir then
760 Close (Search.Value.Dir);
761 end if;
763 Free (Search.Value);
764 end if;
765 end Finalize;
767 ---------------
768 -- Full_Name --
769 ---------------
771 function Full_Name (Name : String) return String is
772 begin
773 -- First, the invalid case
775 if not Is_Valid_Path_Name (Name) then
776 raise Name_Error with "invalid path name """ & Name & '"';
778 else
779 -- Build the return value with lower bound 1
781 -- Use System.OS_Lib.Normalize_Pathname
783 declare
784 Value : constant String := Normalize_Pathname (Name);
785 subtype Result is String (1 .. Value'Length);
786 begin
787 return Result (Value);
788 end;
789 end if;
790 end Full_Name;
792 function Full_Name (Directory_Entry : Directory_Entry_Type) return String is
793 begin
794 -- First, the invalid case
796 if not Directory_Entry.Is_Valid then
797 raise Status_Error with "invalid directory entry";
799 else
800 -- The value to return has already been computed
802 return To_String (Directory_Entry.Full);
803 end if;
804 end Full_Name;
806 --------------------
807 -- Get_Next_Entry --
808 --------------------
810 procedure Get_Next_Entry
811 (Search : in out Search_Type;
812 Directory_Entry : out Directory_Entry_Type)
814 begin
815 -- First, the invalid case
817 if Search.Value = null or else not Search.Value.Is_Valid then
818 raise Status_Error with "invalid search";
819 end if;
821 -- Fetch the next entry, if needed
823 if not Search.Value.Entry_Fetched then
824 Fetch_Next_Entry (Search);
825 end if;
827 -- It is an error if no valid entry is found
829 if not Search.Value.Is_Valid then
830 raise Status_Error with "no next entry";
832 else
833 -- Reset Entry_Fetched and return the entry
835 Search.Value.Entry_Fetched := False;
836 Directory_Entry := Search.Value.Dir_Entry;
837 end if;
838 end Get_Next_Entry;
840 ----------
841 -- Kind --
842 ----------
844 function Kind (Name : String) return File_Kind is
845 begin
846 -- First, the invalid case
848 if not File_Exists (Name) then
849 raise Name_Error with "file """ & Name & """ does not exist";
851 elsif Is_Regular_File (Name) then
852 return Ordinary_File;
854 elsif Is_Directory (Name) then
855 return Directory;
857 else
858 return Special_File;
859 end if;
860 end Kind;
862 function Kind (Directory_Entry : Directory_Entry_Type) return File_Kind is
863 begin
864 -- First, the invalid case
866 if not Directory_Entry.Is_Valid then
867 raise Status_Error with "invalid directory entry";
869 else
870 -- The value to return has already be computed
872 return Directory_Entry.Kind;
873 end if;
874 end Kind;
876 -----------------------
877 -- Modification_Time --
878 -----------------------
880 function Modification_Time (Name : String) return Time is
881 Date : OS_Time;
882 Year : Year_Type;
883 Month : Month_Type;
884 Day : Day_Type;
885 Hour : Hour_Type;
886 Minute : Minute_Type;
887 Second : Second_Type;
888 Result : Time;
890 begin
891 -- First, the invalid cases
893 if not (Is_Regular_File (Name) or else Is_Directory (Name)) then
894 raise Name_Error with '"' & Name & """ not a file or directory";
896 else
897 Date := File_Time_Stamp (Name);
899 -- Break down the time stamp into its constituents relative to GMT.
900 -- This version of Split does not recognize leap seconds or buffer
901 -- space for time zone processing.
903 GM_Split (Date, Year, Month, Day, Hour, Minute, Second);
905 -- On OpenVMS, the resulting time value must be in the local time
906 -- zone. Ada.Calendar.Time_Of is exactly what we need. Note that
907 -- in both cases, the sub seconds are set to zero (0.0) because the
908 -- time stamp does not store them in its value.
910 if OpenVMS then
911 Result :=
912 Ada.Calendar.Time_Of
913 (Year, Month, Day, Seconds_Of (Hour, Minute, Second, 0.0));
915 -- On Unix and Windows, the result must be in GMT. Ada.Calendar.
916 -- Formatting.Time_Of with default time zone of zero (0) is the
917 -- routine of choice.
919 else
920 Result := Time_Of (Year, Month, Day, Hour, Minute, Second, 0.0);
921 end if;
923 return Result;
924 end if;
925 end Modification_Time;
927 function Modification_Time
928 (Directory_Entry : Directory_Entry_Type) return Ada.Calendar.Time
930 begin
931 -- First, the invalid case
933 if not Directory_Entry.Is_Valid then
934 raise Status_Error with "invalid directory entry";
936 else
937 -- The value to return has already be computed
939 return Modification_Time (To_String (Directory_Entry.Full));
940 end if;
941 end Modification_Time;
943 ------------------
944 -- More_Entries --
945 ------------------
947 function More_Entries (Search : Search_Type) return Boolean is
948 begin
949 if Search.Value = null then
950 return False;
952 elsif Search.Value.Is_Valid then
954 -- Fetch the next entry, if needed
956 if not Search.Value.Entry_Fetched then
957 Fetch_Next_Entry (Search);
958 end if;
959 end if;
961 return Search.Value.Is_Valid;
962 end More_Entries;
964 ------------
965 -- Rename --
966 ------------
968 procedure Rename (Old_Name, New_Name : String) is
969 Success : Boolean;
971 begin
972 -- First, the invalid cases
974 if not Is_Valid_Path_Name (Old_Name) then
975 raise Name_Error with "invalid old path name """ & Old_Name & '"';
977 elsif not Is_Valid_Path_Name (New_Name) then
978 raise Name_Error with "invalid new path name """ & New_Name & '"';
980 elsif not Is_Regular_File (Old_Name)
981 and then not Is_Directory (Old_Name)
982 then
983 raise Name_Error with "old file """ & Old_Name & """ does not exist";
985 elsif Is_Regular_File (New_Name) or else Is_Directory (New_Name) then
986 raise Use_Error with
987 "new name """ & New_Name
988 & """ designates a file that already exists";
990 else
991 -- The implementation uses System.OS_Lib.Rename_File
993 Rename_File (Old_Name, New_Name, Success);
995 if not Success then
996 raise Use_Error with
997 "file """ & Old_Name & """ could not be renamed";
998 end if;
999 end if;
1000 end Rename;
1002 ------------
1003 -- Search --
1004 ------------
1006 procedure Search
1007 (Directory : String;
1008 Pattern : String;
1009 Filter : Filter_Type := (others => True);
1010 Process : not null access procedure
1011 (Directory_Entry : Directory_Entry_Type))
1013 Srch : Search_Type;
1014 Directory_Entry : Directory_Entry_Type;
1016 begin
1017 Start_Search (Srch, Directory, Pattern, Filter);
1019 while More_Entries (Srch) loop
1020 Get_Next_Entry (Srch, Directory_Entry);
1021 Process (Directory_Entry);
1022 end loop;
1024 End_Search (Srch);
1025 end Search;
1027 -------------------
1028 -- Set_Directory --
1029 -------------------
1031 procedure Set_Directory (Directory : String) is
1032 C_Dir_Name : constant String := Directory & ASCII.NUL;
1033 begin
1034 if not Is_Valid_Path_Name (Directory) then
1035 raise Name_Error with
1036 "invalid directory path name & """ & Directory & '"';
1038 elsif not Is_Directory (Directory) then
1039 raise Name_Error with
1040 "directory """ & Directory & """ does not exist";
1042 elsif chdir (C_Dir_Name) /= 0 then
1043 raise Name_Error with
1044 "could not set to designated directory """ & Directory & '"';
1045 end if;
1046 end Set_Directory;
1048 -----------------
1049 -- Simple_Name --
1050 -----------------
1052 function Simple_Name (Name : String) return String is
1054 function Simple_Name_Internal (Path : String) return String;
1055 -- This function does the job
1057 --------------------------
1058 -- Simple_Name_Internal --
1059 --------------------------
1061 function Simple_Name_Internal (Path : String) return String is
1062 Cut_Start : Natural :=
1063 Strings.Fixed.Index
1064 (Path, Dir_Seps, Going => Strings.Backward);
1065 Cut_End : Natural;
1067 begin
1068 -- Cut_Start pointS to the first simple name character
1070 Cut_Start := (if Cut_Start = 0 then Path'First else Cut_Start + 1);
1072 -- Cut_End point to the last simple name character
1074 Cut_End := Path'Last;
1076 Check_For_Standard_Dirs : declare
1077 BN : constant String := Path (Cut_Start .. Cut_End);
1078 Has_Drive_Letter : constant Boolean :=
1079 System.OS_Lib.Path_Separator /= ':';
1080 -- If Path separator is not ':' then we are on a DOS based OS
1081 -- where this character is used as a drive letter separator.
1083 begin
1084 if BN = "." or else BN = ".." then
1085 return "";
1087 elsif Has_Drive_Letter
1088 and then BN'Length > 2
1089 and then Characters.Handling.Is_Letter (BN (BN'First))
1090 and then BN (BN'First + 1) = ':'
1091 then
1092 -- We have a DOS drive letter prefix, remove it
1094 return BN (BN'First + 2 .. BN'Last);
1096 else
1097 return BN;
1098 end if;
1099 end Check_For_Standard_Dirs;
1100 end Simple_Name_Internal;
1102 -- Start of processing for Simple_Name
1104 begin
1105 -- First, the invalid case
1107 if not Is_Valid_Path_Name (Name) then
1108 raise Name_Error with "invalid path name """ & Name & '"';
1110 else
1111 -- Build the value to return with lower bound 1
1113 declare
1114 Value : constant String := Simple_Name_Internal (Name);
1115 subtype Result is String (1 .. Value'Length);
1116 begin
1117 return Result (Value);
1118 end;
1119 end if;
1120 end Simple_Name;
1122 function Simple_Name
1123 (Directory_Entry : Directory_Entry_Type) return String is
1124 begin
1125 -- First, the invalid case
1127 if not Directory_Entry.Is_Valid then
1128 raise Status_Error with "invalid directory entry";
1130 else
1131 -- The value to return has already be computed
1133 return To_String (Directory_Entry.Simple);
1134 end if;
1135 end Simple_Name;
1137 ----------
1138 -- Size --
1139 ----------
1141 function Size (Name : String) return File_Size is
1142 C_Name : String (1 .. Name'Length + 1);
1144 function C_Size (Name : System.Address) return Long_Integer;
1145 pragma Import (C, C_Size, "__gnat_named_file_length");
1147 begin
1148 -- First, the invalid case
1150 if not Is_Regular_File (Name) then
1151 raise Name_Error with "file """ & Name & """ does not exist";
1153 else
1154 C_Name (1 .. Name'Length) := Name;
1155 C_Name (C_Name'Last) := ASCII.NUL;
1156 return File_Size (C_Size (C_Name'Address));
1157 end if;
1158 end Size;
1160 function Size (Directory_Entry : Directory_Entry_Type) return File_Size is
1161 begin
1162 -- First, the invalid case
1164 if not Directory_Entry.Is_Valid then
1165 raise Status_Error with "invalid directory entry";
1167 else
1168 -- The value to return has already be computed
1170 return Size (To_String (Directory_Entry.Full));
1171 end if;
1172 end Size;
1174 ------------------
1175 -- Start_Search --
1176 ------------------
1178 procedure Start_Search
1179 (Search : in out Search_Type;
1180 Directory : String;
1181 Pattern : String;
1182 Filter : Filter_Type := (others => True))
1184 function opendir (file_name : String) return DIRs;
1185 pragma Import (C, opendir, "__gnat_opendir");
1187 C_File_Name : constant String := Directory & ASCII.NUL;
1188 Pat : Regexp;
1189 Dir : Dir_Type_Value;
1191 begin
1192 -- First, the invalid case Name_Error
1194 if not Is_Directory (Directory) then
1195 raise Name_Error with
1196 "unknown directory """ & Simple_Name (Directory) & '"';
1197 end if;
1199 -- Check the pattern
1201 begin
1202 Pat := Compile
1203 (Pattern,
1204 Glob => True,
1205 Case_Sensitive => Is_Path_Name_Case_Sensitive);
1206 exception
1207 when Error_In_Regexp =>
1208 Free (Search.Value);
1209 raise Name_Error with "invalid pattern """ & Pattern & '"';
1210 end;
1212 Dir := Dir_Type_Value (opendir (C_File_Name));
1214 if Dir = No_Dir then
1215 raise Use_Error with
1216 "unreadable directory """ & Simple_Name (Directory) & '"';
1217 end if;
1219 -- If needed, finalize Search
1221 Finalize (Search);
1223 -- Allocate the default data
1225 Search.Value := new Search_Data;
1227 -- Initialize some Search components
1229 Search.Value.Filter := Filter;
1230 Search.Value.Name := To_Unbounded_String (Full_Name (Directory));
1231 Search.Value.Pattern := Pat;
1232 Search.Value.Dir := Dir;
1233 Search.Value.Is_Valid := True;
1234 end Start_Search;
1236 end Ada.Directories;