magicada_1.0.1_32af2d9a/src/magic-manager.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
-- --------------------------------------------------------------------
--  magic-manager -- magic manager to help in using the libmagic library
--  Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--  SPDX-License-Identifier: Apache-2.0
-----------------------------------------------------------------------

package body Magic.Manager is

   package ICS renames Interfaces.C.Strings;
   use type ICS.chars_ptr;

   --  ------------------------------
   --  Initialize the magic manager and prepare the libmagic library.
   --  ------------------------------
   procedure Initialize (Manager : in out Magic_Manager;
                         Config  : in Flags;
                         Path    : in String) is
      E : Integer;
      S : ICS.chars_ptr;
   begin
      if Manager.Magic_Cookie = null then
         Manager.Magic_Cookie := Open (Config);
         if Manager.Magic_Cookie = null then
            raise Error;
         end if;
      else
         E := Set_Flags (Manager.Magic_Cookie, Config);
         if E /= 0 then
            raise Error;
         end if;
      end if;

      S := ICS.New_String (Path);
      E := Load (Manager.Magic_Cookie, S);
      ICS.Free (S);
      if E /= 0 then
         raise Error;
      end if;
   end Initialize;

   --  ------------------------------
   --  Check whether the manager and the libmagic library is initialized.
   --  ------------------------------
   function Is_Initialized (Manager : in Magic_Manager) return Boolean is
   begin
      return Manager.Magic_Cookie /= null;
   end Is_Initialized;

   --  ------------------------------
   --  Identify the content of the buffer by using the libmagic library.
   --  ------------------------------
   function Identify (Manager : in Magic_Manager;
                      Data    : in Ada.Streams.Stream_Element_Array)
                      return String is
      R : Interfaces.C.Strings.chars_ptr;
   begin
      R := Buffer (Manager.Magic_Cookie,
                   Data'Address, Interfaces.C.size_t (Data'Length));
      if R /= Interfaces.C.Strings.Null_Ptr then
         return Interfaces.C.Strings.Value (R);
      else
         raise Error;
      end if;
   end Identify;

   --  ------------------------------
   --  Identify the content of the file.
   --  ------------------------------
   function Identify (Manager : in Magic_Manager;
                      Path    : in String) return String is
      S : ICS.chars_ptr := ICS.New_String (Path);
      R : Interfaces.C.Strings.chars_ptr;
   begin
      R := File (Manager.Magic_Cookie, S);
      ICS.Free (S);
      if R /= Interfaces.C.Strings.Null_Ptr then
         return Interfaces.C.Strings.Value (R);
      else
         raise Error;
      end if;
   end Identify;

   --  ------------------------------
   --  Return the last error.
   --  ------------------------------
   function Last_Error (Manager : in Magic_Manager) return String is
      S : constant ICS.chars_ptr := Magic.Error (Manager.Magic_Cookie);
   begin
      if S /= ICS.Null_Ptr then
         return Interfaces.C.Strings.Value (S);
      else
         return "";
      end if;
   end Last_Error;

   function Last_Errno (Manager : in Magic_Manager) return Integer is
   begin
      return Errno (Manager.Magic_Cookie);
   end Last_Errno;

   overriding
   procedure Finalize (Manager : in out Magic_Manager) is
   begin
      if Manager.Magic_Cookie /= null then
         Close (Manager.Magic_Cookie);
         Manager.Magic_Cookie := null;
      end if;
   end Finalize;

end Magic.Manager;