Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 1 | with Ada.Unchecked_Conversion; |
Nico Huber | 0a9591e | 2023-11-27 16:59:11 +0100 | [diff] [blame] | 2 | with Interfaces.C; |
Nico Huber | 0a9591e | 2023-11-27 16:59:11 +0100 | [diff] [blame] | 3 | |
| 4 | use Interfaces.C; |
| 5 | |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 6 | package body FILO.FS.VFS is |
Nico Huber | 0a9591e | 2023-11-27 16:59:11 +0100 | [diff] [blame] | 7 | |
Nico Huber | 51f6041 | 2023-12-04 14:48:11 +0100 | [diff] [blame] | 8 | State : T := Initial; |
| 9 | |
Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 10 | Path_Max : constant := 4096; |
| 11 | Max_Link_Depth : constant := 32; |
| 12 | |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 13 | subtype Path_Length is Natural range 0 .. Path_Max; |
| 14 | subtype Path_Index_Plus is Positive range 1 .. Path_Max + 1; |
| 15 | subtype Path_Index is Path_Index_Plus range 1 .. Path_Max; |
| 16 | |
| 17 | subtype Path_Buffer is Buffer_Type (Path_Index) |
| 18 | with |
| 19 | Object_Size => Path_Max * 8; |
Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 20 | Path_Buf : Path_Buffer; |
| 21 | |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 22 | subtype Path_String is String (Path_Index) |
| 23 | with |
| 24 | Object_Size => Path_Max * 8; |
Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 25 | Path : Path_String; |
| 26 | |
Nico Huber | 0a9591e | 2023-11-27 16:59:11 +0100 | [diff] [blame] | 27 | function C_Mount return int |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 28 | with |
| 29 | SPARK_Mode => Off |
Nico Huber | 0a9591e | 2023-11-27 16:59:11 +0100 | [diff] [blame] | 30 | is |
| 31 | Success : Boolean; |
| 32 | begin |
Nico Huber | 51f6041 | 2023-12-04 14:48:11 +0100 | [diff] [blame] | 33 | if not Is_Mounted (State) then |
| 34 | State := Initial; -- Work around not having an unmount in FILO. |
| 35 | end if; |
| 36 | Mount (State, Part_Len, Success); |
Nico Huber | 0a9591e | 2023-11-27 16:59:11 +0100 | [diff] [blame] | 37 | return (if Success then 1 else 0); |
| 38 | end C_Mount; |
| 39 | |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 40 | procedure Open (File_Path : System.Address; Ret : out int) |
Nico Huber | 0a9591e | 2023-11-27 16:59:11 +0100 | [diff] [blame] | 41 | is |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 42 | function Str_Len (S : System.Address) return Natural; |
| 43 | function Str_Len (S : System.Address) return Natural |
| 44 | with |
| 45 | SPARK_Mode => Off |
| 46 | is |
| 47 | Buf : Buffer_Type (1 .. Path_Max + 1) |
| 48 | with |
| 49 | Convention => C, |
| 50 | Address => S; |
| 51 | begin |
| 52 | for I in Buf'Range loop |
| 53 | if Buf (I) = 16#00# then |
| 54 | return I - 1; |
| 55 | end if; |
| 56 | end loop; |
| 57 | return Path_Max + 1; |
| 58 | end Str_Len; |
| 59 | |
| 60 | procedure Str_Cpy (Dst : out String; Src : in System.Address); |
| 61 | procedure Str_Cpy (Dst : out String; Src : in System.Address) |
| 62 | with |
| 63 | SPARK_Mode => Off |
| 64 | is |
| 65 | Src_String : String (Dst'Range) |
| 66 | with |
| 67 | Convention => C, |
| 68 | Address => Src; |
| 69 | begin |
| 70 | Dst := Src_String; |
| 71 | end Str_Cpy; |
| 72 | |
| 73 | function Component_Start (Path : Path_String; Pos : Positive) return Path_Index_Plus is |
Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 74 | begin |
| 75 | for I in Pos .. Path'Last loop |
| 76 | if Path (I) /= '/' then |
| 77 | return I; |
| 78 | end if; |
| 79 | end loop; |
| 80 | return Path'Last + 1; |
| 81 | end Component_Start; |
| 82 | |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 83 | function Component_End (Path : Path_String; Start : Path_Index_Plus; Limit : Path_Index) return Path_Length |
| 84 | with |
| 85 | Post => Component_End'Result <= Limit |
| 86 | is |
Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 87 | begin |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 88 | for I in Start .. Limit loop |
Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 89 | if Path (I) = '/' or Is_Space (Path (I)) then |
| 90 | return I - 1; |
| 91 | end if; |
| 92 | end loop; |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 93 | return Limit; |
Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 94 | end Component_End; |
| 95 | |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 96 | function Path_End (Path : Path_String; Start : Path_Index) return Path_Length is |
Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 97 | begin |
| 98 | for I in Start .. Path'Last loop |
| 99 | if Is_Space (Path (I)) then |
| 100 | return I - 1; |
| 101 | end if; |
| 102 | end loop; |
| 103 | return Path'Last; |
| 104 | end Path_End; |
| 105 | |
| 106 | procedure Read_Link |
| 107 | (Path : in out Path_String; |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 108 | Rest_First : in Path_Index_Plus; |
| 109 | Rest_Last : in Path_Index; |
Nico Huber | 53df785 | 2024-01-15 18:36:04 +0100 | [diff] [blame] | 110 | Link_Len : in Positive; |
Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 111 | Success : out Boolean) |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 112 | with |
| 113 | Pre => |
| 114 | Is_Open (State) and |
| 115 | Rest_First - 1 <= Rest_Last, |
| 116 | Post => |
| 117 | Is_Open (State) |
Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 118 | is |
| 119 | function As_String is new Ada.Unchecked_Conversion (Path_Buffer, Path_String); |
| 120 | |
| 121 | Rest_Len : constant Natural := Rest_Last - Rest_First + 1; |
| 122 | File_Pos : File_Offset := 0; |
| 123 | Read_Len : Natural; |
| 124 | begin |
| 125 | if Path_Max - Rest_Len < Link_Len then |
| 126 | Success := False; |
| 127 | return; |
| 128 | end if; |
| 129 | |
Nico Huber | 53df785 | 2024-01-15 18:36:04 +0100 | [diff] [blame] | 130 | pragma Warnings |
| 131 | (GNATprove, Off, """File_Pos"" is set*but not used", |
| 132 | Reason => "We only read the link at once, during path walks."); |
Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 133 | Read |
| 134 | (State => State, |
| 135 | File_Pos => File_Pos, |
| 136 | Buf => Path_Buf (1 .. Link_Len), |
| 137 | Len => Read_Len); |
Nico Huber | 53df785 | 2024-01-15 18:36:04 +0100 | [diff] [blame] | 138 | pragma Warnings (GNATprove, On, """File_Pos"" is set*but not used"); |
Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 139 | Success := Read_Len = Link_Len; |
| 140 | |
| 141 | if Success then |
| 142 | Path (Link_Len + 1 .. Link_Len + Rest_Len) := Path (Rest_First .. Rest_Last); |
| 143 | Path (Link_Len + Rest_Len + 1 .. Path'Last) := (others => ' '); |
| 144 | Path (1 .. Link_Len) := As_String (Path_Buf) (1 .. Link_Len); |
| 145 | end if; |
| 146 | end Read_Link; |
| 147 | |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 148 | Path_Len : constant Natural := Str_Len (File_Path); |
Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 149 | Root_Dir : Boolean := True; |
Nico Huber | 0a9591e | 2023-11-27 16:59:11 +0100 | [diff] [blame] | 150 | begin |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 151 | Ret := 0; |
| 152 | |
Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 153 | if not Is_Mounted (State) or Path_Len > Path_Max then |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 154 | return; |
Nico Huber | 51f6041 | 2023-12-04 14:48:11 +0100 | [diff] [blame] | 155 | end if; |
Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 156 | |
Nico Huber | 4331448 | 2024-01-23 16:19:33 +0100 | [diff] [blame^] | 157 | if Is_Open (State) then |
| 158 | Close (State); |
| 159 | end if; |
| 160 | |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 161 | Str_Cpy (Path (1 .. Path_Len), File_Path); |
| 162 | Path (Path_Len + 1 .. Path'Last) := (others => ' '); |
Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 163 | |
| 164 | Link_Loop : |
| 165 | for I in 1 .. Max_Link_Depth loop |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 166 | pragma Loop_Invariant (Is_Mounted (State)); |
Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 167 | declare |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 168 | Path_Pos : Path_Index_Plus := Path'First; |
| 169 | Path_Last : constant Path_Length := Path_End (Path, Path_Pos); |
Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 170 | begin |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 171 | if Path_Last < Path'First then |
| 172 | return; |
| 173 | end if; |
| 174 | |
Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 175 | Path_Loop : |
| 176 | loop |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 177 | pragma Loop_Invariant |
| 178 | (Is_Mounted (State) and |
| 179 | Path_Last in Path'Range); |
Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 180 | declare |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 181 | Comp_First : constant Path_Index_Plus := Component_Start (Path, Path_Pos); |
| 182 | Comp_Last : constant Path_Length := Component_End (Path, Comp_First, Path_Last); |
Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 183 | File_Type : FS.File_Type; |
| 184 | File_Len : File_Length; |
| 185 | Success : Boolean; |
| 186 | begin |
| 187 | if Comp_First > Comp_Last then |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 188 | return; |
Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 189 | end if; |
| 190 | |
| 191 | Open |
| 192 | (State => State, |
| 193 | File_Len => File_Len, |
| 194 | File_Type => File_Type, |
| 195 | File_Name => Path (Comp_First .. Comp_Last), |
| 196 | In_Root => Root_Dir, |
| 197 | Success => Success); |
| 198 | if not Success then |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 199 | return; |
Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 200 | end if; |
| 201 | Root_Dir := False; |
| 202 | |
| 203 | case File_Type is |
| 204 | when Dir => |
| 205 | if Comp_Last = Path_Last then |
| 206 | Close (State); |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 207 | return; |
Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 208 | end if; |
| 209 | Path_Pos := Comp_Last + 1; |
| 210 | when Regular => |
| 211 | if Comp_Last = Path_Last then |
| 212 | Set_File_Max (File_Len); |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 213 | Ret := 1; |
| 214 | return; |
Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 215 | else |
| 216 | Close (State); |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 217 | return; |
Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 218 | end if; |
| 219 | when Link => |
Nico Huber | 53df785 | 2024-01-15 18:36:04 +0100 | [diff] [blame] | 220 | if File_Len = 0 or File_Len > Path_Max then |
Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 221 | Success := False; |
| 222 | else |
| 223 | Read_Link |
| 224 | (Path => Path, |
| 225 | Rest_First => Comp_Last + 1, |
| 226 | Rest_Last => Path_Last, |
Nico Huber | 53df785 | 2024-01-15 18:36:04 +0100 | [diff] [blame] | 227 | Link_Len => Positive (File_Len), |
Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 228 | Success => Success); |
| 229 | end if; |
| 230 | Close (State); |
| 231 | if not Success then |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 232 | return; |
Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 233 | end if; |
| 234 | Root_Dir := Path (1) = '/'; |
| 235 | exit Path_Loop; -- continue in Link_Loop |
| 236 | end case; |
| 237 | end; |
| 238 | end loop Path_Loop; |
| 239 | end; |
| 240 | end loop Link_Loop; |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 241 | end Open; |
Nico Huber | b1cb2d3 | 2023-12-17 01:45:47 +0100 | [diff] [blame] | 242 | |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 243 | function C_Open (File_Path : System.Address) return int |
| 244 | with |
| 245 | SPARK_Mode => Off |
| 246 | is |
| 247 | Ret : int; |
| 248 | begin |
| 249 | if Is_Mounted (State) then |
| 250 | Open (File_Path, Ret); |
| 251 | else |
| 252 | Ret := 0; |
| 253 | end if; |
| 254 | return Ret; |
Nico Huber | 0a9591e | 2023-11-27 16:59:11 +0100 | [diff] [blame] | 255 | end C_Open; |
Nico Huber | 51f6041 | 2023-12-04 14:48:11 +0100 | [diff] [blame] | 256 | |
| 257 | procedure C_Close is |
| 258 | begin |
| 259 | if Is_Open (State) then |
| 260 | Close (State); |
| 261 | end if; |
| 262 | end C_Close; |
Nico Huber | 0a9591e | 2023-11-27 16:59:11 +0100 | [diff] [blame] | 263 | |
| 264 | function C_Read (Buf : System.Address; Len : int) return int |
Nico Huber | 3da2147 | 2023-12-18 15:43:35 +0100 | [diff] [blame] | 265 | with |
| 266 | SPARK_Mode => Off |
Nico Huber | 0a9591e | 2023-11-27 16:59:11 +0100 | [diff] [blame] | 267 | is |
| 268 | subtype Buffer_Range is Natural range 0 .. Integer (Len) - 1; |
| 269 | Buffer : Buffer_Type (Buffer_Range) |
| 270 | with |
| 271 | Convention => C, |
| 272 | Address => Buf; |
| 273 | |
Thomas Heijligen | 5c43abc | 2023-12-11 15:24:36 +0000 | [diff] [blame] | 274 | File_Pos : File_Offset := FILO.FS.File_Pos; |
Nico Huber | 0a9591e | 2023-11-27 16:59:11 +0100 | [diff] [blame] | 275 | Read_Len : Natural; |
| 276 | begin |
Nico Huber | 51f6041 | 2023-12-04 14:48:11 +0100 | [diff] [blame] | 277 | if Is_Open (State) then |
Nico Huber | 549a1b8 | 2023-12-17 01:51:59 +0100 | [diff] [blame] | 278 | Read (State, File_Pos, Buffer, Read_Len); |
Nico Huber | 51f6041 | 2023-12-04 14:48:11 +0100 | [diff] [blame] | 279 | Set_File_Pos (File_Pos); |
| 280 | else |
| 281 | Read_Len := 0; |
| 282 | end if; |
Nico Huber | 0a9591e | 2023-11-27 16:59:11 +0100 | [diff] [blame] | 283 | return int (Read_Len); |
| 284 | end C_Read; |
| 285 | |
Thomas Heijligen | 5c43abc | 2023-12-11 15:24:36 +0000 | [diff] [blame] | 286 | end FILO.FS.VFS; |