blob: a76d84c3e56f9db876eece1f00846b89d8562e1b [file] [log] [blame]
Nico Huber0a9591e2023-11-27 16:59:11 +01001with Interfaces.C;
2with Interfaces.C.Strings;
3
4use Interfaces.C;
5
Thomas Heijligen5c43abc2023-12-11 15:24:36 +00006package body FILO.FS.VFS
Thomas Heijligend49cb122023-11-29 10:03:02 +00007with
Nico Huber691220d2023-12-04 14:54:01 +01008 SPARK_Mode => Off
Thomas Heijligend49cb122023-11-29 10:03:02 +00009is
Nico Huber0a9591e2023-11-27 16:59:11 +010010
Nico Huber51f60412023-12-04 14:48:11 +010011 State : T := Initial;
12
Nico Huber0a9591e2023-11-27 16:59:11 +010013 function C_Mount return int
14 is
15 Success : Boolean;
16 begin
Nico Huber51f60412023-12-04 14:48:11 +010017 if not Is_Mounted (State) then
18 State := Initial; -- Work around not having an unmount in FILO.
19 end if;
20 Mount (State, Part_Len, Success);
Nico Huber0a9591e2023-11-27 16:59:11 +010021 return (if Success then 1 else 0);
22 end C_Mount;
23
24 function C_Open (File_Path : Strings.chars_ptr) return int
25 is
Nico Huber51f60412023-12-04 14:48:11 +010026 File_Len : File_Length;
Nico Huber0a9591e2023-11-27 16:59:11 +010027 Success : Boolean;
28 begin
Nico Huber51f60412023-12-04 14:48:11 +010029 if Is_Mounted (State) and not Is_Open (State) then
30 Open (State, File_Len, Strings.Value (File_Path), Success);
31 Set_File_Max (File_Len);
32 else
33 Success := False;
34 end if;
Nico Huber0a9591e2023-11-27 16:59:11 +010035 return (if Success then 1 else 0);
36 end C_Open;
Nico Huber51f60412023-12-04 14:48:11 +010037
38 procedure C_Close is
39 begin
40 if Is_Open (State) then
41 Close (State);
42 end if;
43 end C_Close;
Nico Huber0a9591e2023-11-27 16:59:11 +010044
45 function C_Read (Buf : System.Address; Len : int) return int
46 is
47 subtype Buffer_Range is Natural range 0 .. Integer (Len) - 1;
48 Buffer : Buffer_Type (Buffer_Range)
49 with
50 Convention => C,
51 Address => Buf;
52
Thomas Heijligen5c43abc2023-12-11 15:24:36 +000053 File_Pos : File_Offset := FILO.FS.File_Pos;
Nico Huber0a9591e2023-11-27 16:59:11 +010054 Read_Len : Natural;
55 begin
Nico Huber51f60412023-12-04 14:48:11 +010056 if Is_Open (State) then
Nico Huber549a1b82023-12-17 01:51:59 +010057 Read (State, File_Pos, Buffer, Read_Len);
Nico Huber51f60412023-12-04 14:48:11 +010058 Set_File_Pos (File_Pos);
59 else
60 Read_Len := 0;
61 end if;
Nico Huber0a9591e2023-11-27 16:59:11 +010062 return int (Read_Len);
63 end C_Read;
64
Thomas Heijligen5c43abc2023-12-11 15:24:36 +000065end FILO.FS.VFS;