Skip to content

Commit

Permalink
Filesystem: Create a "front-end" for file handling
Browse files Browse the repository at this point in the history
in a package called File_IO. This hides some of the complexity of
dealing with access to handle and answers some of the questions
discussed in the issue #68.

The package also provides the mounting interface, taken from
Filesystem.VFS.
  • Loading branch information
Fabien-Chouteau committed Aug 4, 2017
1 parent cad2a50 commit a06119f
Show file tree
Hide file tree
Showing 28 changed files with 1,243 additions and 839 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
with Ada.Directories;
with Ada.Unchecked_Deallocation;

package body Native.Filesystem is
package body Filesystem.Native is

-- ??? There are a bunch of 'Unrestricted_Access here because the
-- HAL.Filesystem API embeds implicit references to filesystems. It
Expand Down Expand Up @@ -265,8 +265,8 @@ package body Native.Filesystem is

overriding
function Get_FS
(This : Directory_Handle) return Any_Filesystem
is (Any_Filesystem (This.FS));
(This : Directory_Handle) return Any_Filesystem_Driver
is (Any_Filesystem_Driver (This.FS));

---------------
-- Root_Node --
Expand Down Expand Up @@ -484,8 +484,8 @@ package body Native.Filesystem is

overriding
function Get_FS
(This : in out File_Handle) return Any_Filesystem
is (Any_Filesystem (This.FS));
(This : in out File_Handle) return Any_Filesystem_Driver
is (Any_Filesystem_Driver (This.FS));

----------
-- Size --
Expand Down Expand Up @@ -516,16 +516,20 @@ package body Native.Filesystem is
return Status_Code
is
Data : UInt8_Array (1 .. Natural (Length)) with Address => Addr;
Ret : File_Size := 0;
begin
for B of Data loop
Byte_IO.Read (This.File, B);
Ret := Ret + 1;
end loop;
Length := Ret;
return OK;
exception
when Byte_IO.Mode_Error
| Byte_IO.End_Error
| Byte_IO.Data_Error
=>
Length := Ret;
return Generic_Error;
end Read;

Expand Down Expand Up @@ -668,9 +672,8 @@ package body Native.Filesystem is
------------

overriding
function Get_FS (This : Node_Handle) return Any_Filesystem
is (Any_Filesystem (This.FS));

function Get_FS (This : Node_Handle) return Any_Filesystem_Driver
is (Any_Filesystem_Driver (This.FS));

--------------
-- Basename --
Expand Down Expand Up @@ -794,4 +797,4 @@ package body Native.Filesystem is
return +Result;
end Join;

end Native.Filesystem;
end Filesystem.Native;
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ with System;
-- Simple wrappers around the Ada standard library to provide implementations
-- for HAL.Filesystem interfaces.

package Native.Filesystem is
package Filesystem.Native is

package HALFS renames HAL.Filesystem;

Expand Down Expand Up @@ -40,11 +40,6 @@ package Native.Filesystem is
return Status_Code;
-- Open a new Directory Handle at the given Filesystem Path

overriding
function Create_File (This : in out Native_FS_Driver;
Path : String)
return Status_Code;

overriding
function Unlink (This : in out Native_FS_Driver;
Path : String)
Expand All @@ -59,7 +54,7 @@ package Native.Filesystem is

overriding
function Get_FS
(This : Directory_Handle) return Any_Filesystem;
(This : Directory_Handle) return Any_Filesystem_Driver;
-- Return the filesystem the handle belongs to.

overriding
Expand Down Expand Up @@ -91,7 +86,7 @@ package Native.Filesystem is
---------------------

overriding
function Get_FS (This : Node_Handle) return Any_Filesystem;
function Get_FS (This : Node_Handle) return Any_Filesystem_Driver;

overriding
function Basename (This : Node_Handle) return String;
Expand Down Expand Up @@ -138,7 +133,7 @@ package Native.Filesystem is

overriding
function Get_FS
(This : in out File_Handle) return Any_Filesystem;
(This : in out File_Handle) return Any_Filesystem_Driver;

overriding
function Size
Expand Down Expand Up @@ -250,7 +245,7 @@ private

package Byte_IO is new Ada.Direct_IO (UInt8);

