zipada_56.0.2_b3043499/extras/ada_directories_extensions.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
--  (c) Martin M. Dowie, 2003-2004

pragma License (Modified_GPL);

with Ada.Exceptions; use Ada.Exceptions;
with Ada.Unchecked_Conversion;

with GNAT.Calendar;

with Interfaces.C.Strings; use Interfaces.C.Strings;

with Win32.crt.Stat;
with Win32.crt.Time;
with Win32.crt.Utime;

package body Ada_Directories_Extensions is

   type utimbuf_ptr is access all Win32.crt.Utime.utimbuf;
   pragma Convention (C, utimbuf_ptr);

   Null_utimbuf_ptr : constant utimbuf_ptr := null;

   --------------
   -- To_PCSTR --
   --------------

   function To_PCSTR is
      new Ada.Unchecked_Conversion (Interfaces.C.Strings.chars_ptr,
                                    Win32.PCSTR);

   -----------
   -- To_tm --
   -----------

   function To_tm (From : Ada.Calendar.Time)
      return Win32.crt.Time.tm;

   -----------
   -- utime --
   -----------

   --  Can't use Win32.crt.Utime binding as it is wrong. It uses
   --  an access parameter which means we can't pass 'NULL' which
   --  is actually a valid value to pass and the one required to
   --  provide this 'touch' facility.
   --
   function utime (filename : Interfaces.C.Strings.chars_ptr;
                   utimbuf : utimbuf_ptr)
      return Interfaces.C.int;
   pragma Import (C, utime, "_utime");

   -----------------------
   -- Set_Access_Time --
   -----------------------

   procedure Set_Access_Time (Directory_Entry : in Directory_Entry_Type;
                              To                : in Ada.Calendar.Time) is
   begin
      Set_Access_Time (Full_Name (Directory_Entry), To);
   end Set_Access_Time;

   -----------------------
   -- Set_Access_Time --
   -----------------------

   procedure Set_Access_Time (Name : in String;
                              To   : in Ada.Calendar.Time) is
      use type Interfaces.C.int;
      File_Stat : aliased Win32.crt.Stat.struct_stat;
      C_Name : constant Win32.PCSTR :=
         To_PCSTR (New_String (Full_Name (Name)));
      Ok : constant Interfaces.C.int :=
         Win32.crt.Stat.stat (C_Name, File_Stat'Access);
   begin
      if Ok /= 0 then
         -- Validate (Name);
         Raise_Exception (Use_Error'Identity,
                          "Unknown fault in setting access time");
      end if;
      declare
         Tm : aliased Win32.crt.Time.tm := To_tm (To);
         Buffer : aliased Win32.crt.Utime.utimbuf :=
            (actime => Win32.crt.Time.mktime (Tm'Access),
             modtime => File_Stat.st_mtime);
         Ok : constant Interfaces.C.int :=
            utime (New_String (Full_Name (Name)),
                   Buffer'Unchecked_Access);
      begin
         if Ok /= 0 then
            Raise_Exception (Program_Error'Identity,
                             "Could not change time - check access rights [" &
                                Simple_Name (Name) & "]");
         end if;
      end;
   end Set_Access_Time;

   ---------------------------
   -- Set_Modification_Time --
   ---------------------------

   procedure Set_Modification_Time (Directory_Entry : in Directory_Entry_Type;
                                    To              : in Ada.Calendar.Time) is
   begin
      Set_Modification_Time (Full_Name (Directory_Entry), To);
   end Set_Modification_Time;

   ---------------------------
   -- Set_Modification_Time --
   ---------------------------

   procedure Set_Modification_Time (Name : in String;
                                    To   : in Ada.Calendar.Time) is
      use type Interfaces.C.int;
      File_Stat : aliased Win32.crt.Stat.struct_stat;
      C_Name : constant Win32.PCSTR :=
         To_PCSTR (New_String (Full_Name (Name)));
      Ok : constant Interfaces.C.int :=
         Win32.crt.Stat.stat (C_Name, File_Stat'Access);
   begin
      if Ok /= 0 then
         -- Validate (Name);
         Raise_Exception (Use_Error'Identity,
                          "Unknown fault in setting modification time");
      end if;
      declare
         Tm : aliased Win32.crt.Time.tm := To_tm (To);
         Buffer : aliased Win32.crt.Utime.utimbuf :=
            (actime => File_Stat.st_atime,
             modtime => Win32.crt.Time.mktime (Tm'Access));
         Ok : constant Interfaces.C.int :=
            utime (New_String (Full_Name (Name)),
                   Buffer'Unchecked_Access);
      begin
         if Ok /= 0 then
            Raise_Exception (Program_Error'Identity,
                             "Could not change modification time - check " &
                                "access rights [" & Simple_Name (Name) & "]");
         end if;
      end;
   end Set_Modification_Time;

   ---------------
   -- Set_Times --
   ---------------

   procedure Set_Times (Directory_Entry : in Directory_Entry_Type;
                        Access_Time : in Ada.Calendar.Time;
                        Modification_Time : in Ada.Calendar.Time) is
   begin
      Set_Times (Full_Name (Directory_Entry), Access_Time, Modification_Time);
   end Set_Times;

   ---------------
   -- Set_Times --
   ---------------

   procedure Set_Times (Name : in String;
                        Access_Time : in Ada.Calendar.Time;
                        Modification_Time : in Ada.Calendar.Time) is
      use type Interfaces.C.int;
      File_Stat : aliased Win32.crt.Stat.struct_stat;
      C_Name : constant Win32.PCSTR :=
         To_PCSTR (New_String (Full_Name (Name)));
      Ok : constant Interfaces.C.int :=
         Win32.crt.Stat.stat (C_Name, File_Stat'Access);
   begin
      if Ok /= 0 then
         -- Validate (Name);
         Raise_Exception (Use_Error'Identity,
                          "Unknown fault in setting times");
      end if;
      declare
         Atm : aliased Win32.crt.Time.tm := To_tm (Access_Time);
         Mtm : aliased Win32.crt.Time.tm := To_tm (Modification_Time);
         Buffer : aliased Win32.crt.Utime.utimbuf :=
            (actime => Win32.crt.Time.mktime (Atm'Access),
             modtime => Win32.crt.Time.mktime (Mtm'Access));
         Ok : constant Interfaces.C.int :=
            utime (New_String (Full_Name (Name)),
                   Buffer'Unchecked_Access);
      begin
         if Ok /= 0 then
            Raise_Exception (Program_Error'Identity,
                             "Could not change times - check " &
                                "access rights [" & Simple_Name (Name) & "]");
         end if;
      end;
   end Set_Times;

   -----------------------------
   -- Touch_Modification_Time --
   -----------------------------

   procedure Touch_Modification_Time
      (Directory_Entry : in Directory_Entry_Type) is
   begin
      Touch_Modification_Time (Full_Name (Directory_Entry));
   end Touch_Modification_Time;

   -----------------------------
   -- Touch_Modification_Time --
   -----------------------------

   procedure Touch_Modification_Time (Name : in String) is

      use type Interfaces.C.int;

      Ok : constant Interfaces.C.int := utime (New_String (Full_Name (Name)),
                                               Null_utimbuf_ptr);
   begin
      if Ok /= 0 then
         -- Validate (Name);
         Raise_Exception (Program_Error'Identity,
                          "Could not change time - check access rights [" &
                             Simple_Name (Name) & "]");
      end if;
   end Touch_Modification_Time;

   -----------
   -- To_tm --
   -----------

   function To_tm (From : Ada.Calendar.Time)
      return Win32.crt.Time.tm is
      use type Win32.INT;
      Year       : Ada.Calendar.Year_Number;
      Month      : Ada.Calendar.Month_Number;
      Day        : Ada.Calendar.Day_Number;
      Hour       : GNAT.Calendar.Hour_Number;
      Minute     : GNAT.Calendar.Minute_Number;
      Second     : GNAT.Calendar.Second_Number;
      Sub_Second : GNAT.Calendar.Second_Duration;
   begin
      GNAT.Calendar.Split
         (From, Year, Month, Day, Hour, Minute, Second, Sub_Second);
      return (tm_sec   => Win32.INT (Second),
              tm_min   => Win32.INT (Minute),
              tm_hour  => Win32.INT (Hour),
              tm_mday  => Win32.INT (Day),
              tm_mon   => Win32.INT (Month) - 1,
              tm_year  => Win32.INT (Year) - 1900,
              tm_wday  => Win32.INT (GNAT.Calendar.Day_Name'Pos
                                        (GNAT.Calendar.Day_Of_Week (From))),
              tm_yday  => Win32.INT (GNAT.Calendar.Day_In_Year (From)),
              tm_isdst => -1);
   end To_tm;

   --------------
   -- Validate --
   --------------

   procedure Validate (Name : in Directory_Entry_Type) is
   begin
      Validate (Full_Name (Name));
   end Validate;

   --------------
   -- Validate --
   --------------

   procedure Validate (Name : in String) is
   begin
      --  if not Port.Is_Valid_Filename (Name) then
      --     Raise_Exception (Name_Error'Identity,
      --                      "Invalid name [" & Name & "]");
      --  end if;
      if not Exists (Name) then
         Raise_Exception (Use_Error'Identity,
                          "Does not exist [" & Name & "]");
      end if;
   end Validate;

end Ada_Directories_Extensions;