Debian Codegraph @

gcc/ada/libgnat/g-dirope.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- G N A T . D I R E C T O R Y _ O P E R A T I O N S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1998-2025, AdaCore --
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 ------------------------------------------------------------------------------
31
32 with Ada.IO_Exceptions;
33 with Ada.Characters.Handling;
34 with Ada.Strings.Fixed;
35
36 with Ada.Unchecked_Deallocation;
37
38 with System; use System;
39 with System.CRTL; use System.CRTL;
40
41 with GNAT.OS_Lib;
42
43 package body GNAT.Directory_Operations is
44
45 use Ada;
46
47 Filename_Max : constant Integer := 1024;
48 -- 1024 is the value of FILENAME_MAX in stdio.h
49
50 procedure Free is new
51 Ada.Unchecked_Deallocation (Dir_Type_Value, Dir_Type);
52
53 On_Windows : constant Boolean := GNAT.OS_Lib.Directory_Separator = '\';
54 -- An indication that we are on Windows. Used in Get_Current_Dir, to
55 -- deal with drive letters in the beginning of absolute paths.
56
57 ---------------
58 -- Base_Name --
59 ---------------
60
61 function Base_Name
62 (Path : Path_Name;
63 Suffix : String := "") return String
64 is
65 function Get_File_Names_Case_Sensitive return Integer;
66 pragma Import
67 (C, Get_File_Names_Case_Sensitive,
68 "__gnat_get_file_names_case_sensitive");
69
70 Case_Sensitive_File_Name : constant Boolean :=
71 Get_File_Names_Case_Sensitive = 1;
72
73 function Basename
74 (Path : Path_Name;
75 Suffix : String := "") return String;
76 -- This function does the job. The only difference between Basename
77 -- and Base_Name (the parent function) is that the former is case
78 -- sensitive, while the latter is not. Path and Suffix are adjusted
79 -- appropriately before calling Basename under platforms where the
80 -- file system is not case sensitive.
81
82 --------------
83 -- Basename --
84 --------------
85
86 function Basename
87 (Path : Path_Name;
88 Suffix : String := "") return String
89 is
90 Cut_Start : Natural :=
91 Strings.Fixed.Index
92 (Path, Dir_Seps, Going => Strings.Backward);
93 Cut_End : Natural;
94
95 begin
96 -- Cut_Start point to the first basename character
97
98 Cut_Start := (if Cut_Start = 0 then Path'First else Cut_Start + 1);
99
100 -- Cut_End point to the last basename character
101
102 Cut_End := Path'Last;
103
104 -- If basename ends with Suffix, adjust Cut_End
105
106 if Suffix /= ""
107 and then Path (Path'Last - Suffix'Length + 1 .. Cut_End) = Suffix
108 then
109 Cut_End := Path'Last - Suffix'Length;
110 end if;
111
112 Check_For_Standard_Dirs : declare
113 Offset : constant Integer := Path'First - Base_Name.Path'First;
114 BN : constant String :=
115 Base_Name.Path (Cut_Start - Offset .. Cut_End - Offset);
116 -- Here we use Base_Name.Path to keep the original casing
117
118 Has_Drive_Letter : constant Boolean :=
119 OS_Lib.Path_Separator /= ':';
120 -- If Path separator is not ':' then we are on a DOS based OS
121 -- where this character is used as a drive letter separator.
122
123 begin
124 if BN = "." or else BN = ".." then
125 return "";
126
127 elsif Has_Drive_Letter
128 and then BN'Length > 2
129 and then Characters.Handling.Is_Letter (BN (BN'First))
130 and then BN (BN'First + 1) = ':'
131 then
132 -- We have a DOS drive letter prefix, remove it
133
134 return BN (BN'First + 2 .. BN'Last);
135
136 else
137 return BN;
138 end if;
139 end Check_For_Standard_Dirs;
140 end Basename;
141
142 -- Start of processing for Base_Name
143
144 begin
145 if Path'Length <= Suffix'Length then
146 return Path;
147 end if;
148
149 if Case_Sensitive_File_Name then
150 return Basename (Path, Suffix);
151 else
152 return Basename
153 (Characters.Handling.To_Lower (Path),
154 Characters.Handling.To_Lower (Suffix));
155 end if;
156 end Base_Name;
157
158 ----------------
159 -- Change_Dir --
160 ----------------
161
162 procedure Change_Dir (Dir_Name : Dir_Name_Str) is
163 C_Dir_Name : constant String := Dir_Name & ASCII.NUL;
164 begin
165 if chdir (C_Dir_Name) /= 0 then
166 raise Directory_Error;
167 end if;
168 end Change_Dir;
169
170 -----------
171 -- Close --
172 -----------
173
174 procedure Close (Dir : in out Dir_Type) is
175 Discard : Integer;
176 pragma Warnings (Off, Discard);
177
178 function closedir (directory : DIRs) return Integer;
179 pragma Import (C, closedir, "__gnat_closedir");
180
181 begin
182 if not Is_Open (Dir) then
183 raise Directory_Error;
184 end if;
185
186 Discard := closedir (DIRs (Dir.all));
187 Free (Dir);
188 end Close;
189
190 --------------
191 -- Dir_Name --
192 --------------
193
194 function Dir_Name (Path : Path_Name) return Dir_Name_Str is
195 Last_DS : constant Natural :=
196 Strings.Fixed.Index
197 (Path, Dir_Seps, Going => Strings.Backward);
198
199 begin
200 if Last_DS = 0 then
201
202 -- There is no directory separator, returns current working directory
203
204 return "." & Dir_Separator;
205
206 else
207 return Path (Path'First .. Last_DS);
208 end if;
209 end Dir_Name;
210
211 -----------------
212 -- Expand_Path --
213 -----------------
214
215 function Expand_Path
216 (Path : Path_Name;
217 Mode : Environment_Style := System_Default) return Path_Name
218 is
219 Environment_Variable_Char : Character;
220 pragma Import (C, Environment_Variable_Char, "__gnat_environment_char");
221
222 Result : OS_Lib.String_Access := new String (1 .. 200);
223 Result_Last : Natural := 0;
224
225 procedure Append (C : Character);
226 procedure Append (S : String);
227 -- Append to Result
228
229 procedure Double_Result_Size;
230 -- Reallocate Result, doubling its size
231
232 function Is_Var_Prefix (C : Character) return Boolean;
233 pragma Inline (Is_Var_Prefix);
234
235 procedure Read (K : in out Positive);
236 -- Update Result while reading current Path starting at position K. If
237 -- a variable is found, call Var below.
238
239 procedure Var (K : in out Positive);
240 -- Translate variable name starting at position K with the associated
241 -- environment value.
242
243 ------------
244 -- Append --
245 ------------
246
247 procedure Append (C : Character) is
248 begin
249 if Result_Last = Result'Last then
250 Double_Result_Size;
251 end if;
252
253 Result_Last := Result_Last + 1;
254 Result (Result_Last) := C;
255 end Append;
256
257 procedure Append (S : String) is
258 begin
259 while Result_Last + S'Length - 1 > Result'Last loop
260 Double_Result_Size;
261 end loop;
262
263 Result (Result_Last + 1 .. Result_Last + S'Length) := S;
264 Result_Last := Result_Last + S'Length;
265 end Append;
266
267 ------------------------
268 -- Double_Result_Size --
269 ------------------------
270
271 procedure Double_Result_Size is
272 New_Result : constant OS_Lib.String_Access :=
273 new String (1 .. 2 * Result'Last);
274 begin
275 New_Result (1 .. Result_Last) := Result (1 .. Result_Last);
276 OS_Lib.Free (Result);
277 Result := New_Result;
278 end Double_Result_Size;
279
280 -------------------
281 -- Is_Var_Prefix --
282 -------------------
283
284 function Is_Var_Prefix (C : Character) return Boolean is
285 begin
286 return (C = Environment_Variable_Char and then Mode = System_Default)
287 or else
288 (C = '$' and then (Mode = UNIX or else Mode = Both))
289 or else
290 (C = '%' and then (Mode = DOS or else Mode = Both));
291 end Is_Var_Prefix;
292
293 ----------
294 -- Read --
295 ----------
296
297 procedure Read (K : in out Positive) is
298 P : Character;
299
300 begin
301 For_All_Characters : loop
302 if Is_Var_Prefix (Path (K)) then
303 P := Path (K);
304
305 -- Could be a variable
306
307 if K < Path'Last then
308 if Path (K + 1) = P then
309
310 -- Not a variable after all, this is a double $ or %,
311 -- just insert one in the result string.
312
313 Append (P);
314 K := K + 1;
315
316 else
317 -- Let's parse the variable
318
319 Var (K);
320 end if;
321
322 else
323 -- We have an ending $ or % sign
324
325 Append (P);
326 end if;
327
328 else
329 -- This is a standard character, just add it to the result
330
331 Append (Path (K));
332 end if;
333
334 -- Skip to next character
335
336 K := K + 1;
337
338 exit For_All_Characters when K > Path'Last;
339 end loop For_All_Characters;
340 end Read;
341
342 ---------
343 -- Var --
344 ---------
345
346 procedure Var (K : in out Positive) is
347 P : constant Character := Path (K);
348 T : Character;
349 E : Positive;
350
351 begin
352 K := K + 1;
353
354 pragma Annotate (CodePeer, Modified, P);
355
356 if P = '%' or else Path (K) = '{' then
357
358 -- Set terminator character
359
360 if P = '%' then
361 T := '%';
362 else
363 T := '}';
364 K := K + 1;
365 end if;
366
367 -- Look for terminator character, k point to the first character
368 -- for the variable name.
369
370 E := K;
371
372 loop
373 E := E + 1;
374 exit when Path (E) = T or else E = Path'Last;
375 end loop;
376
377 if Path (E) = T then
378
379 -- OK found, translate with environment value
380
381 declare
382 Env : OS_Lib.String_Access :=
383 OS_Lib.Getenv (Path (K .. E - 1));
384
385 begin
386 Append (Env.all);
387 OS_Lib.Free (Env);
388 end;
389
390 else
391 -- No terminator character, not a variable after all or a
392 -- syntax error, ignore it, insert string as-is.
393
394 Append (P); -- Add prefix character
395
396 if T = '}' then -- If we were looking for curly bracket
397 Append ('{'); -- terminator, add the curly bracket
398 end if;
399
400 Append (Path (K .. E));
401 end if;
402
403 else
404 -- The variable name is everything from current position to first
405 -- non letter/digit character.
406
407 E := K;
408
409 -- Check that first character is a letter
410
411 if Characters.Handling.Is_Letter (Path (E)) then
412 E := E + 1;
413
414 Var_Name : loop
415 exit Var_Name when E > Path'Last;
416
417 if Characters.Handling.Is_Letter (Path (E))
418 or else Characters.Handling.Is_Digit (Path (E))
419 then
420 E := E + 1;
421 else
422 exit Var_Name;
423 end if;
424 end loop Var_Name;
425
426 E := E - 1;
427
428 declare
429 Env : OS_Lib.String_Access := OS_Lib.Getenv (Path (K .. E));
430
431 begin
432 Append (Env.all);
433 OS_Lib.Free (Env);
434 end;
435
436 else
437 -- This is not a variable after all
438
439 Append ('$');
440 Append (Path (E));
441 end if;
442
443 end if;
444
445 K := E;
446 end Var;
447
448 -- Start of processing for Expand_Path
449
450 begin
451 declare
452 K : Positive := Path'First;
453
454 begin
455 Read (K);
456
457 declare
458 Returned_Value : constant String := Result (1 .. Result_Last);
459
460 begin
461 OS_Lib.Free (Result);
462 return Returned_Value;
463 end;
464 end;
465 end Expand_Path;
466
467 --------------------
468 -- File_Extension --
469 --------------------
470
471 function File_Extension (Path : Path_Name) return String is
472 First : Natural :=
473 Strings.Fixed.Index
474 (Path, Dir_Seps, Going => Strings.Backward);
475
476 Dot : Natural;
477
478 begin
479 if First = 0 then
480 First := Path'First;
481 end if;
482
483 Dot := Strings.Fixed.Index (Path (First .. Path'Last),
484 ".",
485 Going => Strings.Backward);
486
487 if Dot = 0 or else Dot = Path'Last then
488 return "";
489 else
490 return Path (Dot .. Path'Last);
491 end if;
492 end File_Extension;
493
494 ---------------
495 -- File_Name --
496 ---------------
497
498 function File_Name (Path : Path_Name) return String is
499 begin
500 return Base_Name (Path);
501 end File_Name;
502
503 ---------------------
504 -- Format_Pathname --
505 ---------------------
506
507 function Format_Pathname
508 (Path : Path_Name;
509 Style : Path_Style := System_Default) return String
510 is
511 N_Path : String := Path;
512 K : Positive := N_Path'First;
513 Prev_Dirsep : Boolean := False;
514
515 begin
516 if Dir_Separator = '\'
517 and then Path'Length > 1
518 and then Path (K .. K + 1) = "\\"
519 then
520 if Style = UNIX then
521 N_Path (K .. K + 1) := "//";
522 end if;
523
524 K := K + 2;
525 end if;
526
527 for J in K .. Path'Last loop
528 if Strings.Maps.Is_In (Path (J), Dir_Seps) then
529 if not Prev_Dirsep then
530 case Style is
531 when UNIX => N_Path (K) := '/';
532 when DOS => N_Path (K) := '\';
533 when System_Default => N_Path (K) := Dir_Separator;
534 end case;
535
536 K := K + 1;
537 end if;
538
539 Prev_Dirsep := True;
540
541 else
542 N_Path (K) := Path (J);
543 K := K + 1;
544 Prev_Dirsep := False;
545 end if;
546 end loop;
547
548 return N_Path (N_Path'First .. K - 1);
549 end Format_Pathname;
550
551 ---------------------
552 -- Get_Current_Dir --
553 ---------------------
554
555 Max_Path : Integer;
556 pragma Import (C, Max_Path, "__gnat_max_path_len");
557
558 function Get_Current_Dir return Dir_Name_Str is
559 Current_Dir : String (1 .. Max_Path + 1);
560 Last : Natural;
561 begin
562 Get_Current_Dir (Current_Dir, Last);
563 return Current_Dir (1 .. Last);
564 end Get_Current_Dir;
565
566 procedure Get_Current_Dir (Dir : out Dir_Name_Str; Last : out Natural) is
567 Path_Len : Natural := Max_Path;
568 Buffer : String (Dir'First .. Dir'First + Max_Path + 1);
569
570 procedure Local_Get_Current_Dir
571 (Dir : System.Address;
572 Length : System.Address);
573 pragma Import (C, Local_Get_Current_Dir, "__gnat_get_current_dir");
574
575 begin
576 Local_Get_Current_Dir (Buffer'Address, Path_Len'Address);
577
578 if Path_Len = 0 then
579 raise Ada.IO_Exceptions.Use_Error
580 with "current directory does not exist";
581 end if;
582
583 Last :=
584 (if Dir'Length > Path_Len then Dir'First + Path_Len - 1 else Dir'Last);
585
586 Dir (Buffer'First .. Last) := Buffer (Buffer'First .. Last);
587
588 -- By default, the drive letter on Windows is in upper case
589
590 if On_Windows and then Last > Dir'First and then
591 Dir (Dir'First + 1) = ':'
592 then
593 Dir (Dir'First) :=
594 Ada.Characters.Handling.To_Upper (Dir (Dir'First));
595 end if;
596 end Get_Current_Dir;
597
598 -------------
599 -- Is_Open --
600 -------------
601
602 function Is_Open (Dir : Dir_Type) return Boolean is
603 begin
604 return Dir /= Null_Dir
605 and then System.Address (Dir.all) /= System.Null_Address;
606 end Is_Open;
607
608 --------------
609 -- Make_Dir --
610 --------------
611
612 procedure Make_Dir (Dir_Name : Dir_Name_Str) is
613 C_Dir_Name : constant String := Dir_Name & ASCII.NUL;
614 begin
615 if CRTL.mkdir (C_Dir_Name, Unspecified) /= 0 then
616 raise Directory_Error;
617 end if;
618 end Make_Dir;
619
620 ----------
621 -- Open --
622 ----------
623
624 procedure Open
625 (Dir : out Dir_Type;
626 Dir_Name : Dir_Name_Str)
627 is
628 function opendir (file_name : String) return DIRs;
629 pragma Import (C, opendir, "__gnat_opendir");
630
631 C_File_Name : constant String := Dir_Name & ASCII.NUL;
632
633 begin
634 Dir := new Dir_Type_Value'(Dir_Type_Value (opendir (C_File_Name)));
635
636 if not Is_Open (Dir) then
637 Free (Dir);
638 raise Directory_Error;
639 end if;
640 end Open;
641
642 ----------
643 -- Read --
644 ----------
645
646 procedure Read
647 (Dir : Dir_Type;
648 Str : out String;
649 Last : out Natural)
650 is
651 Filename_Addr : Address;
652 Filename_Len : aliased Integer;
653
654 Buffer : array (0 .. Filename_Max + 12) of Character;
655 -- 12 is the size of the dirent structure (see dirent.h), without the
656 -- field for the filename.
657
658 function readdir_gnat
659 (Directory : System.Address;
660 Buffer : System.Address;
661 Last : not null access Integer) return System.Address;
662 pragma Import (C, readdir_gnat, "__gnat_readdir");
663
664 begin
665 if not Is_Open (Dir) then
666 raise Directory_Error;
667 end if;
668
669 Filename_Addr :=
670 readdir_gnat
671 (System.Address (Dir.all), Buffer'Address, Filename_Len'Access);
672
673 if Filename_Addr = System.Null_Address then
674 Last := 0;
675 return;
676 end if;
677
678 declare
679 Filename : constant String (1 .. Filename_Len)
680 with Import, Address => Filename_Addr;
681 begin
682 if Str'Length > Filename_Len then
683 Last := Str'First + Filename_Len - 1;
684 Str (Str'First .. Last) := Filename;
685 else
686 Last := Str'Last;
687 Str := Filename (1 .. Str'Length);
688 end if;
689 end;
690 end Read;
691
692 -------------------------
693 -- Read_Is_Thread_Safe --
694 -------------------------
695
696 function Read_Is_Thread_Safe return Boolean is
697 function readdir_is_thread_safe return Integer;
698 pragma Import
699 (C, readdir_is_thread_safe, "__gnat_readdir_is_thread_safe");
700 begin
701 return (readdir_is_thread_safe /= 0);
702 end Read_Is_Thread_Safe;
703
704 ----------------
705 -- Remove_Dir --
706 ----------------
707
708 procedure Remove_Dir
709 (Dir_Name : Dir_Name_Str;
710 Recursive : Boolean := False)
711 is
712 C_Dir_Name : constant String := Dir_Name & ASCII.NUL;
713 Last : Integer;
714 Str : String (1 .. Filename_Max);
715 Success : Boolean;
716 Current_Dir : Dir_Type;
717
718 begin
719 -- Remove the directory only if it is empty
720
721 if not Recursive then
722 if rmdir (C_Dir_Name) /= 0 then
723 raise Directory_Error;
724 end if;
725
726 -- Remove directory and all files and directories that it may contain
727
728 else
729 Open (Current_Dir, Dir_Name);
730
731 loop
732 Read (Current_Dir, Str, Last);
733 exit when Last = 0;
734
735 if GNAT.OS_Lib.Is_Directory
736 (Dir_Name & Dir_Separator & Str (1 .. Last))
737 then
738 if Str (1 .. Last) /= "."
739 and then
740 Str (1 .. Last) /= ".."
741 then
742 -- Recursive call to remove a subdirectory and all its
743 -- files.
744
745 Remove_Dir
746 (Dir_Name & Dir_Separator & Str (1 .. Last),
747 True);
748 end if;
749
750 else
751 GNAT.OS_Lib.Delete_File
752 (Dir_Name & Dir_Separator & Str (1 .. Last),
753 Success);
754
755 if not Success then
756 raise Directory_Error;
757 end if;
758 end if;
759 end loop;
760
761 Close (Current_Dir);
762 Remove_Dir (Dir_Name);
763 end if;
764 end Remove_Dir;
765
766 end GNAT.Directory_Operations;