blob: 660a1cf06a5d6198d73198fa085cbfd3a219e019 [file] [log] [blame]
Nico Huber0a9591e2023-11-27 16:59:11 +01001with Interfaces.C;
2with Interfaces.C.Strings;
3
4use Interfaces.C;
5
Thomas Heijligend49cb122023-11-29 10:03:02 +00006package body FS.FILO.VFS
7with
8 SPARK_Mode => Off,
9 Convention => C
10is
Nico Huber0a9591e2023-11-27 16:59:11 +010011
Nico Huber51f60412023-12-04 14:48:11 +010012 State : T := Initial;
13
Nico Huber0a9591e2023-11-27 16:59:11 +010014 function C_Mount return int
15 is
16 Success : Boolean;
17 begin
Nico Huber51f60412023-12-04 14:48:11 +010018 if not Is_Mounted (State) then
19 State := Initial; -- Work around not having an unmount in FILO.
20 end if;
21 Mount (State, Part_Len, Success);
Nico Huber0a9591e2023-11-27 16:59:11 +010022 return (if Success then 1 else 0);
23 end C_Mount;
24
25 function C_Open (File_Path : Strings.chars_ptr) return int
26 is
Nico Huber51f60412023-12-04 14:48:11 +010027 File_Len : File_Length;
Nico Huber0a9591e2023-11-27 16:59:11 +010028 Success : Boolean;
29 begin
Nico Huber51f60412023-12-04 14:48:11 +010030 if Is_Mounted (State) and not Is_Open (State) then
31 Open (State, File_Len, Strings.Value (File_Path), Success);
32 Set_File_Max (File_Len);
33 else
34 Success := False;
35 end if;
Nico Huber0a9591e2023-11-27 16:59:11 +010036 return (if Success then 1 else 0);
37 end C_Open;
Nico Huber51f60412023-12-04 14:48:11 +010038
39 procedure C_Close is
40 begin
41 if Is_Open (State) then
42 Close (State);
43 end if;
44 end C_Close;
Nico Huber0a9591e2023-11-27 16:59:11 +010045
46 function C_Read (Buf : System.Address; Len : int) return int
47 is
48 subtype Buffer_Range is Natural range 0 .. Integer (Len) - 1;
49 Buffer : Buffer_Type (Buffer_Range)
50 with
51 Convention => C,
52 Address => Buf;
53
Nico Huber51f60412023-12-04 14:48:11 +010054 File_Pos : File_Offset := FILO.File_Pos;
Nico Huber0a9591e2023-11-27 16:59:11 +010055 Read_Len : Natural;
56 begin
Nico Huber51f60412023-12-04 14:48:11 +010057 if Is_Open (State) then
58 Read (State, File_Max, File_Pos, Buffer, Read_Len);
59 Set_File_Pos (File_Pos);
60 else
61 Read_Len := 0;
62 end if;
Nico Huber0a9591e2023-11-27 16:59:11 +010063 return int (Read_Len);
64 end C_Read;
65
66end FS.FILO.VFS;