Nico Huber | 0a9591e | 2023-11-27 16:59:11 +0100 | [diff] [blame] | 1 | with Interfaces.C; |
| 2 | with Interfaces.C.Strings; |
| 3 | |
| 4 | use Interfaces.C; |
| 5 | |
Thomas Heijligen | d49cb12 | 2023-11-29 10:03:02 +0000 | [diff] [blame] | 6 | package body FS.FILO.VFS |
| 7 | with |
| 8 | SPARK_Mode => Off, |
| 9 | Convention => C |
| 10 | is |
Nico Huber | 0a9591e | 2023-11-27 16:59:11 +0100 | [diff] [blame] | 11 | |
Nico Huber | 51f6041 | 2023-12-04 14:48:11 +0100 | [diff] [blame^] | 12 | State : T := Initial; |
| 13 | |
Nico Huber | 0a9591e | 2023-11-27 16:59:11 +0100 | [diff] [blame] | 14 | function C_Mount return int |
| 15 | is |
| 16 | Success : Boolean; |
| 17 | begin |
Nico Huber | 51f6041 | 2023-12-04 14:48:11 +0100 | [diff] [blame^] | 18 | 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 Huber | 0a9591e | 2023-11-27 16:59:11 +0100 | [diff] [blame] | 22 | 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 Huber | 51f6041 | 2023-12-04 14:48:11 +0100 | [diff] [blame^] | 27 | File_Len : File_Length; |
Nico Huber | 0a9591e | 2023-11-27 16:59:11 +0100 | [diff] [blame] | 28 | Success : Boolean; |
| 29 | begin |
Nico Huber | 51f6041 | 2023-12-04 14:48:11 +0100 | [diff] [blame^] | 30 | 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 Huber | 0a9591e | 2023-11-27 16:59:11 +0100 | [diff] [blame] | 36 | return (if Success then 1 else 0); |
| 37 | end C_Open; |
Nico Huber | 51f6041 | 2023-12-04 14:48:11 +0100 | [diff] [blame^] | 38 | |
| 39 | procedure C_Close is |
| 40 | begin |
| 41 | if Is_Open (State) then |
| 42 | Close (State); |
| 43 | end if; |
| 44 | end C_Close; |
Nico Huber | 0a9591e | 2023-11-27 16:59:11 +0100 | [diff] [blame] | 45 | |
| 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 Huber | 51f6041 | 2023-12-04 14:48:11 +0100 | [diff] [blame^] | 54 | File_Pos : File_Offset := FILO.File_Pos; |
Nico Huber | 0a9591e | 2023-11-27 16:59:11 +0100 | [diff] [blame] | 55 | Read_Len : Natural; |
| 56 | begin |
Nico Huber | 51f6041 | 2023-12-04 14:48:11 +0100 | [diff] [blame^] | 57 | 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 Huber | 0a9591e | 2023-11-27 16:59:11 +0100 | [diff] [blame] | 63 | return int (Read_Len); |
| 64 | end C_Read; |
| 65 | |
| 66 | end FS.FILO.VFS; |