1--
2--  Copyright (c) 2014-15 John Marino <draco@marino.st>
3--
4--  Permission to use, copy, modify, and distribute this software for any
5--  purpose with or without fee is hereby granted, provided that the above
6--  copyright notice and this permission notice appear in all copies.
7--
8--  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9--  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10--  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11--  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12--  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13--  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14--  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15--
16
17
18with DragonFly.HAMMER.Binding;
19with Ada.Calendar;
20with Ada.Calendar.Time_Zones;
21with Ada.Containers.Vectors;
22
23package DragonFly.HAMMER.History is
24
25   package HB renames DragonFly.HAMMER.Binding;
26   package CAL renames Ada.Calendar;
27   package CTZ renames Ada.Calendar.Time_Zones;
28
29
30   ---------------------------
31   --  Types and Constants  --
32   ---------------------------
33
34   type flush_state   is (dirty, clean);
35   type search_result is (found, deleted, not_found, secret);
36
37   subtype Trax       is String (1 .. 20);
38   subtype TraxTime   is String (1 .. 19);
39
40   failed_search : constant Trax := "@@0xFAIL            ";
41
42   type Transaction is record
43      trax_id   : Trax;
44      timestamp : TraxTime;
45   end record;
46
47   package Transaction_Container is
48      new Ada.Containers.Vectors (Positive, Transaction);
49
50   type scan_result is record
51      state      : flush_state;
52      path_check : search_result;
53      diff_old   : Positive;
54      diff_new   : Positive;
55      history    : Transaction_Container.Vector;
56   end record;
57
58   -----------------
59   --  Functions  --
60   -----------------
61
62   procedure scan_history (path : in String; result : out scan_result);
63   --   This saves hammer history or the latest found version in the case
64   --   of a deleted file.
65
66   function match_against_directory_trax (filename : String) return Trax;
67   --  This returns the first transaction that has a prefix of filename.
68   --  It is only used if the file doesn't exist, so we are looking for
69   --  deleted files.
70
71   function modification_timestamp (path : in String) return TraxTime;
72   --  Convert file modification time YYYY-MM-DD HH:MM:SS format
73   --  There is no handling for bad path
74
75private
76
77   tz_offset : constant CTZ.Time_Offset := CTZ.UTC_Time_Offset;
78
79   function format_as_hex (bignum : HB.hammer_tid) return String;
80   --  Format string with c-equivalent of %016x
81
82   function format_timestamp (raw : uInt32) return TraxTime;
83   --  Convert uInt32 to YYYY-MM-DD HH:MM:SS format with system offset
84
85   procedure collect_history (
86               fd        : in DragonFly.file_descriptor;
87               filename  : in String;
88               suffix    : out Trax;
89               timestamp : out uInt32);
90   --  Used to find latest version of a deleted file
91
92end DragonFly.HAMMER.History;
93