OSDN Git Service

2007-04-20 Vincent Celier <celier@adacore.com>
[pf3gnuchains/gcc-fork.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 --          Copyright (C) 1998-2001 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 2,  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.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- As a special exception,  if other files  instantiate  generics from this --
23 -- unit, or you link  this unit with other files  to produce an executable, --
24 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
25 -- covered  by the  GNU  General  Public  License.  This exception does not --
26 -- however invalidate  any other reasons why  the executable file  might be --
27 -- covered by the  GNU Public License.                                      --
28 --                                                                          --
29 -- GNAT was originally developed  by the GNAT team at  New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 with System;
35
36 package body GNAT.Lock_Files is
37
38    Dir_Separator : Character;
39    pragma Import (C, Dir_Separator, "__gnat_dir_separator");
40
41    ---------------
42    -- Lock_File --
43    ---------------
44
45    procedure Lock_File
46      (Directory      : Path_Name;
47       Lock_File_Name : Path_Name;
48       Wait           : Duration := 1.0;
49       Retries        : Natural  := Natural'Last)
50    is
51       Dir  : aliased String := Directory & ASCII.NUL;
52       File : aliased String := Lock_File_Name & ASCII.NUL;
53
54       function Try_Lock (Dir, File : System.Address) return Integer;
55       pragma Import (C, Try_Lock, "__gnat_try_lock");
56
57    begin
58       --  If a directory separator was provided, just remove the one we have
59       --  added above.
60
61       if Directory (Directory'Last) = Dir_Separator
62         or else Directory (Directory'Last) = '/'
63       then
64          Dir (Dir'Last - 1) := ASCII.Nul;
65       end if;
66
67       --  Try to lock the file Retries times
68
69       for I in 0 .. Retries loop
70          if Try_Lock (Dir'Address, File'Address) = 1 then
71             return;
72          end if;
73
74          exit when I = Retries;
75          delay Wait;
76       end loop;
77
78       raise Lock_Error;
79    end Lock_File;
80
81    ---------------
82    -- Lock_File --
83    ---------------
84
85    procedure Lock_File
86      (Lock_File_Name : Path_Name;
87       Wait           : Duration := 1.0;
88       Retries        : Natural  := Natural'Last)
89    is
90    begin
91       for J in reverse Lock_File_Name'Range loop
92          if Lock_File_Name (J) = Dir_Separator
93            or else Lock_File_Name (J) = '/'
94          then
95             Lock_File
96               (Lock_File_Name (Lock_File_Name'First .. J - 1),
97                Lock_File_Name (J + 1 .. Lock_File_Name'Last),
98                Wait,
99                Retries);
100             return;
101          end if;
102       end loop;
103
104       Lock_File (".", Lock_File_Name, Wait, Retries);
105    end Lock_File;
106
107    -----------------
108    -- Unlock_File --
109    -----------------
110
111    procedure Unlock_File (Lock_File_Name : Path_Name) is
112       S : aliased String := Lock_File_Name & ASCII.NUL;
113
114       procedure unlink (A : System.Address);
115       pragma Import (C, unlink, "unlink");
116
117    begin
118       unlink (S'Address);
119    end Unlock_File;
120
121    -----------------
122    -- Unlock_File --
123    -----------------
124
125    procedure Unlock_File (Directory : Path_Name; Lock_File_Name : Path_Name) is
126    begin
127       if Directory (Directory'Last) = Dir_Separator
128         or else Directory (Directory'Last) = '/'
129       then
130          Unlock_File (Directory & Lock_File_Name);
131       else
132          Unlock_File (Directory & Dir_Separator & Lock_File_Name);
133       end if;
134    end Unlock_File;
135
136 end GNAT.Lock_Files;