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 System;
19with Interfaces.C;
20with Interfaces.C.Strings;
21
22package DragonFly.HAMMER.Binding is
23
24   package IC renames Interfaces.C;
25
26
27   -------------------------------
28   ---  DATA TYPE DEFINITIONS  ---
29   -------------------------------
30
31   subtype uLong           is IC.unsigned_long;
32   subtype hammer_tid      is uInt64;
33   subtype hammer_off      is uInt64;
34   subtype hammer_seq      is uInt32;
35   subtype hammer_crc      is uInt32;
36   subtype ioctl_command   is uLong;
37
38
39   -------------------
40   ---  CONSTANTS  ---
41   -------------------
42
43   HAMMER_MAX_HISTORY_ELMS     : constant Int32  := 64;
44   HAMMER_IOC_HISTORY_ATKEY    : constant uInt32 := 16#01#;
45   HAMMER_IOC_HISTORY_NEXT_TID : constant uInt32 := 16#02#;
46   HAMMER_IOC_HISTORY_NEXT_KEY : constant uInt32 := 16#04#;
47   HAMMER_IOC_HISTORY_EOF      : constant uInt32 := 16#08#;
48   HAMMER_IOC_HISTORY_UNSYNCED : constant uInt32 := 16#10#;
49   HAMMER_MIN_TID              : constant hammer_tid := 0;
50   HAMMER_MAX_TID              : constant hammer_tid := 16#FFFFFFFFFFFFFFFF#;
51   HAMMER_MAX_KEY              : constant Int64      := 16#7FFFFFFFFFFFFFFF#;
52   O_RDONLY                    : constant uInt32 := 0;       -- fcntl.h
53
54   --  These constants are revealed by dev-tools/print-ioctls.c
55
56   HAMMERIOC_GETHISTORY        : constant ioctl_command := 16#c4506802#;
57
58
59   --------------------
60   ---  STRUCTURES  ---
61   --------------------
62
63   type hammer_ioc_head is record
64      flags      : uInt32;
65      error      : uInt32;
66      reserved01 : uInt32;
67      reserved02 : uInt32;
68      reserved03 : uInt32;
69      reserved04 : uInt32;
70   end record;
71
72
73   type hammer_ioc_hist_entry is record
74      tid        : hammer_tid;
75      time32     : uInt32;
76      unused     : uInt32;
77   end record;
78   type history is array (0 .. HAMMER_MAX_HISTORY_ELMS - 1) of
79                   hammer_ioc_hist_entry;
80
81   type hammer_ioc_history is record
82      head       : hammer_ioc_head;
83      obj_id     : Int64;
84      beg_tid    : hammer_tid;
85      nxt_tid    : hammer_tid;
86      end_tid    : hammer_tid;
87      key        : Int64;
88      nxt_key    : Int64;
89      count      : Int32;
90      reserved01 : Int32;
91      hist_ary   : history;
92   end record;
93   type history_access is access hammer_ioc_history;
94
95
96   ------------------
97   --  FUNCTIONS  ---
98   ------------------
99
100   function ioctl (
101      descriptor : file_descriptor;
102      command    : ioctl_command;
103      argument   : System.Address)
104   return Int32;
105   pragma Import (C, ioctl, "ioctl");
106   --  The ioctl() system call manipulates the underlying device parameters
107   --  of special files.  In particular, many operating characteristics of
108   --  character special files (e.g. terminals) may be controlled with
109   --  ioctl() requests.  The descriptor must point to an open file.
110
111
112   procedure retrieve_history (
113                open_file : in     file_descriptor;
114                history   : in out hammer_ioc_history);
115   --  The procedure is passed an open file descriptor and a pointer to
116   --  the hammer history structure which is populated when the procedure
117   --  returns.  The IOCTL_Failure exception can be raised if the internal
118   --  call to ioctl() fails.
119
120
121   function initialize_history (offset : Int64 := 0) return hammer_ioc_history;
122   --  Returns an initialized hammer_ioc_history record that can be passed
123   --  to the retrieve history function
124
125
126   function last_error_message return String;
127   --  Print a text description of the last seen error
128   --  This is normally called as result of a raised exception that is
129   --  caused by an error captured in the system's errno variable
130
131
132   function open_file_for_reading (path : String) return file_descriptor;
133   --  Given a path, this function will open the file for reading and
134   --  return a file descriptor, otherwise it will raise an exception
135
136
137   procedure close_file (descriptor : file_descriptor);
138   --  Given a file descriptor to an open file, this function will close
139   --  the file.
140
141
142private
143
144   function strerror (error_number : Integer) return IC.Strings.chars_ptr;
145   pragma Import (C, strerror, "strerror");
146   --  Get pointer to text description of the last error
147
148   function fd_open (path : IC.Strings.chars_ptr; flags : uInt32)
149   return file_descriptor;
150   pragma Import (C, fd_open, "open");
151   --  Return an open file descriptor given a path
152
153   function fd_close (d : file_descriptor) return Int32;
154   pragma Import (C, fd_close, "close");
155   --  Close an open file given its descriptor
156
157end DragonFly.HAMMER.Binding;
158