* 5oosinte.adb: Add 2001 to copyright notice.
[platform/upstream/gcc.git] / gcc / ada / g-locfil.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                      G N A T . L O C K _ F I L E S                       --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --                            $Revision: 1.4 $
10 --                                                                          --
11 --          Copyright (C) 1998-2001 Free Software Foundation, Inc.          --
12 --                                                                          --
13 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
14 -- terms of the  GNU General Public License as published  by the Free Soft- --
15 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
16 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
19 -- for  more details.  You should have  received  a copy of the GNU General --
20 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
21 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
22 -- MA 02111-1307, USA.                                                      --
23 --                                                                          --
24 -- As a special exception,  if other files  instantiate  generics from this --
25 -- unit, or you link  this unit with other files  to produce an executable, --
26 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
27 -- covered  by the  GNU  General  Public  License.  This exception does not --
28 -- however invalidate  any other reasons why  the executable file  might be --
29 -- covered by the  GNU Public License.                                      --
30 --                                                                          --
31 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com).   --
32 --                                                                          --
33 ------------------------------------------------------------------------------
34
35 with System;
36
37 package body GNAT.Lock_Files is
38
39    Dir_Separator : Character;
40    pragma Import (C, Dir_Separator, "__gnat_dir_separator");
41
42    ---------------
43    -- Lock_File --
44    ---------------
45
46    procedure Lock_File
47      (Directory      : String;
48       Lock_File_Name : String;
49       Wait           : Duration := 1.0;
50       Retries        : Natural  := Natural'Last)
51    is
52       Dir  : aliased String := Directory & ASCII.NUL;
53       File : aliased String := Lock_File_Name & ASCII.NUL;
54
55       function Try_Lock (Dir, File : System.Address) return Integer;
56       pragma Import (C, Try_Lock, "__gnat_try_lock");
57
58    begin
59       for I in 0 .. Retries loop
60          if Try_Lock (Dir'Address, File'Address) = 1 then
61             return;
62          end if;
63          exit when I = Retries;
64          delay Wait;
65       end loop;
66       raise Lock_Error;
67    end Lock_File;
68
69    ---------------
70    -- Lock_File --
71    ---------------
72
73    procedure Lock_File
74      (Lock_File_Name : String;
75       Wait           : Duration := 1.0;
76       Retries        : Natural  := Natural'Last)
77    is
78    begin
79       for J in reverse Lock_File_Name'Range loop
80          if Lock_File_Name (J) = Dir_Separator then
81             Lock_File
82               (Lock_File_Name (Lock_File_Name'First .. J - 1),
83                Lock_File_Name (J + 1 .. Lock_File_Name'Last),
84                Wait,
85                Retries);
86             return;
87          end if;
88       end loop;
89
90       Lock_File (".", Lock_File_Name, Wait, Retries);
91    end Lock_File;
92
93    -----------------
94    -- Unlock_File --
95    -----------------
96
97    procedure Unlock_File (Lock_File_Name : String) is
98       S : aliased String := Lock_File_Name & ASCII.NUL;
99
100       procedure unlink (A : System.Address);
101       pragma Import (C, unlink, "unlink");
102
103    begin
104       unlink (S'Address);
105    end Unlock_File;
106
107    -----------------
108    -- Unlock_File --
109    -----------------
110
111    procedure Unlock_File (Directory : String; Lock_File_Name : String) is
112    begin
113       Unlock_File (Directory & Dir_Separator & Lock_File_Name);
114    end Unlock_File;
115
116 end GNAT.Lock_Files;