generator: Add Pointer parameter type to the generator.
[libguestfs.git] / generator / generator_types.ml
1 (* libguestfs
2  * Copyright (C) 2009-2010 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  *)
18
19 (* Please read generator/README first. *)
20
21 (* Types used to describe the API. *)
22
23 type style = ret * args * args
24     (* The [style] is a tuple which describes the return value and
25      * arguments of a function.
26      * 
27      * [ret] is the return value, one of the [R*] values below.
28      * 
29      * The second element is the list of required arguments, a list of
30      * [argt]s from the list below, eg. [Bool], [String] etc.  Each has
31      * a name so that for example [Int "foo"] corresponds in the C
32      * bindings to an [int foo] parameter.
33      * 
34      * The third element is the list of optional arguments.  These are
35      * mapped to optional arguments in the language binding, eg. in
36      * Perl to:
37      *   $g->fn (required1, required2, opt1 => val, opt2 => val);
38      * As the name suggests these are optional, and the function can
39      * tell which optional parameters were supplied by the caller.
40      * 
41      * Only [Bool], [Int], [Int64], [String] may currently appear in
42      * the optional argument list (we may permit more types in future).
43      *
44      * ABI and API considerations
45      * --------------------------
46      * 
47      * The return type and required arguments may not be changed after
48      * these have been published in a stable version of libguestfs,
49      * because doing so would break the ABI.
50      * 
51      * If a published function has one or more optional arguments,
52      * then the call can be extended without breaking the ABI or API.
53      * This is in fact the only way to change an existing function.
54      * There are limitations on this:
55      *
56      * (1) you may _only_ add extra elements at the end of the list
57      * (2) you may _not_ rearrange or rename or remove existing elements
58      * (3) you may _not_ add optional arguments to a function which did
59      *     not have any before (since this breaks the C ABI).
60      *)
61
62 and ret =
63     (* "RErr" as a return value means an int used as a simple error
64      * indication, ie. 0 or -1.
65      *)
66   | RErr
67
68     (* "RInt" as a return value means an int which is -1 for error
69      * or any value >= 0 on success.  Only use this for smallish
70      * positive ints (0 <= i < 2^30).
71      *)
72   | RInt of string
73
74     (* "RInt64" is the same as RInt, but is guaranteed to be able
75      * to return a full 64 bit value, _except_ that -1 means error
76      * (so -1 cannot be a valid, non-error return value).
77      *)
78   | RInt64 of string
79
80     (* "RBool" is a bool return value which can be true/false or
81      * -1 for error.
82      *)
83   | RBool of string
84
85     (* "RConstString" is a string that refers to a constant value.
86      * The return value must NOT be NULL (since NULL indicates
87      * an error).
88      *
89      * Try to avoid using this.  In particular you cannot use this
90      * for values returned from the daemon, because there is no
91      * thread-safe way to return them in the C API.
92      *)
93   | RConstString of string
94
95     (* "RConstOptString" is an even more broken version of
96      * "RConstString".  The returned string may be NULL and there
97      * is no way to return an error indication.  Avoid using this!
98      *)
99   | RConstOptString of string
100
101     (* "RString" is a returned string.  It must NOT be NULL, since
102      * a NULL return indicates an error.  The caller frees this.
103      *)
104   | RString of string
105
106     (* "RStringList" is a list of strings.  No string in the list
107      * can be NULL.  The caller frees the strings and the array.
108      *)
109   | RStringList of string
110
111     (* "RStruct" is a function which returns a single named structure
112      * or an error indication (in C, a struct, and in other languages
113      * with varying representations, but usually very efficient).  See
114      * after the function list below for the structures.
115      *)
116   | RStruct of string * string          (* name of retval, name of struct *)
117
118     (* "RStructList" is a function which returns either a list/array
119      * of structures (could be zero-length), or an error indication.
120      *)
121   | RStructList of string * string      (* name of retval, name of struct *)
122
123     (* Key-value pairs of untyped strings.  Turns into a hashtable or
124      * dictionary in languages which support it.  DON'T use this as a
125      * general "bucket" for results.  Prefer a stronger typed return
126      * value if one is available, or write a custom struct.  Don't use
127      * this if the list could potentially be very long, since it is
128      * inefficient.  Keys should be unique.  NULLs are not permitted.
129      *)
130   | RHashtable of string
131
132     (* "RBufferOut" is handled almost exactly like RString, but
133      * it allows the string to contain arbitrary 8 bit data including
134      * ASCII NUL.  In the C API this causes an implicit extra parameter
135      * to be added of type <size_t *size_r>.  The extra parameter
136      * returns the actual size of the return buffer in bytes.
137      *
138      * Other programming languages support strings with arbitrary 8 bit
139      * data.
140      *
141      * At the RPC layer we have to use the opaque<> type instead of
142      * string<>.  Returned data is still limited to the max message
143      * size (ie. ~ 2 MB).
144      *)
145   | RBufferOut of string
146
147 and args = argt list    (* Function parameters, guestfs handle is implicit. *)
148
149 and argt =
150   | Bool of string      (* boolean *)
151   | Int of string       (* int (smallish ints, signed, <= 31 bits) *)
152   | Int64 of string     (* any 64 bit int *)
153   | String of string    (* const char *name, cannot be NULL *)
154   | Device of string    (* /dev device name, cannot be NULL *)
155   | Pathname of string  (* file name, cannot be NULL *)
156   | Dev_or_Path of string (* /dev device name or Pathname, cannot be NULL *)
157   | OptString of string (* const char *name, may be NULL *)
158   | StringList of string(* list of strings (each string cannot be NULL) *)
159   | DeviceList of string(* list of Device names (each cannot be NULL) *)
160     (* Opaque buffer which can contain arbitrary 8 bit data.
161      * In the C API, this is expressed as <const char *, size_t> pair.
162      * Most other languages have a string type which can contain
163      * ASCII NUL.  We use whatever type is appropriate for each
164      * language.
165      * Buffers are limited by the total message size.  To transfer
166      * large blocks of data, use FileIn/FileOut parameters instead.
167      * To return an arbitrary buffer, use RBufferOut.
168      *)
169   | BufferIn of string
170     (* Key material / passphrase.  Eventually we should treat this
171      * as sensitive and mlock it into physical RAM.  However this
172      * is highly complex because of all the places that XDR-encoded
173      * strings can end up.  So currently the only difference from
174      * 'String' is the way that guestfish requests these parameters
175      * from the user.
176      *)
177   | Key of string
178     (* These are treated as filenames (simple string parameters) in
179      * the C API and bindings.  But in the RPC protocol, we transfer
180      * the actual file content up to or down from the daemon.
181      * FileIn: local machine -> daemon (in request)
182      * FileOut: daemon -> local machine (in reply)
183      * In guestfish (only), the special name "-" means read from
184      * stdin or write to stdout.
185      *)
186   | FileIn of string
187   | FileOut of string
188     (* This specifies an opaque pointer that is passed through
189      * untouched.  Only non-daemon functions are supported.
190      *
191      * Pointer ("foo *", "bar") translates to "foo *bar" in the
192      * C API.  The pointer ("bar") cannot be NULL.
193      *
194      * This is less well supported in other language bindings:
195      * if the pointer type is known then we may be able to produce
196      * a suitable binding, otherwise this is translated into a 64
197      * bit int.
198      *
199      * Functions with this parameter type are not supported at all
200      * in guestfish (the function must be declared "NotInFish" else
201      * you will get an error).  Also the function cannot contain
202      * tests, although we should fix this in future.
203      *)
204   | Pointer of (string * string)
205
206 type flags =
207   | ProtocolLimitWarning  (* display warning about protocol size limits *)
208   | DangerWillRobinson    (* flags particularly dangerous commands *)
209   | FishAlias of string   (* provide an alias for this cmd in guestfish *)
210   | FishOutput of fish_output_t (* how to display output in guestfish *)
211   | NotInFish             (* do not export via guestfish *)
212   | NotInDocs             (* do not add this function to documentation *)
213   | DeprecatedBy of string (* function is deprecated, use .. instead *)
214   | Optional of string    (* function is part of an optional group *)
215   | Progress              (* function can generate progress messages *)
216
217 and fish_output_t =
218   | FishOutputOctal       (* for int return, print in octal *)
219   | FishOutputHexadecimal (* for int return, print in hex *)
220
221 (* You can supply zero or as many tests as you want per API call.
222  *
223  * Note that the test environment has 3 block devices, of size 500MB,
224  * 50MB and 10MB (respectively /dev/sda, /dev/sdb, /dev/sdc), and
225  * a fourth ISO block device with some known files on it (/dev/sdd).
226  *
227  * Note for partitioning purposes, the 500MB device has 1015 cylinders.
228  * Number of cylinders was 63 for IDE emulated disks with precisely
229  * the same size.  How exactly this is calculated is a mystery.
230  *
231  * The ISO block device (/dev/sdd) comes from images/test.iso.
232  *
233  * To be able to run the tests in a reasonable amount of time,
234  * the virtual machine and block devices are reused between tests.
235  * So don't try testing kill_subprocess :-x
236  *
237  * Between each test we blockdev-setrw, umount-all, lvm-remove-all.
238  *
239  * Don't assume anything about the previous contents of the block
240  * devices.  Use 'Init*' to create some initial scenarios.
241  *
242  * You can add a prerequisite clause to any individual test.  This
243  * is a run-time check, which, if it fails, causes the test to be
244  * skipped.  Useful if testing a command which might not work on
245  * all variations of libguestfs builds.  A test that has prerequisite
246  * of 'Always' is run unconditionally.
247  *
248  * In addition, packagers can skip individual tests by setting the
249  * environment variables:     eg:
250  *   SKIP_TEST_<CMD>_<NUM>=1  SKIP_TEST_COMMAND_3=1  (skips test #3 of command)
251  *   SKIP_TEST_<CMD>=1        SKIP_TEST_ZEROFREE=1   (skips all zerofree tests)
252  *)
253 type tests = (test_init * test_prereq * test) list
254 and test =
255     (* Run the command sequence and just expect nothing to fail. *)
256   | TestRun of seq
257
258     (* Run the command sequence and expect the output of the final
259      * command to be the string.
260      *)
261   | TestOutput of seq * string
262
263     (* Run the command sequence and expect the output of the final
264      * command to be the list of strings.
265      *)
266   | TestOutputList of seq * string list
267
268     (* Run the command sequence and expect the output of the final
269      * command to be the list of block devices (could be either
270      * "/dev/sd.." or "/dev/hd.." form - we don't check the 5th
271      * character of each string).
272      *)
273   | TestOutputListOfDevices of seq * string list
274
275     (* Run the command sequence and expect the output of the final
276      * command to be the integer.
277      *)
278   | TestOutputInt of seq * int
279
280     (* Run the command sequence and expect the output of the final
281      * command to be <op> <int>, eg. ">=", "1".
282      *)
283   | TestOutputIntOp of seq * string * int
284
285     (* Run the command sequence and expect the output of the final
286      * command to be a true value (!= 0 or != NULL).
287      *)
288   | TestOutputTrue of seq
289
290     (* Run the command sequence and expect the output of the final
291      * command to be a false value (== 0 or == NULL, but not an error).
292      *)
293   | TestOutputFalse of seq
294
295     (* Run the command sequence and expect the output of the final
296      * command to be a list of the given length (but don't care about
297      * content).
298      *)
299   | TestOutputLength of seq * int
300
301     (* Run the command sequence and expect the output of the final
302      * command to be a buffer (RBufferOut), ie. string + size.
303      *)
304   | TestOutputBuffer of seq * string
305
306     (* Run the command sequence and expect the output of the final
307      * command to be a structure.
308      *)
309   | TestOutputStruct of seq * test_field_compare list
310
311     (* Run the command sequence and expect the output of the final
312      * command to be a string which is the hex MD5 of the content of
313      * the named file.
314      *)
315   | TestOutputFileMD5 of seq * string
316
317     (* Run the command sequence and expect the output of the final
318      * command to be a string which is a block device name (we don't
319      * check the 5th character of the string, so "/dev/sda" == "/dev/vda").
320      *)
321   | TestOutputDevice of seq * string
322
323   (* Run the command sequence and expect the final command (only)
324    * to fail.
325    *)
326   | TestLastFail of seq
327
328 and test_field_compare =
329   | CompareWithInt of string * int
330   | CompareWithIntOp of string * string * int
331   | CompareWithString of string * string
332   | CompareFieldsIntEq of string * string
333   | CompareFieldsStrEq of string * string
334
335 (* Test prerequisites. *)
336 and test_prereq =
337     (* Test always runs. *)
338   | Always
339
340     (* Test is currently disabled - eg. it fails, or it tests some
341      * unimplemented feature.
342      *)
343   | Disabled
344
345     (* 'string' is some C code (a function body) that should return
346      * true or false.  The test will run if the code returns true.
347      *)
348   | If of string
349
350     (* As for 'If' but the test runs _unless_ the code returns true. *)
351   | Unless of string
352
353     (* Run the test only if 'string' is available in the daemon. *)
354   | IfAvailable of string
355
356 (* Some initial scenarios for testing. *)
357 and test_init =
358     (* Do nothing, block devices could contain random stuff including
359      * LVM PVs, and some filesystems might be mounted.  This is usually
360      * a bad idea.
361      *)
362   | InitNone
363
364     (* Block devices are empty and no filesystems are mounted. *)
365   | InitEmpty
366
367     (* /dev/sda contains a single partition /dev/sda1, with random
368      * content.  /dev/sdb and /dev/sdc may have random content.
369      * No LVM.
370      *)
371   | InitPartition
372
373     (* /dev/sda contains a single partition /dev/sda1, which is formatted
374      * as ext2, empty [except for lost+found] and mounted on /.
375      * /dev/sdb and /dev/sdc may have random content.
376      * No LVM.
377      *)
378   | InitBasicFS
379
380     (* /dev/sda:
381      *   /dev/sda1 (is a PV):
382      *     /dev/VG/LV (size 8MB):
383      *       formatted as ext2, empty [except for lost+found], mounted on /
384      * /dev/sdb and /dev/sdc may have random content.
385      *)
386   | InitBasicFSonLVM
387
388     (* /dev/sdd (the ISO, see images/ directory in source)
389      * is mounted on /
390      *)
391   | InitISOFS
392
393 (* Sequence of commands for testing. *)
394 and seq = cmd list
395 and cmd = string list
396
397 (* Type of an action as declared in Generator_actions module. *)
398 type action = string * style * int * flags list * tests * string * string
399
400 (* Field types for structures. *)
401 type field =
402   | FChar                       (* C 'char' (really, a 7 bit byte). *)
403   | FString                     (* nul-terminated ASCII string, NOT NULL. *)
404   | FBuffer                     (* opaque buffer of bytes, (char *, int) pair *)
405   | FUInt32
406   | FInt32
407   | FUInt64
408   | FInt64
409   | FBytes                      (* Any int measure that counts bytes. *)
410   | FUUID                       (* 32 bytes long, NOT nul-terminated. *)
411   | FOptPercent                 (* [0..100], or -1 meaning "not present". *)
412
413 (* Used for testing language bindings. *)
414 type callt =
415   | CallString of string
416   | CallOptString of string option
417   | CallStringList of string list
418   | CallInt of int
419   | CallInt64 of int64
420   | CallBool of bool
421   | CallBuffer of string