Debian Codegraph @

gcc/ada/libgnat/s-direio.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- S Y S T E M . D I R E C T _ I O --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2025, 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 ------------------------------------------------------------------------------
31
32 with Ada.IO_Exceptions; use Ada.IO_Exceptions;
33 with Ada.Unchecked_Deallocation;
34 with Interfaces.C_Streams; use Interfaces.C_Streams;
35 with System.CRTL;
36 with System.File_IO;
37 with System.Soft_Links;
38
39 package body System.Direct_IO is
40
41 package FIO renames System.File_IO;
42 package SSL renames System.Soft_Links;
43
44 subtype AP is FCB.AFCB_Ptr;
45 use type FCB.Shared_Status_Type;
46
47 use type System.CRTL.int64;
48 use type System.CRTL.size_t;
49
50 -----------------------
51 -- Local Subprograms --
52 -----------------------
53
54 procedure Set_Position (File : File_Type);
55 -- Sets file position pointer according to value of current index
56
57 -------------------
58 -- AFCB_Allocate --
59 -------------------
60
61 function AFCB_Allocate (Control_Block : Direct_AFCB) return FCB.AFCB_Ptr is
62 pragma Unreferenced (Control_Block);
63 begin
64 return new Direct_AFCB;
65 end AFCB_Allocate;
66
67 ----------------
68 -- AFCB_Close --
69 ----------------
70
71 -- No special processing required for Direct_IO close
72
73 procedure AFCB_Close (File : not null access Direct_AFCB) is
74 pragma Unreferenced (File);
75 begin
76 null;
77 end AFCB_Close;
78
79 ---------------
80 -- AFCB_Free --
81 ---------------
82
83 procedure AFCB_Free (File : not null access Direct_AFCB) is
84
85 type FCB_Ptr is access all Direct_AFCB;
86
87 FT : FCB_Ptr := FCB_Ptr (File);
88
89 procedure Free is new
90 Ada.Unchecked_Deallocation (Direct_AFCB, FCB_Ptr);
91
92 begin
93 Free (FT);
94 end AFCB_Free;
95
96 ------------
97 -- Create --
98 ------------
99
100 procedure Create
101 (File : in out File_Type;
102 Mode : FCB.File_Mode := FCB.Inout_File;
103 Name : String := "";
104 Form : String := "")
105 is
106 Dummy_File_Control_Block : Direct_AFCB;
107 pragma Warnings (Off, Dummy_File_Control_Block);
108 -- Yes, we know this is never assigned a value, only the tag is used for
109 -- dispatching purposes, so that's expected.
110
111 begin
112 FIO.Open (File_Ptr => AP (File),
113 Dummy_FCB => Dummy_File_Control_Block,
114 Mode => Mode,
115 Name => Name,
116 Form => Form,
117 Amethod => 'D',
118 Creat => True,
119 Text => False);
120 end Create;
121
122 -----------------
123 -- End_Of_File --
124 -----------------
125
126 function End_Of_File (File : File_Type) return Boolean is
127 begin
128 FIO.Check_Read_Status (AP (File));
129 return File.Index > Size (File);
130 end End_Of_File;
131
132 -----------
133 -- Index --
134 -----------
135
136 function Index (File : File_Type) return Positive_Count is
137 begin
138 FIO.Check_File_Open (AP (File));
139 return File.Index;
140 end Index;
141
142 ----------
143 -- Open --
144 ----------
145
146 procedure Open
147 (File : in out File_Type;
148 Mode : FCB.File_Mode;
149 Name : String;
150 Form : String := "")
151 is
152 Dummy_File_Control_Block : Direct_AFCB;
153 pragma Warnings (Off, Dummy_File_Control_Block);
154 -- Yes, we know this is never assigned a value, only the tag is used for
155 -- dispatching purposes, so that's expected.
156
157 begin
158 FIO.Open (File_Ptr => AP (File),
159 Dummy_FCB => Dummy_File_Control_Block,
160 Mode => Mode,
161 Name => Name,
162 Form => Form,
163 Amethod => 'D',
164 Creat => False,
165 Text => False);
166 end Open;
167
168 ----------
169 -- Read --
170 ----------
171
172 procedure Read
173 (File : File_Type;
174 Item : Address;
175 Size : Interfaces.C_Streams.size_t;
176 From : Positive_Count)
177 is
178 begin
179 Set_Index (File, From);
180 Read (File, Item, Size);
181 end Read;
182
183 procedure Read
184 (File : File_Type;
185 Item : Address;
186 Size : Interfaces.C_Streams.size_t)
187 is
188 begin
189 FIO.Check_Read_Status (AP (File));
190
191 -- If last operation was not a read, or if in file sharing mode,
192 -- then reset the physical pointer of the file to match the index
193 -- We lock out task access over the two operations in this case.
194
195 if File.Last_Op /= Op_Read
196 or else File.Shared_Status = FCB.Yes
197 then
198 if End_Of_File (File) then
199 raise End_Error;
200 end if;
201
202 Locked_Processing : begin
203 SSL.Lock_Task.all;
204 Set_Position (File);
205 FIO.Read_Buf (AP (File), Item, Size);
206 SSL.Unlock_Task.all;
207
208 exception
209 when others =>
210 SSL.Unlock_Task.all;
211 raise;
212 end Locked_Processing;
213
214 else
215 FIO.Read_Buf (AP (File), Item, Size);
216 end if;
217
218 File.Index := File.Index + 1;
219
220 -- Set last operation to read, unless we did not read a full record
221 -- (happens with the variant record case) in which case we set the
222 -- last operation as other, to force the file position to be reset
223 -- on the next read.
224
225 File.Last_Op := (if File.Bytes = Size then Op_Read else Op_Other);
226 end Read;
227
228 -- The following is the required overriding for Stream.Read, which is
229 -- not used, since we do not do Stream operations on Direct_IO files.
230
231 procedure Read
232 (File : in out Direct_AFCB;
233 Item : out Ada.Streams.Stream_Element_Array;
234 Last : out Ada.Streams.Stream_Element_Offset)
235 is
236 begin
237 raise Program_Error;
238 end Read;
239
240 -----------
241 -- Reset --
242 -----------
243
244 procedure Reset (File : in out File_Type; Mode : FCB.File_Mode) is
245 pragma Warnings (Off, File);
246 -- File is actually modified via Unrestricted_Access below, but
247 -- GNAT will generate a warning anyway.
248 --
249 -- Note that we do not use pragma Unmodified here, since in -gnatc mode,
250 -- GNAT will complain that File is modified for "File.Index := 1;"
251 begin
252 FIO.Reset (AP (File)'Unrestricted_Access, Mode);
253 File.Index := 1;
254 File.Last_Op := Op_Read;
255 end Reset;
256
257 procedure Reset (File : in out File_Type) is
258 pragma Warnings (Off, File);
259 -- See above (other Reset procedure) for explanations on this pragma
260 begin
261 FIO.Reset (AP (File)'Unrestricted_Access);
262 File.Index := 1;
263 File.Last_Op := Op_Read;
264 end Reset;
265
266 ---------------
267 -- Set_Index --
268 ---------------
269
270 procedure Set_Index (File : File_Type; To : Positive_Count) is
271 begin
272 FIO.Check_File_Open (AP (File));
273 File.Index := Count (To);
274 File.Last_Op := Op_Other;
275 end Set_Index;
276
277 ------------------
278 -- Set_Position --
279 ------------------
280
281 procedure Set_Position (File : File_Type) is
282 R : int;
283 begin
284 R :=
285 fseek64
286 (File.Stream, int64 (File.Bytes) * int64 (File.Index - 1), SEEK_SET);
287
288 if R /= 0 then
289 raise Use_Error;
290 end if;
291 end Set_Position;
292
293 ----------
294 -- Size --
295 ----------
296
297 function Size (File : File_Type) return Count is
298 Pos : int64;
299
300 begin
301 FIO.Check_File_Open (AP (File));
302 File.Last_Op := Op_Other;
303
304 if fseek64 (File.Stream, 0, SEEK_END) /= 0 then
305 raise Device_Error;
306 end if;
307
308 Pos := ftell64 (File.Stream);
309
310 if Pos = -1 then
311 raise Use_Error;
312 end if;
313
314 return Count (Pos / int64 (File.Bytes));
315 end Size;
316
317 -----------
318 -- Write --
319 -----------
320
321 procedure Write
322 (File : File_Type;
323 Item : Address;
324 Size : Interfaces.C_Streams.size_t;
325 Zeroes : System.Storage_Elements.Storage_Array)
326
327 is
328 procedure Do_Write;
329 -- Do the actual write
330
331 --------------
332 -- Do_Write --
333 --------------
334
335 procedure Do_Write is
336 begin
337 FIO.Write_Buf (AP (File), Item, Size);
338
339 -- If we did not write the whole record (happens with the variant
340 -- record case), then fill out the rest of the record with zeroes.
341 -- This is cleaner in any case, and is required for the last
342 -- record, since otherwise the length of the file is wrong.
343
344 if File.Bytes > Size then
345 FIO.Write_Buf (AP (File), Zeroes'Address, File.Bytes - Size);
346 end if;
347 end Do_Write;
348
349 -- Start of processing for Write
350
351 begin
352 FIO.Check_Write_Status (AP (File));
353
354 -- If last operation was not a write, or if in file sharing mode,
355 -- then reset the physical pointer of the file to match the index
356 -- We lock out task access over the two operations in this case.
357
358 if File.Last_Op /= Op_Write
359 or else File.Shared_Status = FCB.Yes
360 then
361 Locked_Processing : begin
362 SSL.Lock_Task.all;
363 Set_Position (File);
364 Do_Write;
365 SSL.Unlock_Task.all;
366
367 exception
368 when others =>
369 SSL.Unlock_Task.all;
370 raise;
371 end Locked_Processing;
372
373 else
374 Do_Write;
375 end if;
376
377 File.Index := File.Index + 1;
378
379 -- Set last operation to write, unless we did not read a full record
380 -- (happens with the variant record case) in which case we set the
381 -- last operation as other, to force the file position to be reset
382 -- on the next write.
383
384 File.Last_Op := (if File.Bytes = Size then Op_Write else Op_Other);
385 end Write;
386
387 -- The following is the required overriding for Stream.Write, which is
388 -- not used, since we do not do Stream operations on Direct_IO files.
389
390 procedure Write
391 (File : in out Direct_AFCB;
392 Item : Ada.Streams.Stream_Element_Array)
393 is
394 begin
395 raise Program_Error;
396 end Write;
397
398 end System.Direct_IO;