4.2.9. GNATCOLL.File_Indexes

package GNATCOLL.File_Indexes is

   package UTF8 renames Ada.Strings.UTF_Encoding;
   package Stat renames GNATCOLL.OS.Stat;
   package Blake3 renames GNATCOLL.Hash.Blake3;

   type File_Index is private;
   --  A database tracking SHA1 of a set of files. The caching mechanism
   --  ensures that SHA1 for a given file is recomputed only when the file
   --  changes

   subtype File_Index_Digest is Blake3.Blake3_Digest;
   --  Digest returned by the distinct Hash functions. Currently the file index
   --  uses the Blake3 hash which is safe and efficient.

   type Entry_State is
     (UNCHANGED_FILE, UPDATED_FILE, NEW_FILE, REMOVED_FILE, UNHASHABLE_FILE);
   --  Indication returned when doing a SHA1 query in File_Index:
   --
   --  UNCHANGED_FILE: returned when SHA1 did not change since last query
   --  UPDATED_FILE: returned when file was in the File_Index but SHA1 has been
   --    recomputed as a file change has been detected
   --  NEW_FILE: returned when SHA1 is computed because file was not present in
   --    the index
   --  REMOVED_FILE: returned whenever a file does not exist anymore.
   --  UNHASHABLE_FILE: returned when a file content hash cannot be computed
   --    (file does not exist, is not a file or content cannot be accessed).

   function Indexed_Content_Size (Self : File_Index) return Long_Long_Integer;
   --  Return the total number of bytes that have been indexed

   procedure Hash
      (Self   : in out File_Index;
       Path   : UTF8.UTF_8_String;
       Attrs  : Stat.File_Attributes;
       State  : out Entry_State;
       Digest : out File_Index_Digest)
   with Inline => True;
   --  Get the hash digest for the file located at Path and with file
   --  attributes Attrs (obtained with a call to GNATCOLL.OS.Stat). See
   --  Entry_State documentation for the meaning of State.
   --  Note that this version of the Hash function is interesting when
   --  iterating on a directory using GNATCOLL.OS.Dir. Indeed the Dir_Entry
   --  already contains the stat information for the given and thus this avoid
   --  calling stat a second time (specially efficient on Windows platform).

   procedure Hash
      (Self   : in out File_Index;
       Path   : UTF8.UTF_8_String;
       State  : out Entry_State;
       Digest : out File_Index_Digest);
   --  Same as previous function except that a call to Stat is done
   --  automatically to get file attributes.

   function Hash (Self : in out File_Index; Path : UTF8.UTF_8_String)
      return File_Index_Digest;
   --  Same as previous function without State as output.

   --  procedure Save_Index (Self : File_Index; Filename : UTF8.UTF_8_String);
   --  Dump a File_Index on disk

   --  function Load_Index (Filename : UTF8.UTF_8_String) return File_Index;
   --  Load a File_Index from disk

   procedure Clear_Cache (Self : in out File_Index);
   --  Clear the index content.

end GNATCOLL.File_Indexes;