type Native_FS_Driver is limited new HAL.Filesystem.Filesystem with record
type Native_FS_Driver is limited new Filesystem_Driver with record
Root_Dir : Ada.Strings.Unbounded.Unbounded_String;
-- Path on the host file system to be used as root directory for this FS

Expand Down Expand Up @@ -334,4 +329,4 @@ private
-- Current index in the vector of Node
end record;

end Native.Filesystem;
end Filesystem.Native;
17 changes: 9 additions & 8 deletions middleware/src/bitmap/bitmap_file_output.adb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

with Interfaces; use Interfaces;
with HAL; use HAL;
with Filesystem; use Filesystem;

package body Bitmap_File_Output is

Expand Down Expand Up @@ -71,7 +70,7 @@ package body Bitmap_File_Output is
-- Write_BMP_File --
--------------------

procedure Write_BMP_File (File : Any_File_Handle;
procedure Write_BMP_File (File : in out File_Descriptor;
Bitmap : Bitmap_Buffer'Class)
is
Hdr : Header;
Expand All @@ -84,8 +83,8 @@ package body Bitmap_File_Output is
Pix_Out : UInt8_Array (1 .. 3);
Padding : constant UInt8_Array (1 .. Integer (Row_Padding)) := (others => 0);

function Write is new Filesystem.Generic_Write (Header);
function Write is new Filesystem.Generic_Write (Info);
-- function Write is new Generic_Write (Header);
-- function Write is new Generic_Write (Info);
begin
Hdr.Signature := 16#4D42#;
Hdr.Size := (Data_Size + 54) / 4;
Expand All @@ -107,11 +106,11 @@ package body Bitmap_File_Output is
Inf.Important := 0;


if Write (File, Hdr) /= OK then
if Write (File, Hdr'Address, Hdr'Size / 8) /= (Hdr'Size / 8) then
raise Program_Error;
end if;

if Write (File, Inf) /= OK then
if Write (File, Inf'Address, Inf'Size / 8) /= (Inf'Size / 8) then
raise Program_Error;
end if;

Expand All @@ -124,12 +123,14 @@ package body Bitmap_File_Output is
Pix_Out (2) := RGB_Pix.Green;
Pix_Out (3) := RGB_Pix.Red;

if File.Write (Pix_Out'Address, Pix_Out'Length) /= OK then
if Write (File, Pix_Out'Address, Pix_Out'Length) /= Pix_Out'Length
then
raise Program_Error;
end if;
end loop;

if File.Write (Padding'Address, Padding'Length) /= OK then
if Write (File, Padding'Address, Padding'Length) /= Padding'Length
then
raise Program_Error;
end if;
end loop;
Expand Down
6 changes: 3 additions & 3 deletions middleware/src/bitmap/bitmap_file_output.ads
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
-- --
------------------------------------------------------------------------------

with HAL.Filesystem; use HAL.Filesystem;
with HAL.Bitmap; use HAL.Bitmap;
with File_IO; use File_IO;
with HAL.Bitmap; use HAL.Bitmap;

package Bitmap_File_Output is

procedure Write_BMP_File (File : Any_File_Handle;
procedure Write_BMP_File (File : in out File_Descriptor;
Bitmap : Bitmap_Buffer'Class);

end Bitmap_File_Output;
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
------------------------------------------------------------------------------

with Ada.Unchecked_Conversion;
with HAL; use HAL;

package body Filesystem.FAT.Directories is

Expand Down
1 change: 1 addition & 0 deletions middleware/src/filesystem/FAT/filesystem-fat-files.adb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ with Filesystem.FAT.Directories;

package body Filesystem.FAT.Files is


function Absolute_Block (File : in out FAT_File_Handle) return Block_Number
with Inline_Always;

Expand Down
3 changes: 1 addition & 2 deletions middleware/src/filesystem/FAT/filesystem-fat-files.ads
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ private package Filesystem.FAT.Files is
function Read
(File : in out FAT_File_Handle;
Addr : System.Address;
Length : in out FAT_File_Size) return Status_Code
with Pre => Mode (File) /= Write_Mode;
Length : in out FAT_File_Size) return Status_Code;
-- read data from file.
-- @return number of bytes read (at most Data'Length), or -1 on error.

Expand Down
7 changes: 7 additions & 0 deletions middleware/src/filesystem/FAT/filesystem-fat.adb
Original file line number Diff line number Diff line change
Expand Up @@ -1146,4 +1146,11 @@ package body Filesystem.FAT is
null;
end Close;

----------
-- Size --
----------

overriding function Size (E : FAT_Node) return File_Size
is (File_Size (E.Size));

end Filesystem.FAT;
26 changes: 11 additions & 15 deletions middleware/src/filesystem/FAT/filesystem-fat.ads
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

with System;
with Interfaces; use Interfaces;
with HAL; use HAL;
with HAL.Block_Drivers; use HAL.Block_Drivers;
with HAL.Filesystem; use HAL.Filesystem;

Expand All @@ -51,7 +50,7 @@ package Filesystem.FAT is

type FAT_Name is private;

type FAT_Filesystem is limited new HAL.Filesystem.Filesystem with private;
type FAT_Filesystem is limited new Filesystem_Driver with private;

type FAT_Node is new Node_Handle with private;

Expand Down Expand Up @@ -101,7 +100,7 @@ package Filesystem.FAT is
overriding function Size (E : FAT_Node) return File_Size;
overriding procedure Close (E : in out FAT_Node);
overriding function Get_FS
(E : FAT_Node) return Any_Filesystem;
(E : FAT_Node) return Any_Filesystem_Driver;

-------------------
-- FILE HANDLING --
Expand Down Expand Up @@ -291,7 +290,7 @@ private
Last_Allocated_Cluster at 8 range 0 .. 31;
end record;

type FAT_Filesystem is limited new HAL.Filesystem.Filesystem with record
type FAT_Filesystem is limited new Filesystem_Driver with record
Initialized : Boolean := False;
Disk_Parameters : FAT_Disk_Parameter;
LBA : Block_Number;
Expand Down Expand Up @@ -402,7 +401,7 @@ private
type Any_FAT_Directory_Handle is access all FAT_Directory_Handle'Class;

overriding function Get_FS
(Dir : FAT_Directory_Handle) return Any_Filesystem;
(Dir : FAT_Directory_Handle) return Any_Filesystem_Driver;

overriding function Read
(Dir : in out FAT_Directory_Handle;
Expand Down Expand Up @@ -487,7 +486,7 @@ private
type FAT_File_Handle_Access is access all FAT_File_Handle;

overriding function Get_FS
(File : in out FAT_File_Handle) return Any_Filesystem;
(File : in out FAT_File_Handle) return Any_Filesystem_Driver;

overriding function Size (File : FAT_File_Handle)
return File_Size;
Expand Down Expand Up @@ -701,15 +700,15 @@ private
-- for the trailing ASCII.NUL + 0xFFFF sequence.

overriding function Get_FS
(Dir : FAT_Directory_Handle) return Any_Filesystem
is (Any_Filesystem (Dir.FS));
(Dir : FAT_Directory_Handle) return Any_Filesystem_Driver
is (Any_Filesystem_Driver (Dir.FS));

overriding function Get_FS
(File : in out FAT_File_Handle) return Any_Filesystem
is (Any_Filesystem (File.FS));
(File : in out FAT_File_Handle) return Any_Filesystem_Driver
is (Any_Filesystem_Driver (File.FS));

overriding function Get_FS (E : FAT_Node) return Any_Filesystem
is (Any_Filesystem (E.FS));
overriding function Get_FS (E : FAT_Node) return Any_Filesystem_Driver
is (Any_Filesystem_Driver (E.FS));

function Long_Name (E : FAT_Node) return FAT_Name
is (if E.L_Name.Len > 0 then E.L_Name else Short_Name (E));
Expand Down Expand Up @@ -741,9 +740,6 @@ private
function Get_Start_Cluster (E : FAT_Node) return Cluster_Type
is (E.Start_Cluster);

overriding function Size (E : FAT_Node) return File_Size
is (File_Size (E.Size));

function Size (E : FAT_Node) return FAT_File_Size
is (E.Size);

Expand Down
Loading

0 comments on commit a06119f

Please sign in to comment.