| package FILO.FS.Ext2 is |
| |
| type T is private; |
| |
| function Is_Mounted (State : T) return Boolean; |
| function Is_Open (State : T) return Boolean |
| with |
| Post => (if Is_Open'Result then Is_Mounted (State)); |
| |
| procedure Mount |
| (State : in out T; |
| Part_Len : in Partition_Length; |
| Success : out Boolean) |
| with |
| Pre => not Is_Mounted (State), |
| Post => Success = Is_Mounted (State); |
| |
| procedure Open |
| (State : in out T; |
| File_Len : out File_Length; |
| File_Type : out FS.File_Type; |
| File_Name : in String; |
| In_Root : in Boolean; |
| Success : out Boolean) |
| with |
| Pre => Is_Mounted (State), |
| Post => Success = Is_Open (State); |
| |
| procedure Close (State : in out T) |
| with |
| Pre => Is_Open (State), |
| Post => Is_Mounted (State); |
| |
| procedure Read |
| (State : in out T; |
| File_Pos : in out File_Offset; |
| Buf : out Buffer_Type; |
| Len : out Natural) |
| with |
| Pre => Is_Open (State), |
| Post => Is_Open (State); |
| |
| private |
| type State is (Unmounted, Mounted, File_Opened); |
| |
| -- maximum block size is 64KiB (2^16): |
| subtype Log_Block_Size is Positive range 10 .. 16; |
| subtype Max_Block_Index is Index_Type range 0 .. 2 ** Log_Block_Size'Last - 1; |
| |
| -- Minimum ext2 block size is 1KiB (two 512B blocks) |
| type FSBlock_Offset is new Block_Offset range 0 .. Block_Offset'Last / 2; |
| type FSBlock_Logical is new Block_Offset range 0 .. Block_Offset'Last / 2; |
| |
| -- We use a 64KiB cache which fits at least one ext2 block. If a lower |
| -- block size is encountered (likely), we partition the cache into up |
| -- to 64 1KiB blocks. |
| -- Cache entries can be used for blocks that map logical block offets |
| -- to physical ones. Then we note the first logical block offset mapped |
| -- by the cached block. Otherwise, we use their physical offset. |
| type Cache_Label is new Unsigned_64; |
| subtype Block_Cache_Index is Natural range 0 .. 63; |
| type Cache_Label_Logical is array (Block_Cache_Index) of Boolean; |
| type Cache_Label_Array is array (Block_Cache_Index) of Cache_Label; |
| |
| -- Same as the 12 direct + 3 indirect blocks times 4B: |
| subtype Inode_Extents_Index is Natural range 0 .. 59; |
| subtype Inode_Extents is Buffer_Type (Inode_Extents_Index); |
| |
| type Inode_Index is new Unsigned_32 range 2 .. Unsigned_32'Last; |
| subtype Inode_Size is Positive range 128 .. Positive (Unsigned_16'Last); |
| subtype Desc_Size is Positive range 32 .. 2 ** 15; -- power-of-2 that fits in 16 bits |
| |
| type Inode_Type is (Dir, Regular, Link, Fast_Link); |
| type Inode_Length is range 0 .. 2 ** 46; |
| |
| type Inode_Info is record |
| I : Inode_Index := Inode_Index'First; |
| Mode : Inode_Type := Inode_Type'First; |
| Size : Inode_Length := 0; |
| Use_Extents : Boolean := False; |
| Inline : Buffer_Type (Inode_Extents_Index) := (others => 16#00#); |
| end record; |
| |
| type T is record |
| S : State; |
| Part_Len : Partition_Length := 0; |
| First_Data_Block : FSBlock_Offset := 0; |
| Block_Size_Bits : Log_Block_Size := 10; |
| Inodes_Per_Group : Inode_Index := Inode_Index'First; |
| Inode_Size : Ext2.Inode_Size := Ext2.Inode_Size'First; |
| Desc_Size : Ext2.Desc_Size := Ext2.Desc_Size'First; |
| Feature_Extents : Boolean := False; |
| Feature_64Bit : Boolean := False; |
| Inode : Inode_Info := (others => <>); |
| Cur_Dir : Inode_Index := Inode_Index'First; |
| Block_Cache_Logical : Cache_Label_Logical := (others => False); |
| Block_Cache_Label : Cache_Label_Array := (others => 0); |
| Block_Cache : Buffer_Type (Max_Block_Index) := (others => 16#00#); |
| end record; |
| |
| end FILO.FS.Ext2; |