Add Sys::Guestfs::Lib - useful functions for using libguestfs from Perl.
[libguestfs.git] / src / generator.ml
1 #!/usr/bin/env ocaml
2 (* libguestfs
3  * Copyright (C) 2009 Red Hat Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *)
19
20 (* This script generates a large amount of code and documentation for
21  * all the daemon actions.
22  *
23  * To add a new action there are only two files you need to change,
24  * this one to describe the interface (see the big table below), and
25  * daemon/<somefile>.c to write the implementation.
26  *
27  * After editing this file, run it (./src/generator.ml) to regenerate
28  * all the output files.
29  *
30  * IMPORTANT: This script should NOT print any warnings.  If it prints
31  * warnings, you should treat them as errors.
32  * [Need to add -warn-error to ocaml command line]
33  *)
34
35 #load "unix.cma";;
36 #load "str.cma";;
37
38 open Printf
39
40 type style = ret * args
41 and ret =
42     (* "RErr" as a return value means an int used as a simple error
43      * indication, ie. 0 or -1.
44      *)
45   | RErr
46     (* "RInt" as a return value means an int which is -1 for error
47      * or any value >= 0 on success.  Only use this for smallish
48      * positive ints (0 <= i < 2^30).
49      *)
50   | RInt of string
51     (* "RInt64" is the same as RInt, but is guaranteed to be able
52      * to return a full 64 bit value, _except_ that -1 means error
53      * (so -1 cannot be a valid, non-error return value).
54      *)
55   | RInt64 of string
56     (* "RBool" is a bool return value which can be true/false or
57      * -1 for error.
58      *)
59   | RBool of string
60     (* "RConstString" is a string that refers to a constant value.
61      * Try to avoid using this.  In particular you cannot use this
62      * for values returned from the daemon, because there is no
63      * thread-safe way to return them in the C API.
64      *)
65   | RConstString of string
66     (* "RString" and "RStringList" are caller-frees. *)
67   | RString of string
68   | RStringList of string
69     (* "RStruct" is a function which returns a single named structure
70      * or an error indication (in C, a struct, and in other languages
71      * with varying representations, but usually very efficient).  See
72      * after the function list below for the structures. 
73      *)
74   | RStruct of string * string          (* name of retval, name of struct *)
75     (* "RStructList" is a function which returns either a list/array
76      * of structures (could be zero-length), or an error indication.
77      *)
78   | RStructList of string * string      (* name of retval, name of struct *)
79     (* Key-value pairs of untyped strings.  Turns into a hashtable or
80      * dictionary in languages which support it.  DON'T use this as a
81      * general "bucket" for results.  Prefer a stronger typed return
82      * value if one is available, or write a custom struct.  Don't use
83      * this if the list could potentially be very long, since it is
84      * inefficient.  Keys should be unique.  NULLs are not permitted.
85      *)
86   | RHashtable of string
87
88 and args = argt list    (* Function parameters, guestfs handle is implicit. *)
89
90     (* Note in future we should allow a "variable args" parameter as
91      * the final parameter, to allow commands like
92      *   chmod mode file [file(s)...]
93      * This is not implemented yet, but many commands (such as chmod)
94      * are currently defined with the argument order keeping this future
95      * possibility in mind.
96      *)
97 and argt =
98   | String of string    (* const char *name, cannot be NULL *)
99   | OptString of string (* const char *name, may be NULL *)
100   | StringList of string(* list of strings (each string cannot be NULL) *)
101   | Bool of string      (* boolean *)
102   | Int of string       (* int (smallish ints, signed, <= 31 bits) *)
103     (* These are treated as filenames (simple string parameters) in
104      * the C API and bindings.  But in the RPC protocol, we transfer
105      * the actual file content up to or down from the daemon.
106      * FileIn: local machine -> daemon (in request)
107      * FileOut: daemon -> local machine (in reply)
108      * In guestfish (only), the special name "-" means read from
109      * stdin or write to stdout.
110      *)
111   | FileIn of string
112   | FileOut of string
113
114 type flags =
115   | ProtocolLimitWarning  (* display warning about protocol size limits *)
116   | DangerWillRobinson    (* flags particularly dangerous commands *)
117   | FishAlias of string   (* provide an alias for this cmd in guestfish *)
118   | FishAction of string  (* call this function in guestfish *)
119   | NotInFish             (* do not export via guestfish *)
120   | NotInDocs             (* do not add this function to documentation *)
121
122 let protocol_limit_warning =
123   "Because of the message protocol, there is a transfer limit
124 of somewhere between 2MB and 4MB.  To transfer large files you should use
125 FTP."
126
127 let danger_will_robinson =
128   "B<This command is dangerous.  Without careful use you
129 can easily destroy all your data>."
130
131 (* You can supply zero or as many tests as you want per API call.
132  *
133  * Note that the test environment has 3 block devices, of size 500MB,
134  * 50MB and 10MB (respectively /dev/sda, /dev/sdb, /dev/sdc), and
135  * a fourth squashfs block device with some known files on it (/dev/sdd).
136  *
137  * Note for partitioning purposes, the 500MB device has 1015 cylinders.
138  * Number of cylinders was 63 for IDE emulated disks with precisely
139  * the same size.  How exactly this is calculated is a mystery.
140  *
141  * The squashfs block device (/dev/sdd) comes from images/test.sqsh.
142  *
143  * To be able to run the tests in a reasonable amount of time,
144  * the virtual machine and block devices are reused between tests.
145  * So don't try testing kill_subprocess :-x
146  *
147  * Between each test we blockdev-setrw, umount-all, lvm-remove-all.
148  *
149  * Don't assume anything about the previous contents of the block
150  * devices.  Use 'Init*' to create some initial scenarios.
151  *
152  * You can add a prerequisite clause to any individual test.  This
153  * is a run-time check, which, if it fails, causes the test to be
154  * skipped.  Useful if testing a command which might not work on
155  * all variations of libguestfs builds.  A test that has prerequisite
156  * of 'Always' is run unconditionally.
157  *
158  * In addition, packagers can skip individual tests by setting the
159  * environment variables:     eg:
160  *   SKIP_TEST_<CMD>_<NUM>=1  SKIP_TEST_COMMAND_3=1  (skips test #3 of command)
161  *   SKIP_TEST_<CMD>=1        SKIP_TEST_ZEROFREE=1   (skips all zerofree tests)
162  *)
163 type tests = (test_init * test_prereq * test) list
164 and test =
165     (* Run the command sequence and just expect nothing to fail. *)
166   | TestRun of seq
167     (* Run the command sequence and expect the output of the final
168      * command to be the string.
169      *)
170   | TestOutput of seq * string
171     (* Run the command sequence and expect the output of the final
172      * command to be the list of strings.
173      *)
174   | TestOutputList of seq * string list
175     (* Run the command sequence and expect the output of the final
176      * command to be the list of block devices (could be either
177      * "/dev/sd.." or "/dev/hd.." form - we don't check the 5th
178      * character of each string).
179      *)
180   | TestOutputListOfDevices of seq * string list
181     (* Run the command sequence and expect the output of the final
182      * command to be the integer.
183      *)
184   | TestOutputInt of seq * int
185     (* Run the command sequence and expect the output of the final
186      * command to be a true value (!= 0 or != NULL).
187      *)
188   | TestOutputTrue of seq
189     (* Run the command sequence and expect the output of the final
190      * command to be a false value (== 0 or == NULL, but not an error).
191      *)
192   | TestOutputFalse of seq
193     (* Run the command sequence and expect the output of the final
194      * command to be a list of the given length (but don't care about
195      * content).
196      *)
197   | TestOutputLength of seq * int
198     (* Run the command sequence and expect the output of the final
199      * command to be a structure.
200      *)
201   | TestOutputStruct of seq * test_field_compare list
202     (* Run the command sequence and expect the final command (only)
203      * to fail.
204      *)
205   | TestLastFail of seq
206
207 and test_field_compare =
208   | CompareWithInt of string * int
209   | CompareWithString of string * string
210   | CompareFieldsIntEq of string * string
211   | CompareFieldsStrEq of string * string
212
213 (* Test prerequisites. *)
214 and test_prereq =
215     (* Test always runs. *)
216   | Always
217     (* Test is currently disabled - eg. it fails, or it tests some
218      * unimplemented feature.
219      *)
220   | Disabled
221     (* 'string' is some C code (a function body) that should return
222      * true or false.  The test will run if the code returns true.
223      *)
224   | If of string
225     (* As for 'If' but the test runs _unless_ the code returns true. *)
226   | Unless of string
227
228 (* Some initial scenarios for testing. *)
229 and test_init =
230     (* Do nothing, block devices could contain random stuff including
231      * LVM PVs, and some filesystems might be mounted.  This is usually
232      * a bad idea.
233      *)
234   | InitNone
235     (* Block devices are empty and no filesystems are mounted. *)
236   | InitEmpty
237     (* /dev/sda contains a single partition /dev/sda1, which is formatted
238      * as ext2, empty [except for lost+found] and mounted on /.
239      * /dev/sdb and /dev/sdc may have random content.
240      * No LVM.
241      *)
242   | InitBasicFS
243     (* /dev/sda:
244      *   /dev/sda1 (is a PV):
245      *     /dev/VG/LV (size 8MB):
246      *       formatted as ext2, empty [except for lost+found], mounted on /
247      * /dev/sdb and /dev/sdc may have random content.
248      *)
249   | InitBasicFSonLVM
250
251 (* Sequence of commands for testing. *)
252 and seq = cmd list
253 and cmd = string list
254
255 (* Note about long descriptions: When referring to another
256  * action, use the format C<guestfs_other> (ie. the full name of
257  * the C function).  This will be replaced as appropriate in other
258  * language bindings.
259  *
260  * Apart from that, long descriptions are just perldoc paragraphs.
261  *)
262
263 (* These test functions are used in the language binding tests. *)
264
265 let test_all_args = [
266   String "str";
267   OptString "optstr";
268   StringList "strlist";
269   Bool "b";
270   Int "integer";
271   FileIn "filein";
272   FileOut "fileout";
273 ]
274
275 let test_all_rets = [
276   (* except for RErr, which is tested thoroughly elsewhere *)
277   "test0rint",         RInt "valout";
278   "test0rint64",       RInt64 "valout";
279   "test0rbool",        RBool "valout";
280   "test0rconststring", RConstString "valout";
281   "test0rstring",      RString "valout";
282   "test0rstringlist",  RStringList "valout";
283   "test0rstruct",      RStruct ("valout", "lvm_pv");
284   "test0rstructlist",  RStructList ("valout", "lvm_pv");
285   "test0rhashtable",   RHashtable "valout";
286 ]
287
288 let test_functions = [
289   ("test0", (RErr, test_all_args), -1, [NotInFish; NotInDocs],
290    [],
291    "internal test function - do not use",
292    "\
293 This is an internal test function which is used to test whether
294 the automatically generated bindings can handle every possible
295 parameter type correctly.
296
297 It echos the contents of each parameter to stdout.
298
299 You probably don't want to call this function.");
300 ] @ List.flatten (
301   List.map (
302     fun (name, ret) ->
303       [(name, (ret, [String "val"]), -1, [NotInFish; NotInDocs],
304         [],
305         "internal test function - do not use",
306         "\
307 This is an internal test function which is used to test whether
308 the automatically generated bindings can handle every possible
309 return type correctly.
310
311 It converts string C<val> to the return type.
312
313 You probably don't want to call this function.");
314        (name ^ "err", (ret, []), -1, [NotInFish; NotInDocs],
315         [],
316         "internal test function - do not use",
317         "\
318 This is an internal test function which is used to test whether
319 the automatically generated bindings can handle every possible
320 return type correctly.
321
322 This function always returns an error.
323
324 You probably don't want to call this function.")]
325   ) test_all_rets
326 )
327
328 (* non_daemon_functions are any functions which don't get processed
329  * in the daemon, eg. functions for setting and getting local
330  * configuration values.
331  *)
332
333 let non_daemon_functions = test_functions @ [
334   ("launch", (RErr, []), -1, [FishAlias "run"; FishAction "launch"],
335    [],
336    "launch the qemu subprocess",
337    "\
338 Internally libguestfs is implemented by running a virtual machine
339 using L<qemu(1)>.
340
341 You should call this after configuring the handle
342 (eg. adding drives) but before performing any actions.");
343
344   ("wait_ready", (RErr, []), -1, [NotInFish],
345    [],
346    "wait until the qemu subprocess launches",
347    "\
348 Internally libguestfs is implemented by running a virtual machine
349 using L<qemu(1)>.
350
351 You should call this after C<guestfs_launch> to wait for the launch
352 to complete.");
353
354   ("kill_subprocess", (RErr, []), -1, [],
355    [],
356    "kill the qemu subprocess",
357    "\
358 This kills the qemu subprocess.  You should never need to call this.");
359
360   ("add_drive", (RErr, [String "filename"]), -1, [FishAlias "add"],
361    [],
362    "add an image to examine or modify",
363    "\
364 This function adds a virtual machine disk image C<filename> to the
365 guest.  The first time you call this function, the disk appears as IDE
366 disk 0 (C</dev/sda>) in the guest, the second time as C</dev/sdb>, and
367 so on.
368
369 You don't necessarily need to be root when using libguestfs.  However
370 you obviously do need sufficient permissions to access the filename
371 for whatever operations you want to perform (ie. read access if you
372 just want to read the image or write access if you want to modify the
373 image).
374
375 This is equivalent to the qemu parameter
376 C<-drive file=filename,cache=off,if=...>.
377
378 Note that this call checks for the existence of C<filename>.  This
379 stops you from specifying other types of drive which are supported
380 by qemu such as C<nbd:> and C<http:> URLs.  To specify those, use
381 the general C<guestfs_config> call instead.");
382
383   ("add_cdrom", (RErr, [String "filename"]), -1, [FishAlias "cdrom"],
384    [],
385    "add a CD-ROM disk image to examine",
386    "\
387 This function adds a virtual CD-ROM disk image to the guest.
388
389 This is equivalent to the qemu parameter C<-cdrom filename>.
390
391 Note that this call checks for the existence of C<filename>.  This
392 stops you from specifying other types of drive which are supported
393 by qemu such as C<nbd:> and C<http:> URLs.  To specify those, use
394 the general C<guestfs_config> call instead.");
395
396   ("add_drive_ro", (RErr, [String "filename"]), -1, [FishAlias "add-ro"],
397    [],
398    "add a drive in snapshot mode (read-only)",
399    "\
400 This adds a drive in snapshot mode, making it effectively
401 read-only.
402
403 Note that writes to the device are allowed, and will be seen for
404 the duration of the guestfs handle, but they are written
405 to a temporary file which is discarded as soon as the guestfs
406 handle is closed.  We don't currently have any method to enable
407 changes to be committed, although qemu can support this.
408
409 This is equivalent to the qemu parameter
410 C<-drive file=filename,snapshot=on,if=...>.
411
412 Note that this call checks for the existence of C<filename>.  This
413 stops you from specifying other types of drive which are supported
414 by qemu such as C<nbd:> and C<http:> URLs.  To specify those, use
415 the general C<guestfs_config> call instead.");
416
417   ("config", (RErr, [String "qemuparam"; OptString "qemuvalue"]), -1, [],
418    [],
419    "add qemu parameters",
420    "\
421 This can be used to add arbitrary qemu command line parameters
422 of the form C<-param value>.  Actually it's not quite arbitrary - we
423 prevent you from setting some parameters which would interfere with
424 parameters that we use.
425
426 The first character of C<param> string must be a C<-> (dash).
427
428 C<value> can be NULL.");
429
430   ("set_qemu", (RErr, [String "qemu"]), -1, [FishAlias "qemu"],
431    [],
432    "set the qemu binary",
433    "\
434 Set the qemu binary that we will use.
435
436 The default is chosen when the library was compiled by the
437 configure script.
438
439 You can also override this by setting the C<LIBGUESTFS_QEMU>
440 environment variable.
441
442 Setting C<qemu> to C<NULL> restores the default qemu binary.");
443
444   ("get_qemu", (RConstString "qemu", []), -1, [],
445    [],
446    "get the qemu binary",
447    "\
448 Return the current qemu binary.
449
450 This is always non-NULL.  If it wasn't set already, then this will
451 return the default qemu binary name.");
452
453   ("set_path", (RErr, [String "path"]), -1, [FishAlias "path"],
454    [],
455    "set the search path",
456    "\
457 Set the path that libguestfs searches for kernel and initrd.img.
458
459 The default is C<$libdir/guestfs> unless overridden by setting
460 C<LIBGUESTFS_PATH> environment variable.
461
462 Setting C<path> to C<NULL> restores the default path.");
463
464   ("get_path", (RConstString "path", []), -1, [],
465    [],
466    "get the search path",
467    "\
468 Return the current search path.
469
470 This is always non-NULL.  If it wasn't set already, then this will
471 return the default path.");
472
473   ("set_append", (RErr, [String "append"]), -1, [FishAlias "append"],
474    [],
475    "add options to kernel command line",
476    "\
477 This function is used to add additional options to the
478 guest kernel command line.
479
480 The default is C<NULL> unless overridden by setting
481 C<LIBGUESTFS_APPEND> environment variable.
482
483 Setting C<append> to C<NULL> means I<no> additional options
484 are passed (libguestfs always adds a few of its own).");
485
486   ("get_append", (RConstString "append", []), -1, [],
487    [],
488    "get the additional kernel options",
489    "\
490 Return the additional kernel options which are added to the
491 guest kernel command line.
492
493 If C<NULL> then no options are added.");
494
495   ("set_autosync", (RErr, [Bool "autosync"]), -1, [FishAlias "autosync"],
496    [],
497    "set autosync mode",
498    "\
499 If C<autosync> is true, this enables autosync.  Libguestfs will make a
500 best effort attempt to run C<guestfs_umount_all> followed by
501 C<guestfs_sync> when the handle is closed
502 (also if the program exits without closing handles).
503
504 This is disabled by default (except in guestfish where it is
505 enabled by default).");
506
507   ("get_autosync", (RBool "autosync", []), -1, [],
508    [],
509    "get autosync mode",
510    "\
511 Get the autosync flag.");
512
513   ("set_verbose", (RErr, [Bool "verbose"]), -1, [FishAlias "verbose"],
514    [],
515    "set verbose mode",
516    "\
517 If C<verbose> is true, this turns on verbose messages (to C<stderr>).
518
519 Verbose messages are disabled unless the environment variable
520 C<LIBGUESTFS_DEBUG> is defined and set to C<1>.");
521
522   ("get_verbose", (RBool "verbose", []), -1, [],
523    [],
524    "get verbose mode",
525    "\
526 This returns the verbose messages flag.");
527
528   ("is_ready", (RBool "ready", []), -1, [],
529    [],
530    "is ready to accept commands",
531    "\
532 This returns true iff this handle is ready to accept commands
533 (in the C<READY> state).
534
535 For more information on states, see L<guestfs(3)>.");
536
537   ("is_config", (RBool "config", []), -1, [],
538    [],
539    "is in configuration state",
540    "\
541 This returns true iff this handle is being configured
542 (in the C<CONFIG> state).
543
544 For more information on states, see L<guestfs(3)>.");
545
546   ("is_launching", (RBool "launching", []), -1, [],
547    [],
548    "is launching subprocess",
549    "\
550 This returns true iff this handle is launching the subprocess
551 (in the C<LAUNCHING> state).
552
553 For more information on states, see L<guestfs(3)>.");
554
555   ("is_busy", (RBool "busy", []), -1, [],
556    [],
557    "is busy processing a command",
558    "\
559 This returns true iff this handle is busy processing a command
560 (in the C<BUSY> state).
561
562 For more information on states, see L<guestfs(3)>.");
563
564   ("get_state", (RInt "state", []), -1, [],
565    [],
566    "get the current state",
567    "\
568 This returns the current state as an opaque integer.  This is
569 only useful for printing debug and internal error messages.
570
571 For more information on states, see L<guestfs(3)>.");
572
573   ("set_busy", (RErr, []), -1, [NotInFish],
574    [],
575    "set state to busy",
576    "\
577 This sets the state to C<BUSY>.  This is only used when implementing
578 actions using the low-level API.
579
580 For more information on states, see L<guestfs(3)>.");
581
582   ("set_ready", (RErr, []), -1, [NotInFish],
583    [],
584    "set state to ready",
585    "\
586 This sets the state to C<READY>.  This is only used when implementing
587 actions using the low-level API.
588
589 For more information on states, see L<guestfs(3)>.");
590
591   ("end_busy", (RErr, []), -1, [NotInFish],
592    [],
593    "leave the busy state",
594    "\
595 This sets the state to C<READY>, or if in C<CONFIG> then it leaves the
596 state as is.  This is only used when implementing
597 actions using the low-level API.
598
599 For more information on states, see L<guestfs(3)>.");
600
601   ("set_memsize", (RErr, [Int "memsize"]), -1, [FishAlias "memsize"],
602    [],
603    "set memory allocated to the qemu subprocess",
604    "\
605 This sets the memory size in megabytes allocated to the
606 qemu subprocess.  This only has any effect if called before
607 C<guestfs_launch>.
608
609 You can also change this by setting the environment
610 variable C<LIBGUESTFS_MEMSIZE> before the handle is
611 created.
612
613 For more information on the architecture of libguestfs,
614 see L<guestfs(3)>.");
615
616   ("get_memsize", (RInt "memsize", []), -1, [],
617    [],
618    "get memory allocated to the qemu subprocess",
619    "\
620 This gets the memory size in megabytes allocated to the
621 qemu subprocess.
622
623 If C<guestfs_set_memsize> was not called
624 on this handle, and if C<LIBGUESTFS_MEMSIZE> was not set,
625 then this returns the compiled-in default value for memsize.
626
627 For more information on the architecture of libguestfs,
628 see L<guestfs(3)>.");
629
630   ("get_pid", (RInt "pid", []), -1, [FishAlias "pid"],
631    [],
632    "get PID of qemu subprocess",
633    "\
634 Return the process ID of the qemu subprocess.  If there is no
635 qemu subprocess, then this will return an error.
636
637 This is an internal call used for debugging and testing.");
638
639 ]
640
641 (* daemon_functions are any functions which cause some action
642  * to take place in the daemon.
643  *)
644
645 let daemon_functions = [
646   ("mount", (RErr, [String "device"; String "mountpoint"]), 1, [],
647    [InitEmpty, Always, TestOutput (
648       [["sfdiskM"; "/dev/sda"; ","];
649        ["mkfs"; "ext2"; "/dev/sda1"];
650        ["mount"; "/dev/sda1"; "/"];
651        ["write_file"; "/new"; "new file contents"; "0"];
652        ["cat"; "/new"]], "new file contents")],
653    "mount a guest disk at a position in the filesystem",
654    "\
655 Mount a guest disk at a position in the filesystem.  Block devices
656 are named C</dev/sda>, C</dev/sdb> and so on, as they were added to
657 the guest.  If those block devices contain partitions, they will have
658 the usual names (eg. C</dev/sda1>).  Also LVM C</dev/VG/LV>-style
659 names can be used.
660
661 The rules are the same as for L<mount(2)>:  A filesystem must
662 first be mounted on C</> before others can be mounted.  Other
663 filesystems can only be mounted on directories which already
664 exist.
665
666 The mounted filesystem is writable, if we have sufficient permissions
667 on the underlying device.
668
669 The filesystem options C<sync> and C<noatime> are set with this
670 call, in order to improve reliability.");
671
672   ("sync", (RErr, []), 2, [],
673    [ InitEmpty, Always, TestRun [["sync"]]],
674    "sync disks, writes are flushed through to the disk image",
675    "\
676 This syncs the disk, so that any writes are flushed through to the
677 underlying disk image.
678
679 You should always call this if you have modified a disk image, before
680 closing the handle.");
681
682   ("touch", (RErr, [String "path"]), 3, [],
683    [InitBasicFS, Always, TestOutputTrue (
684       [["touch"; "/new"];
685        ["exists"; "/new"]])],
686    "update file timestamps or create a new file",
687    "\
688 Touch acts like the L<touch(1)> command.  It can be used to
689 update the timestamps on a file, or, if the file does not exist,
690 to create a new zero-length file.");
691
692   ("cat", (RString "content", [String "path"]), 4, [ProtocolLimitWarning],
693    [InitBasicFS, Always, TestOutput (
694       [["write_file"; "/new"; "new file contents"; "0"];
695        ["cat"; "/new"]], "new file contents")],
696    "list the contents of a file",
697    "\
698 Return the contents of the file named C<path>.
699
700 Note that this function cannot correctly handle binary files
701 (specifically, files containing C<\\0> character which is treated
702 as end of string).  For those you need to use the C<guestfs_download>
703 function which has a more complex interface.");
704
705   ("ll", (RString "listing", [String "directory"]), 5, [],
706    [], (* XXX Tricky to test because it depends on the exact format
707         * of the 'ls -l' command, which changes between F10 and F11.
708         *)
709    "list the files in a directory (long format)",
710    "\
711 List the files in C<directory> (relative to the root directory,
712 there is no cwd) in the format of 'ls -la'.
713
714 This command is mostly useful for interactive sessions.  It
715 is I<not> intended that you try to parse the output string.");
716
717   ("ls", (RStringList "listing", [String "directory"]), 6, [],
718    [InitBasicFS, Always, TestOutputList (
719       [["touch"; "/new"];
720        ["touch"; "/newer"];
721        ["touch"; "/newest"];
722        ["ls"; "/"]], ["lost+found"; "new"; "newer"; "newest"])],
723    "list the files in a directory",
724    "\
725 List the files in C<directory> (relative to the root directory,
726 there is no cwd).  The '.' and '..' entries are not returned, but
727 hidden files are shown.
728
729 This command is mostly useful for interactive sessions.  Programs
730 should probably use C<guestfs_readdir> instead.");
731
732   ("list_devices", (RStringList "devices", []), 7, [],
733    [InitEmpty, Always, TestOutputListOfDevices (
734       [["list_devices"]], ["/dev/sda"; "/dev/sdb"; "/dev/sdc"; "/dev/sdd"])],
735    "list the block devices",
736    "\
737 List all the block devices.
738
739 The full block device names are returned, eg. C</dev/sda>");
740
741   ("list_partitions", (RStringList "partitions", []), 8, [],
742    [InitBasicFS, Always, TestOutputListOfDevices (
743       [["list_partitions"]], ["/dev/sda1"]);
744     InitEmpty, Always, TestOutputListOfDevices (
745       [["sfdiskM"; "/dev/sda"; ",100 ,200 ,"];
746        ["list_partitions"]], ["/dev/sda1"; "/dev/sda2"; "/dev/sda3"])],
747    "list the partitions",
748    "\
749 List all the partitions detected on all block devices.
750
751 The full partition device names are returned, eg. C</dev/sda1>
752
753 This does not return logical volumes.  For that you will need to
754 call C<guestfs_lvs>.");
755
756   ("pvs", (RStringList "physvols", []), 9, [],
757    [InitBasicFSonLVM, Always, TestOutputListOfDevices (
758       [["pvs"]], ["/dev/sda1"]);
759     InitEmpty, Always, TestOutputListOfDevices (
760       [["sfdiskM"; "/dev/sda"; ",100 ,200 ,"];
761        ["pvcreate"; "/dev/sda1"];
762        ["pvcreate"; "/dev/sda2"];
763        ["pvcreate"; "/dev/sda3"];
764        ["pvs"]], ["/dev/sda1"; "/dev/sda2"; "/dev/sda3"])],
765    "list the LVM physical volumes (PVs)",
766    "\
767 List all the physical volumes detected.  This is the equivalent
768 of the L<pvs(8)> command.
769
770 This returns a list of just the device names that contain
771 PVs (eg. C</dev/sda2>).
772
773 See also C<guestfs_pvs_full>.");
774
775   ("vgs", (RStringList "volgroups", []), 10, [],
776    [InitBasicFSonLVM, Always, TestOutputList (
777       [["vgs"]], ["VG"]);
778     InitEmpty, Always, TestOutputList (
779       [["sfdiskM"; "/dev/sda"; ",100 ,200 ,"];
780        ["pvcreate"; "/dev/sda1"];
781        ["pvcreate"; "/dev/sda2"];
782        ["pvcreate"; "/dev/sda3"];
783        ["vgcreate"; "VG1"; "/dev/sda1 /dev/sda2"];
784        ["vgcreate"; "VG2"; "/dev/sda3"];
785        ["vgs"]], ["VG1"; "VG2"])],
786    "list the LVM volume groups (VGs)",
787    "\
788 List all the volumes groups detected.  This is the equivalent
789 of the L<vgs(8)> command.
790
791 This returns a list of just the volume group names that were
792 detected (eg. C<VolGroup00>).
793
794 See also C<guestfs_vgs_full>.");
795
796   ("lvs", (RStringList "logvols", []), 11, [],
797    [InitBasicFSonLVM, Always, TestOutputList (
798       [["lvs"]], ["/dev/VG/LV"]);
799     InitEmpty, Always, TestOutputList (
800       [["sfdiskM"; "/dev/sda"; ",100 ,200 ,"];
801        ["pvcreate"; "/dev/sda1"];
802        ["pvcreate"; "/dev/sda2"];
803        ["pvcreate"; "/dev/sda3"];
804        ["vgcreate"; "VG1"; "/dev/sda1 /dev/sda2"];
805        ["vgcreate"; "VG2"; "/dev/sda3"];
806        ["lvcreate"; "LV1"; "VG1"; "50"];
807        ["lvcreate"; "LV2"; "VG1"; "50"];
808        ["lvcreate"; "LV3"; "VG2"; "50"];
809        ["lvs"]], ["/dev/VG1/LV1"; "/dev/VG1/LV2"; "/dev/VG2/LV3"])],
810    "list the LVM logical volumes (LVs)",
811    "\
812 List all the logical volumes detected.  This is the equivalent
813 of the L<lvs(8)> command.
814
815 This returns a list of the logical volume device names
816 (eg. C</dev/VolGroup00/LogVol00>).
817
818 See also C<guestfs_lvs_full>.");
819
820   ("pvs_full", (RStructList ("physvols", "lvm_pv"), []), 12, [],
821    [], (* XXX how to test? *)
822    "list the LVM physical volumes (PVs)",
823    "\
824 List all the physical volumes detected.  This is the equivalent
825 of the L<pvs(8)> command.  The \"full\" version includes all fields.");
826
827   ("vgs_full", (RStructList ("volgroups", "lvm_vg"), []), 13, [],
828    [], (* XXX how to test? *)
829    "list the LVM volume groups (VGs)",
830    "\
831 List all the volumes groups detected.  This is the equivalent
832 of the L<vgs(8)> command.  The \"full\" version includes all fields.");
833
834   ("lvs_full", (RStructList ("logvols", "lvm_lv"), []), 14, [],
835    [], (* XXX how to test? *)
836    "list the LVM logical volumes (LVs)",
837    "\
838 List all the logical volumes detected.  This is the equivalent
839 of the L<lvs(8)> command.  The \"full\" version includes all fields.");
840
841   ("read_lines", (RStringList "lines", [String "path"]), 15, [],
842    [InitBasicFS, Always, TestOutputList (
843       [["write_file"; "/new"; "line1\r\nline2\nline3"; "0"];
844        ["read_lines"; "/new"]], ["line1"; "line2"; "line3"]);
845     InitBasicFS, Always, TestOutputList (
846       [["write_file"; "/new"; ""; "0"];
847        ["read_lines"; "/new"]], [])],
848    "read file as lines",
849    "\
850 Return the contents of the file named C<path>.
851
852 The file contents are returned as a list of lines.  Trailing
853 C<LF> and C<CRLF> character sequences are I<not> returned.
854
855 Note that this function cannot correctly handle binary files
856 (specifically, files containing C<\\0> character which is treated
857 as end of line).  For those you need to use the C<guestfs_read_file>
858 function which has a more complex interface.");
859
860   ("aug_init", (RErr, [String "root"; Int "flags"]), 16, [],
861    [], (* XXX Augeas code needs tests. *)
862    "create a new Augeas handle",
863    "\
864 Create a new Augeas handle for editing configuration files.
865 If there was any previous Augeas handle associated with this
866 guestfs session, then it is closed.
867
868 You must call this before using any other C<guestfs_aug_*>
869 commands.
870
871 C<root> is the filesystem root.  C<root> must not be NULL,
872 use C</> instead.
873
874 The flags are the same as the flags defined in
875 E<lt>augeas.hE<gt>, the logical I<or> of the following
876 integers:
877
878 =over 4
879
880 =item C<AUG_SAVE_BACKUP> = 1
881
882 Keep the original file with a C<.augsave> extension.
883
884 =item C<AUG_SAVE_NEWFILE> = 2
885
886 Save changes into a file with extension C<.augnew>, and
887 do not overwrite original.  Overrides C<AUG_SAVE_BACKUP>.
888
889 =item C<AUG_TYPE_CHECK> = 4
890
891 Typecheck lenses (can be expensive).
892
893 =item C<AUG_NO_STDINC> = 8
894
895 Do not use standard load path for modules.
896
897 =item C<AUG_SAVE_NOOP> = 16
898
899 Make save a no-op, just record what would have been changed.
900
901 =item C<AUG_NO_LOAD> = 32
902
903 Do not load the tree in C<guestfs_aug_init>.
904
905 =back
906
907 To close the handle, you can call C<guestfs_aug_close>.
908
909 To find out more about Augeas, see L<http://augeas.net/>.");
910
911   ("aug_close", (RErr, []), 26, [],
912    [], (* XXX Augeas code needs tests. *)
913    "close the current Augeas handle",
914    "\
915 Close the current Augeas handle and free up any resources
916 used by it.  After calling this, you have to call
917 C<guestfs_aug_init> again before you can use any other
918 Augeas functions.");
919
920   ("aug_defvar", (RInt "nrnodes", [String "name"; OptString "expr"]), 17, [],
921    [], (* XXX Augeas code needs tests. *)
922    "define an Augeas variable",
923    "\
924 Defines an Augeas variable C<name> whose value is the result
925 of evaluating C<expr>.  If C<expr> is NULL, then C<name> is
926 undefined.
927
928 On success this returns the number of nodes in C<expr>, or
929 C<0> if C<expr> evaluates to something which is not a nodeset.");
930
931   ("aug_defnode", (RStruct ("nrnodescreated", "int_bool"), [String "name"; String "expr"; String "val"]), 18, [],
932    [], (* XXX Augeas code needs tests. *)
933    "define an Augeas node",
934    "\
935 Defines a variable C<name> whose value is the result of
936 evaluating C<expr>.
937
938 If C<expr> evaluates to an empty nodeset, a node is created,
939 equivalent to calling C<guestfs_aug_set> C<expr>, C<value>.
940 C<name> will be the nodeset containing that single node.
941
942 On success this returns a pair containing the
943 number of nodes in the nodeset, and a boolean flag
944 if a node was created.");
945
946   ("aug_get", (RString "val", [String "path"]), 19, [],
947    [], (* XXX Augeas code needs tests. *)
948    "look up the value of an Augeas path",
949    "\
950 Look up the value associated with C<path>.  If C<path>
951 matches exactly one node, the C<value> is returned.");
952
953   ("aug_set", (RErr, [String "path"; String "val"]), 20, [],
954    [], (* XXX Augeas code needs tests. *)
955    "set Augeas path to value",
956    "\
957 Set the value associated with C<path> to C<value>.");
958
959   ("aug_insert", (RErr, [String "path"; String "label"; Bool "before"]), 21, [],
960    [], (* XXX Augeas code needs tests. *)
961    "insert a sibling Augeas node",
962    "\
963 Create a new sibling C<label> for C<path>, inserting it into
964 the tree before or after C<path> (depending on the boolean
965 flag C<before>).
966
967 C<path> must match exactly one existing node in the tree, and
968 C<label> must be a label, ie. not contain C</>, C<*> or end
969 with a bracketed index C<[N]>.");
970
971   ("aug_rm", (RInt "nrnodes", [String "path"]), 22, [],
972    [], (* XXX Augeas code needs tests. *)
973    "remove an Augeas path",
974    "\
975 Remove C<path> and all of its children.
976
977 On success this returns the number of entries which were removed.");
978
979   ("aug_mv", (RErr, [String "src"; String "dest"]), 23, [],
980    [], (* XXX Augeas code needs tests. *)
981    "move Augeas node",
982    "\
983 Move the node C<src> to C<dest>.  C<src> must match exactly
984 one node.  C<dest> is overwritten if it exists.");
985
986   ("aug_match", (RStringList "matches", [String "path"]), 24, [],
987    [], (* XXX Augeas code needs tests. *)
988    "return Augeas nodes which match path",
989    "\
990 Returns a list of paths which match the path expression C<path>.
991 The returned paths are sufficiently qualified so that they match
992 exactly one node in the current tree.");
993
994   ("aug_save", (RErr, []), 25, [],
995    [], (* XXX Augeas code needs tests. *)
996    "write all pending Augeas changes to disk",
997    "\
998 This writes all pending changes to disk.
999
1000 The flags which were passed to C<guestfs_aug_init> affect exactly
1001 how files are saved.");
1002
1003   ("aug_load", (RErr, []), 27, [],
1004    [], (* XXX Augeas code needs tests. *)
1005    "load files into the tree",
1006    "\
1007 Load files into the tree.
1008
1009 See C<aug_load> in the Augeas documentation for the full gory
1010 details.");
1011
1012   ("aug_ls", (RStringList "matches", [String "path"]), 28, [],
1013    [], (* XXX Augeas code needs tests. *)
1014    "list Augeas nodes under a path",
1015    "\
1016 This is just a shortcut for listing C<guestfs_aug_match>
1017 C<path/*> and sorting the resulting nodes into alphabetical order.");
1018
1019   ("rm", (RErr, [String "path"]), 29, [],
1020    [InitBasicFS, Always, TestRun
1021       [["touch"; "/new"];
1022        ["rm"; "/new"]];
1023     InitBasicFS, Always, TestLastFail
1024       [["rm"; "/new"]];
1025     InitBasicFS, Always, TestLastFail
1026       [["mkdir"; "/new"];
1027        ["rm"; "/new"]]],
1028    "remove a file",
1029    "\
1030 Remove the single file C<path>.");
1031
1032   ("rmdir", (RErr, [String "path"]), 30, [],
1033    [InitBasicFS, Always, TestRun
1034       [["mkdir"; "/new"];
1035        ["rmdir"; "/new"]];
1036     InitBasicFS, Always, TestLastFail
1037       [["rmdir"; "/new"]];
1038     InitBasicFS, Always, TestLastFail
1039       [["touch"; "/new"];
1040        ["rmdir"; "/new"]]],
1041    "remove a directory",
1042    "\
1043 Remove the single directory C<path>.");
1044
1045   ("rm_rf", (RErr, [String "path"]), 31, [],
1046    [InitBasicFS, Always, TestOutputFalse
1047       [["mkdir"; "/new"];
1048        ["mkdir"; "/new/foo"];
1049        ["touch"; "/new/foo/bar"];
1050        ["rm_rf"; "/new"];
1051        ["exists"; "/new"]]],
1052    "remove a file or directory recursively",
1053    "\
1054 Remove the file or directory C<path>, recursively removing the
1055 contents if its a directory.  This is like the C<rm -rf> shell
1056 command.");
1057
1058   ("mkdir", (RErr, [String "path"]), 32, [],
1059    [InitBasicFS, Always, TestOutputTrue
1060       [["mkdir"; "/new"];
1061        ["is_dir"; "/new"]];
1062     InitBasicFS, Always, TestLastFail
1063       [["mkdir"; "/new/foo/bar"]]],
1064    "create a directory",
1065    "\
1066 Create a directory named C<path>.");
1067
1068   ("mkdir_p", (RErr, [String "path"]), 33, [],
1069    [InitBasicFS, Always, TestOutputTrue
1070       [["mkdir_p"; "/new/foo/bar"];
1071        ["is_dir"; "/new/foo/bar"]];
1072     InitBasicFS, Always, TestOutputTrue
1073       [["mkdir_p"; "/new/foo/bar"];
1074        ["is_dir"; "/new/foo"]];
1075     InitBasicFS, Always, TestOutputTrue
1076       [["mkdir_p"; "/new/foo/bar"];
1077        ["is_dir"; "/new"]];
1078     (* Regression tests for RHBZ#503133: *)
1079     InitBasicFS, Always, TestRun
1080       [["mkdir"; "/new"];
1081        ["mkdir_p"; "/new"]];
1082     InitBasicFS, Always, TestLastFail
1083       [["touch"; "/new"];
1084        ["mkdir_p"; "/new"]]],
1085    "create a directory and parents",
1086    "\
1087 Create a directory named C<path>, creating any parent directories
1088 as necessary.  This is like the C<mkdir -p> shell command.");
1089
1090   ("chmod", (RErr, [Int "mode"; String "path"]), 34, [],
1091    [], (* XXX Need stat command to test *)
1092    "change file mode",
1093    "\
1094 Change the mode (permissions) of C<path> to C<mode>.  Only
1095 numeric modes are supported.");
1096
1097   ("chown", (RErr, [Int "owner"; Int "group"; String "path"]), 35, [],
1098    [], (* XXX Need stat command to test *)
1099    "change file owner and group",
1100    "\
1101 Change the file owner to C<owner> and group to C<group>.
1102
1103 Only numeric uid and gid are supported.  If you want to use
1104 names, you will need to locate and parse the password file
1105 yourself (Augeas support makes this relatively easy).");
1106
1107   ("exists", (RBool "existsflag", [String "path"]), 36, [],
1108    [InitBasicFS, Always, TestOutputTrue (
1109       [["touch"; "/new"];
1110        ["exists"; "/new"]]);
1111     InitBasicFS, Always, TestOutputTrue (
1112       [["mkdir"; "/new"];
1113        ["exists"; "/new"]])],
1114    "test if file or directory exists",
1115    "\
1116 This returns C<true> if and only if there is a file, directory
1117 (or anything) with the given C<path> name.
1118
1119 See also C<guestfs_is_file>, C<guestfs_is_dir>, C<guestfs_stat>.");
1120
1121   ("is_file", (RBool "fileflag", [String "path"]), 37, [],
1122    [InitBasicFS, Always, TestOutputTrue (
1123       [["touch"; "/new"];
1124        ["is_file"; "/new"]]);
1125     InitBasicFS, Always, TestOutputFalse (
1126       [["mkdir"; "/new"];
1127        ["is_file"; "/new"]])],
1128    "test if file exists",
1129    "\
1130 This returns C<true> if and only if there is a file
1131 with the given C<path> name.  Note that it returns false for
1132 other objects like directories.
1133
1134 See also C<guestfs_stat>.");
1135
1136   ("is_dir", (RBool "dirflag", [String "path"]), 38, [],
1137    [InitBasicFS, Always, TestOutputFalse (
1138       [["touch"; "/new"];
1139        ["is_dir"; "/new"]]);
1140     InitBasicFS, Always, TestOutputTrue (
1141       [["mkdir"; "/new"];
1142        ["is_dir"; "/new"]])],
1143    "test if file exists",
1144    "\
1145 This returns C<true> if and only if there is a directory
1146 with the given C<path> name.  Note that it returns false for
1147 other objects like files.
1148
1149 See also C<guestfs_stat>.");
1150
1151   ("pvcreate", (RErr, [String "device"]), 39, [],
1152    [InitEmpty, Always, TestOutputListOfDevices (
1153       [["sfdiskM"; "/dev/sda"; ",100 ,200 ,"];
1154        ["pvcreate"; "/dev/sda1"];
1155        ["pvcreate"; "/dev/sda2"];
1156        ["pvcreate"; "/dev/sda3"];
1157        ["pvs"]], ["/dev/sda1"; "/dev/sda2"; "/dev/sda3"])],
1158    "create an LVM physical volume",
1159    "\
1160 This creates an LVM physical volume on the named C<device>,
1161 where C<device> should usually be a partition name such
1162 as C</dev/sda1>.");
1163
1164   ("vgcreate", (RErr, [String "volgroup"; StringList "physvols"]), 40, [],
1165    [InitEmpty, Always, TestOutputList (
1166       [["sfdiskM"; "/dev/sda"; ",100 ,200 ,"];
1167        ["pvcreate"; "/dev/sda1"];
1168        ["pvcreate"; "/dev/sda2"];
1169        ["pvcreate"; "/dev/sda3"];
1170        ["vgcreate"; "VG1"; "/dev/sda1 /dev/sda2"];
1171        ["vgcreate"; "VG2"; "/dev/sda3"];
1172        ["vgs"]], ["VG1"; "VG2"])],
1173    "create an LVM volume group",
1174    "\
1175 This creates an LVM volume group called C<volgroup>
1176 from the non-empty list of physical volumes C<physvols>.");
1177
1178   ("lvcreate", (RErr, [String "logvol"; String "volgroup"; Int "mbytes"]), 41, [],
1179    [InitEmpty, Always, TestOutputList (
1180       [["sfdiskM"; "/dev/sda"; ",100 ,200 ,"];
1181        ["pvcreate"; "/dev/sda1"];
1182        ["pvcreate"; "/dev/sda2"];
1183        ["pvcreate"; "/dev/sda3"];
1184        ["vgcreate"; "VG1"; "/dev/sda1 /dev/sda2"];
1185        ["vgcreate"; "VG2"; "/dev/sda3"];
1186        ["lvcreate"; "LV1"; "VG1"; "50"];
1187        ["lvcreate"; "LV2"; "VG1"; "50"];
1188        ["lvcreate"; "LV3"; "VG2"; "50"];
1189        ["lvcreate"; "LV4"; "VG2"; "50"];
1190        ["lvcreate"; "LV5"; "VG2"; "50"];
1191        ["lvs"]],
1192       ["/dev/VG1/LV1"; "/dev/VG1/LV2";
1193        "/dev/VG2/LV3"; "/dev/VG2/LV4"; "/dev/VG2/LV5"])],
1194    "create an LVM volume group",
1195    "\
1196 This creates an LVM volume group called C<logvol>
1197 on the volume group C<volgroup>, with C<size> megabytes.");
1198
1199   ("mkfs", (RErr, [String "fstype"; String "device"]), 42, [],
1200    [InitEmpty, Always, TestOutput (
1201       [["sfdiskM"; "/dev/sda"; ","];
1202        ["mkfs"; "ext2"; "/dev/sda1"];
1203        ["mount"; "/dev/sda1"; "/"];
1204        ["write_file"; "/new"; "new file contents"; "0"];
1205        ["cat"; "/new"]], "new file contents")],
1206    "make a filesystem",
1207    "\
1208 This creates a filesystem on C<device> (usually a partition
1209 or LVM logical volume).  The filesystem type is C<fstype>, for
1210 example C<ext3>.");
1211
1212   ("sfdisk", (RErr, [String "device";
1213                      Int "cyls"; Int "heads"; Int "sectors";
1214                      StringList "lines"]), 43, [DangerWillRobinson],
1215    [],
1216    "create partitions on a block device",
1217    "\
1218 This is a direct interface to the L<sfdisk(8)> program for creating
1219 partitions on block devices.
1220
1221 C<device> should be a block device, for example C</dev/sda>.
1222
1223 C<cyls>, C<heads> and C<sectors> are the number of cylinders, heads
1224 and sectors on the device, which are passed directly to sfdisk as
1225 the I<-C>, I<-H> and I<-S> parameters.  If you pass C<0> for any
1226 of these, then the corresponding parameter is omitted.  Usually for
1227 'large' disks, you can just pass C<0> for these, but for small
1228 (floppy-sized) disks, sfdisk (or rather, the kernel) cannot work
1229 out the right geometry and you will need to tell it.
1230
1231 C<lines> is a list of lines that we feed to C<sfdisk>.  For more
1232 information refer to the L<sfdisk(8)> manpage.
1233
1234 To create a single partition occupying the whole disk, you would
1235 pass C<lines> as a single element list, when the single element being
1236 the string C<,> (comma).
1237
1238 See also: C<guestfs_sfdisk_l>, C<guestfs_sfdisk_N>");
1239
1240   ("write_file", (RErr, [String "path"; String "content"; Int "size"]), 44, [ProtocolLimitWarning],
1241    [InitBasicFS, Always, TestOutput (
1242       [["write_file"; "/new"; "new file contents"; "0"];
1243        ["cat"; "/new"]], "new file contents");
1244     InitBasicFS, Always, TestOutput (
1245       [["write_file"; "/new"; "\nnew file contents\n"; "0"];
1246        ["cat"; "/new"]], "\nnew file contents\n");
1247     InitBasicFS, Always, TestOutput (
1248       [["write_file"; "/new"; "\n\n"; "0"];
1249        ["cat"; "/new"]], "\n\n");
1250     InitBasicFS, Always, TestOutput (
1251       [["write_file"; "/new"; ""; "0"];
1252        ["cat"; "/new"]], "");
1253     InitBasicFS, Always, TestOutput (
1254       [["write_file"; "/new"; "\n\n\n"; "0"];
1255        ["cat"; "/new"]], "\n\n\n");
1256     InitBasicFS, Always, TestOutput (
1257       [["write_file"; "/new"; "\n"; "0"];
1258        ["cat"; "/new"]], "\n")],
1259    "create a file",
1260    "\
1261 This call creates a file called C<path>.  The contents of the
1262 file is the string C<content> (which can contain any 8 bit data),
1263 with length C<size>.
1264
1265 As a special case, if C<size> is C<0>
1266 then the length is calculated using C<strlen> (so in this case
1267 the content cannot contain embedded ASCII NULs).
1268
1269 I<NB.> Owing to a bug, writing content containing ASCII NUL
1270 characters does I<not> work, even if the length is specified.
1271 We hope to resolve this bug in a future version.  In the meantime
1272 use C<guestfs_upload>.");
1273
1274   ("umount", (RErr, [String "pathordevice"]), 45, [FishAlias "unmount"],
1275    [InitEmpty, Always, TestOutputListOfDevices (
1276       [["sfdiskM"; "/dev/sda"; ","];
1277        ["mkfs"; "ext2"; "/dev/sda1"];
1278        ["mount"; "/dev/sda1"; "/"];
1279        ["mounts"]], ["/dev/sda1"]);
1280     InitEmpty, Always, TestOutputList (
1281       [["sfdiskM"; "/dev/sda"; ","];
1282        ["mkfs"; "ext2"; "/dev/sda1"];
1283        ["mount"; "/dev/sda1"; "/"];
1284        ["umount"; "/"];
1285        ["mounts"]], [])],
1286    "unmount a filesystem",
1287    "\
1288 This unmounts the given filesystem.  The filesystem may be
1289 specified either by its mountpoint (path) or the device which
1290 contains the filesystem.");
1291
1292   ("mounts", (RStringList "devices", []), 46, [],
1293    [InitBasicFS, Always, TestOutputListOfDevices (
1294       [["mounts"]], ["/dev/sda1"])],
1295    "show mounted filesystems",
1296    "\
1297 This returns the list of currently mounted filesystems.  It returns
1298 the list of devices (eg. C</dev/sda1>, C</dev/VG/LV>).
1299
1300 Some internal mounts are not shown.");
1301
1302   ("umount_all", (RErr, []), 47, [FishAlias "unmount-all"],
1303    [InitBasicFS, Always, TestOutputList (
1304       [["umount_all"];
1305        ["mounts"]], []);
1306     (* check that umount_all can unmount nested mounts correctly: *)
1307     InitEmpty, Always, TestOutputList (
1308       [["sfdiskM"; "/dev/sda"; ",100 ,200 ,"];
1309        ["mkfs"; "ext2"; "/dev/sda1"];
1310        ["mkfs"; "ext2"; "/dev/sda2"];
1311        ["mkfs"; "ext2"; "/dev/sda3"];
1312        ["mount"; "/dev/sda1"; "/"];
1313        ["mkdir"; "/mp1"];
1314        ["mount"; "/dev/sda2"; "/mp1"];
1315        ["mkdir"; "/mp1/mp2"];
1316        ["mount"; "/dev/sda3"; "/mp1/mp2"];
1317        ["mkdir"; "/mp1/mp2/mp3"];
1318        ["umount_all"];
1319        ["mounts"]], [])],
1320    "unmount all filesystems",
1321    "\
1322 This unmounts all mounted filesystems.
1323
1324 Some internal mounts are not unmounted by this call.");
1325
1326   ("lvm_remove_all", (RErr, []), 48, [DangerWillRobinson],
1327    [],
1328    "remove all LVM LVs, VGs and PVs",
1329    "\
1330 This command removes all LVM logical volumes, volume groups
1331 and physical volumes.");
1332
1333   ("file", (RString "description", [String "path"]), 49, [],
1334    [InitBasicFS, Always, TestOutput (
1335       [["touch"; "/new"];
1336        ["file"; "/new"]], "empty");
1337     InitBasicFS, Always, TestOutput (
1338       [["write_file"; "/new"; "some content\n"; "0"];
1339        ["file"; "/new"]], "ASCII text");
1340     InitBasicFS, Always, TestLastFail (
1341       [["file"; "/nofile"]])],
1342    "determine file type",
1343    "\
1344 This call uses the standard L<file(1)> command to determine
1345 the type or contents of the file.  This also works on devices,
1346 for example to find out whether a partition contains a filesystem.
1347
1348 The exact command which runs is C<file -bsL path>.  Note in
1349 particular that the filename is not prepended to the output
1350 (the C<-b> option).");
1351
1352   ("command", (RString "output", [StringList "arguments"]), 50, [ProtocolLimitWarning],
1353    [InitBasicFS, Always, TestOutput (
1354       [["upload"; "test-command"; "/test-command"];
1355        ["chmod"; "0o755"; "/test-command"];
1356        ["command"; "/test-command 1"]], "Result1");
1357     InitBasicFS, Always, TestOutput (
1358       [["upload"; "test-command"; "/test-command"];
1359        ["chmod"; "0o755"; "/test-command"];
1360        ["command"; "/test-command 2"]], "Result2\n");
1361     InitBasicFS, Always, TestOutput (
1362       [["upload"; "test-command"; "/test-command"];
1363        ["chmod"; "0o755"; "/test-command"];
1364        ["command"; "/test-command 3"]], "\nResult3");
1365     InitBasicFS, Always, TestOutput (
1366       [["upload"; "test-command"; "/test-command"];
1367        ["chmod"; "0o755"; "/test-command"];
1368        ["command"; "/test-command 4"]], "\nResult4\n");
1369     InitBasicFS, Always, TestOutput (
1370       [["upload"; "test-command"; "/test-command"];
1371        ["chmod"; "0o755"; "/test-command"];
1372        ["command"; "/test-command 5"]], "\nResult5\n\n");
1373     InitBasicFS, Always, TestOutput (
1374       [["upload"; "test-command"; "/test-command"];
1375        ["chmod"; "0o755"; "/test-command"];
1376        ["command"; "/test-command 6"]], "\n\nResult6\n\n");
1377     InitBasicFS, Always, TestOutput (
1378       [["upload"; "test-command"; "/test-command"];
1379        ["chmod"; "0o755"; "/test-command"];
1380        ["command"; "/test-command 7"]], "");
1381     InitBasicFS, Always, TestOutput (
1382       [["upload"; "test-command"; "/test-command"];
1383        ["chmod"; "0o755"; "/test-command"];
1384        ["command"; "/test-command 8"]], "\n");
1385     InitBasicFS, Always, TestOutput (
1386       [["upload"; "test-command"; "/test-command"];
1387        ["chmod"; "0o755"; "/test-command"];
1388        ["command"; "/test-command 9"]], "\n\n");
1389     InitBasicFS, Always, TestOutput (
1390       [["upload"; "test-command"; "/test-command"];
1391        ["chmod"; "0o755"; "/test-command"];
1392        ["command"; "/test-command 10"]], "Result10-1\nResult10-2\n");
1393     InitBasicFS, Always, TestOutput (
1394       [["upload"; "test-command"; "/test-command"];
1395        ["chmod"; "0o755"; "/test-command"];
1396        ["command"; "/test-command 11"]], "Result11-1\nResult11-2");
1397     InitBasicFS, Always, TestLastFail (
1398       [["upload"; "test-command"; "/test-command"];
1399        ["chmod"; "0o755"; "/test-command"];
1400        ["command"; "/test-command"]])],
1401    "run a command from the guest filesystem",
1402    "\
1403 This call runs a command from the guest filesystem.  The
1404 filesystem must be mounted, and must contain a compatible
1405 operating system (ie. something Linux, with the same
1406 or compatible processor architecture).
1407
1408 The single parameter is an argv-style list of arguments.
1409 The first element is the name of the program to run.
1410 Subsequent elements are parameters.  The list must be
1411 non-empty (ie. must contain a program name).  Note that
1412 the command runs directly, and is I<not> invoked via
1413 the shell (see C<guestfs_sh>).
1414
1415 The return value is anything printed to I<stdout> by
1416 the command.
1417
1418 If the command returns a non-zero exit status, then
1419 this function returns an error message.  The error message
1420 string is the content of I<stderr> from the command.
1421
1422 The C<$PATH> environment variable will contain at least
1423 C</usr/bin> and C</bin>.  If you require a program from
1424 another location, you should provide the full path in the
1425 first parameter.
1426
1427 Shared libraries and data files required by the program
1428 must be available on filesystems which are mounted in the
1429 correct places.  It is the caller's responsibility to ensure
1430 all filesystems that are needed are mounted at the right
1431 locations.");
1432
1433   ("command_lines", (RStringList "lines", [StringList "arguments"]), 51, [ProtocolLimitWarning],
1434    [InitBasicFS, Always, TestOutputList (
1435       [["upload"; "test-command"; "/test-command"];
1436        ["chmod"; "0o755"; "/test-command"];
1437        ["command_lines"; "/test-command 1"]], ["Result1"]);
1438     InitBasicFS, Always, TestOutputList (
1439       [["upload"; "test-command"; "/test-command"];
1440        ["chmod"; "0o755"; "/test-command"];
1441        ["command_lines"; "/test-command 2"]], ["Result2"]);
1442     InitBasicFS, Always, TestOutputList (
1443       [["upload"; "test-command"; "/test-command"];
1444        ["chmod"; "0o755"; "/test-command"];
1445        ["command_lines"; "/test-command 3"]], ["";"Result3"]);
1446     InitBasicFS, Always, TestOutputList (
1447       [["upload"; "test-command"; "/test-command"];
1448        ["chmod"; "0o755"; "/test-command"];
1449        ["command_lines"; "/test-command 4"]], ["";"Result4"]);
1450     InitBasicFS, Always, TestOutputList (
1451       [["upload"; "test-command"; "/test-command"];
1452        ["chmod"; "0o755"; "/test-command"];
1453        ["command_lines"; "/test-command 5"]], ["";"Result5";""]);
1454     InitBasicFS, Always, TestOutputList (
1455       [["upload"; "test-command"; "/test-command"];
1456        ["chmod"; "0o755"; "/test-command"];
1457        ["command_lines"; "/test-command 6"]], ["";"";"Result6";""]);
1458     InitBasicFS, Always, TestOutputList (
1459       [["upload"; "test-command"; "/test-command"];
1460        ["chmod"; "0o755"; "/test-command"];
1461        ["command_lines"; "/test-command 7"]], []);
1462     InitBasicFS, Always, TestOutputList (
1463       [["upload"; "test-command"; "/test-command"];
1464        ["chmod"; "0o755"; "/test-command"];
1465        ["command_lines"; "/test-command 8"]], [""]);
1466     InitBasicFS, Always, TestOutputList (
1467       [["upload"; "test-command"; "/test-command"];
1468        ["chmod"; "0o755"; "/test-command"];
1469        ["command_lines"; "/test-command 9"]], ["";""]);
1470     InitBasicFS, Always, TestOutputList (
1471       [["upload"; "test-command"; "/test-command"];
1472        ["chmod"; "0o755"; "/test-command"];
1473        ["command_lines"; "/test-command 10"]], ["Result10-1";"Result10-2"]);
1474     InitBasicFS, Always, TestOutputList (
1475       [["upload"; "test-command"; "/test-command"];
1476        ["chmod"; "0o755"; "/test-command"];
1477        ["command_lines"; "/test-command 11"]], ["Result11-1";"Result11-2"])],
1478    "run a command, returning lines",
1479    "\
1480 This is the same as C<guestfs_command>, but splits the
1481 result into a list of lines.
1482
1483 See also: C<guestfs_sh_lines>");
1484
1485   ("stat", (RStruct ("statbuf", "stat"), [String "path"]), 52, [],
1486    [InitBasicFS, Always, TestOutputStruct (
1487       [["touch"; "/new"];
1488        ["stat"; "/new"]], [CompareWithInt ("size", 0)])],
1489    "get file information",
1490    "\
1491 Returns file information for the given C<path>.
1492
1493 This is the same as the C<stat(2)> system call.");
1494
1495   ("lstat", (RStruct ("statbuf", "stat"), [String "path"]), 53, [],
1496    [InitBasicFS, Always, TestOutputStruct (
1497       [["touch"; "/new"];
1498        ["lstat"; "/new"]], [CompareWithInt ("size", 0)])],
1499    "get file information for a symbolic link",
1500    "\
1501 Returns file information for the given C<path>.
1502
1503 This is the same as C<guestfs_stat> except that if C<path>
1504 is a symbolic link, then the link is stat-ed, not the file it
1505 refers to.
1506
1507 This is the same as the C<lstat(2)> system call.");
1508
1509   ("statvfs", (RStruct ("statbuf", "statvfs"), [String "path"]), 54, [],
1510    [InitBasicFS, Always, TestOutputStruct (
1511       [["statvfs"; "/"]], [CompareWithInt ("namemax", 255);
1512                            CompareWithInt ("bsize", 1024)])],
1513    "get file system statistics",
1514    "\
1515 Returns file system statistics for any mounted file system.
1516 C<path> should be a file or directory in the mounted file system
1517 (typically it is the mount point itself, but it doesn't need to be).
1518
1519 This is the same as the C<statvfs(2)> system call.");
1520
1521   ("tune2fs_l", (RHashtable "superblock", [String "device"]), 55, [],
1522    [], (* XXX test *)
1523    "get ext2/ext3/ext4 superblock details",
1524    "\
1525 This returns the contents of the ext2, ext3 or ext4 filesystem
1526 superblock on C<device>.
1527
1528 It is the same as running C<tune2fs -l device>.  See L<tune2fs(8)>
1529 manpage for more details.  The list of fields returned isn't
1530 clearly defined, and depends on both the version of C<tune2fs>
1531 that libguestfs was built against, and the filesystem itself.");
1532
1533   ("blockdev_setro", (RErr, [String "device"]), 56, [],
1534    [InitEmpty, Always, TestOutputTrue (
1535       [["blockdev_setro"; "/dev/sda"];
1536        ["blockdev_getro"; "/dev/sda"]])],
1537    "set block device to read-only",
1538    "\
1539 Sets the block device named C<device> to read-only.
1540
1541 This uses the L<blockdev(8)> command.");
1542
1543   ("blockdev_setrw", (RErr, [String "device"]), 57, [],
1544    [InitEmpty, Always, TestOutputFalse (
1545       [["blockdev_setrw"; "/dev/sda"];
1546        ["blockdev_getro"; "/dev/sda"]])],
1547    "set block device to read-write",
1548    "\
1549 Sets the block device named C<device> to read-write.
1550
1551 This uses the L<blockdev(8)> command.");
1552
1553   ("blockdev_getro", (RBool "ro", [String "device"]), 58, [],
1554    [InitEmpty, Always, TestOutputTrue (
1555       [["blockdev_setro"; "/dev/sda"];
1556        ["blockdev_getro"; "/dev/sda"]])],
1557    "is block device set to read-only",
1558    "\
1559 Returns a boolean indicating if the block device is read-only
1560 (true if read-only, false if not).
1561
1562 This uses the L<blockdev(8)> command.");
1563
1564   ("blockdev_getss", (RInt "sectorsize", [String "device"]), 59, [],
1565    [InitEmpty, Always, TestOutputInt (
1566       [["blockdev_getss"; "/dev/sda"]], 512)],
1567    "get sectorsize of block device",
1568    "\
1569 This returns the size of sectors on a block device.
1570 Usually 512, but can be larger for modern devices.
1571
1572 (Note, this is not the size in sectors, use C<guestfs_blockdev_getsz>
1573 for that).
1574
1575 This uses the L<blockdev(8)> command.");
1576
1577   ("blockdev_getbsz", (RInt "blocksize", [String "device"]), 60, [],
1578    [InitEmpty, Always, TestOutputInt (
1579       [["blockdev_getbsz"; "/dev/sda"]], 4096)],
1580    "get blocksize of block device",
1581    "\
1582 This returns the block size of a device.
1583
1584 (Note this is different from both I<size in blocks> and
1585 I<filesystem block size>).
1586
1587 This uses the L<blockdev(8)> command.");
1588
1589   ("blockdev_setbsz", (RErr, [String "device"; Int "blocksize"]), 61, [],
1590    [], (* XXX test *)
1591    "set blocksize of block device",
1592    "\
1593 This sets the block size of a device.
1594
1595 (Note this is different from both I<size in blocks> and
1596 I<filesystem block size>).
1597
1598 This uses the L<blockdev(8)> command.");
1599
1600   ("blockdev_getsz", (RInt64 "sizeinsectors", [String "device"]), 62, [],
1601    [InitEmpty, Always, TestOutputInt (
1602       [["blockdev_getsz"; "/dev/sda"]], 1024000)],
1603    "get total size of device in 512-byte sectors",
1604    "\
1605 This returns the size of the device in units of 512-byte sectors
1606 (even if the sectorsize isn't 512 bytes ... weird).
1607
1608 See also C<guestfs_blockdev_getss> for the real sector size of
1609 the device, and C<guestfs_blockdev_getsize64> for the more
1610 useful I<size in bytes>.
1611
1612 This uses the L<blockdev(8)> command.");
1613
1614   ("blockdev_getsize64", (RInt64 "sizeinbytes", [String "device"]), 63, [],
1615    [InitEmpty, Always, TestOutputInt (
1616       [["blockdev_getsize64"; "/dev/sda"]], 524288000)],
1617    "get total size of device in bytes",
1618    "\
1619 This returns the size of the device in bytes.
1620
1621 See also C<guestfs_blockdev_getsz>.
1622
1623 This uses the L<blockdev(8)> command.");
1624
1625   ("blockdev_flushbufs", (RErr, [String "device"]), 64, [],
1626    [InitEmpty, Always, TestRun
1627       [["blockdev_flushbufs"; "/dev/sda"]]],
1628    "flush device buffers",
1629    "\
1630 This tells the kernel to flush internal buffers associated
1631 with C<device>.
1632
1633 This uses the L<blockdev(8)> command.");
1634
1635   ("blockdev_rereadpt", (RErr, [String "device"]), 65, [],
1636    [InitEmpty, Always, TestRun
1637       [["blockdev_rereadpt"; "/dev/sda"]]],
1638    "reread partition table",
1639    "\
1640 Reread the partition table on C<device>.
1641
1642 This uses the L<blockdev(8)> command.");
1643
1644   ("upload", (RErr, [FileIn "filename"; String "remotefilename"]), 66, [],
1645    [InitBasicFS, Always, TestOutput (
1646       (* Pick a file from cwd which isn't likely to change. *)
1647     [["upload"; "../COPYING.LIB"; "/COPYING.LIB"];
1648      ["checksum"; "md5"; "/COPYING.LIB"]], "e3eda01d9815f8d24aae2dbd89b68b06")],
1649    "upload a file from the local machine",
1650    "\
1651 Upload local file C<filename> to C<remotefilename> on the
1652 filesystem.
1653
1654 C<filename> can also be a named pipe.
1655
1656 See also C<guestfs_download>.");
1657
1658   ("download", (RErr, [String "remotefilename"; FileOut "filename"]), 67, [],
1659    [InitBasicFS, Always, TestOutput (
1660       (* Pick a file from cwd which isn't likely to change. *)
1661     [["upload"; "../COPYING.LIB"; "/COPYING.LIB"];
1662      ["download"; "/COPYING.LIB"; "testdownload.tmp"];
1663      ["upload"; "testdownload.tmp"; "/upload"];
1664      ["checksum"; "md5"; "/upload"]], "e3eda01d9815f8d24aae2dbd89b68b06")],
1665    "download a file to the local machine",
1666    "\
1667 Download file C<remotefilename> and save it as C<filename>
1668 on the local machine.
1669
1670 C<filename> can also be a named pipe.
1671
1672 See also C<guestfs_upload>, C<guestfs_cat>.");
1673
1674   ("checksum", (RString "checksum", [String "csumtype"; String "path"]), 68, [],
1675    [InitBasicFS, Always, TestOutput (
1676       [["write_file"; "/new"; "test\n"; "0"];
1677        ["checksum"; "crc"; "/new"]], "935282863");
1678     InitBasicFS, Always, TestLastFail (
1679       [["checksum"; "crc"; "/new"]]);
1680     InitBasicFS, Always, TestOutput (
1681       [["write_file"; "/new"; "test\n"; "0"];
1682        ["checksum"; "md5"; "/new"]], "d8e8fca2dc0f896fd7cb4cb0031ba249");
1683     InitBasicFS, Always, TestOutput (
1684       [["write_file"; "/new"; "test\n"; "0"];
1685        ["checksum"; "sha1"; "/new"]], "4e1243bd22c66e76c2ba9eddc1f91394e57f9f83");
1686     InitBasicFS, Always, TestOutput (
1687       [["write_file"; "/new"; "test\n"; "0"];
1688        ["checksum"; "sha224"; "/new"]], "52f1bf093f4b7588726035c176c0cdb4376cfea53819f1395ac9e6ec");
1689     InitBasicFS, Always, TestOutput (
1690       [["write_file"; "/new"; "test\n"; "0"];
1691        ["checksum"; "sha256"; "/new"]], "f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2");
1692     InitBasicFS, Always, TestOutput (
1693       [["write_file"; "/new"; "test\n"; "0"];
1694        ["checksum"; "sha384"; "/new"]], "109bb6b5b6d5547c1ce03c7a8bd7d8f80c1cb0957f50c4f7fda04692079917e4f9cad52b878f3d8234e1a170b154b72d");
1695     InitBasicFS, Always, TestOutput (
1696       [["write_file"; "/new"; "test\n"; "0"];
1697        ["checksum"; "sha512"; "/new"]], "0e3e75234abc68f4378a86b3f4b32a198ba301845b0cd6e50106e874345700cc6663a86c1ea125dc5e92be17c98f9a0f85ca9d5f595db2012f7cc3571945c123");
1698     InitBasicFS, Always, TestOutput (
1699       (* RHEL 5 thinks this is an HFS+ filesystem unless we give
1700        * the type explicitly.
1701        *)
1702       [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
1703        ["checksum"; "md5"; "/known-3"]], "46d6ca27ee07cdc6fa99c2e138cc522c")],
1704    "compute MD5, SHAx or CRC checksum of file",
1705    "\
1706 This call computes the MD5, SHAx or CRC checksum of the
1707 file named C<path>.
1708
1709 The type of checksum to compute is given by the C<csumtype>
1710 parameter which must have one of the following values:
1711
1712 =over 4
1713
1714 =item C<crc>
1715
1716 Compute the cyclic redundancy check (CRC) specified by POSIX
1717 for the C<cksum> command.
1718
1719 =item C<md5>
1720
1721 Compute the MD5 hash (using the C<md5sum> program).
1722
1723 =item C<sha1>
1724
1725 Compute the SHA1 hash (using the C<sha1sum> program).
1726
1727 =item C<sha224>
1728
1729 Compute the SHA224 hash (using the C<sha224sum> program).
1730
1731 =item C<sha256>
1732
1733 Compute the SHA256 hash (using the C<sha256sum> program).
1734
1735 =item C<sha384>
1736
1737 Compute the SHA384 hash (using the C<sha384sum> program).
1738
1739 =item C<sha512>
1740
1741 Compute the SHA512 hash (using the C<sha512sum> program).
1742
1743 =back
1744
1745 The checksum is returned as a printable string.");
1746
1747   ("tar_in", (RErr, [FileIn "tarfile"; String "directory"]), 69, [],
1748    [InitBasicFS, Always, TestOutput (
1749       [["tar_in"; "../images/helloworld.tar"; "/"];
1750        ["cat"; "/hello"]], "hello\n")],
1751    "unpack tarfile to directory",
1752    "\
1753 This command uploads and unpacks local file C<tarfile> (an
1754 I<uncompressed> tar file) into C<directory>.
1755
1756 To upload a compressed tarball, use C<guestfs_tgz_in>.");
1757
1758   ("tar_out", (RErr, [String "directory"; FileOut "tarfile"]), 70, [],
1759    [],
1760    "pack directory into tarfile",
1761    "\
1762 This command packs the contents of C<directory> and downloads
1763 it to local file C<tarfile>.
1764
1765 To download a compressed tarball, use C<guestfs_tgz_out>.");
1766
1767   ("tgz_in", (RErr, [FileIn "tarball"; String "directory"]), 71, [],
1768    [InitBasicFS, Always, TestOutput (
1769       [["tgz_in"; "../images/helloworld.tar.gz"; "/"];
1770        ["cat"; "/hello"]], "hello\n")],
1771    "unpack compressed tarball to directory",
1772    "\
1773 This command uploads and unpacks local file C<tarball> (a
1774 I<gzip compressed> tar file) into C<directory>.
1775
1776 To upload an uncompressed tarball, use C<guestfs_tar_in>.");
1777
1778   ("tgz_out", (RErr, [String "directory"; FileOut "tarball"]), 72, [],
1779    [],
1780    "pack directory into compressed tarball",
1781    "\
1782 This command packs the contents of C<directory> and downloads
1783 it to local file C<tarball>.
1784
1785 To download an uncompressed tarball, use C<guestfs_tar_out>.");
1786
1787   ("mount_ro", (RErr, [String "device"; String "mountpoint"]), 73, [],
1788    [InitBasicFS, Always, TestLastFail (
1789       [["umount"; "/"];
1790        ["mount_ro"; "/dev/sda1"; "/"];
1791        ["touch"; "/new"]]);
1792     InitBasicFS, Always, TestOutput (
1793       [["write_file"; "/new"; "data"; "0"];
1794        ["umount"; "/"];
1795        ["mount_ro"; "/dev/sda1"; "/"];
1796        ["cat"; "/new"]], "data")],
1797    "mount a guest disk, read-only",
1798    "\
1799 This is the same as the C<guestfs_mount> command, but it
1800 mounts the filesystem with the read-only (I<-o ro>) flag.");
1801
1802   ("mount_options", (RErr, [String "options"; String "device"; String "mountpoint"]), 74, [],
1803    [],
1804    "mount a guest disk with mount options",
1805    "\
1806 This is the same as the C<guestfs_mount> command, but it
1807 allows you to set the mount options as for the
1808 L<mount(8)> I<-o> flag.");
1809
1810   ("mount_vfs", (RErr, [String "options"; String "vfstype"; String "device"; String "mountpoint"]), 75, [],
1811    [],
1812    "mount a guest disk with mount options and vfstype",
1813    "\
1814 This is the same as the C<guestfs_mount> command, but it
1815 allows you to set both the mount options and the vfstype
1816 as for the L<mount(8)> I<-o> and I<-t> flags.");
1817
1818   ("debug", (RString "result", [String "subcmd"; StringList "extraargs"]), 76, [],
1819    [],
1820    "debugging and internals",
1821    "\
1822 The C<guestfs_debug> command exposes some internals of
1823 C<guestfsd> (the guestfs daemon) that runs inside the
1824 qemu subprocess.
1825
1826 There is no comprehensive help for this command.  You have
1827 to look at the file C<daemon/debug.c> in the libguestfs source
1828 to find out what you can do.");
1829
1830   ("lvremove", (RErr, [String "device"]), 77, [],
1831    [InitEmpty, Always, TestOutputList (
1832       [["sfdiskM"; "/dev/sda"; ","];
1833        ["pvcreate"; "/dev/sda1"];
1834        ["vgcreate"; "VG"; "/dev/sda1"];
1835        ["lvcreate"; "LV1"; "VG"; "50"];
1836        ["lvcreate"; "LV2"; "VG"; "50"];
1837        ["lvremove"; "/dev/VG/LV1"];
1838        ["lvs"]], ["/dev/VG/LV2"]);
1839     InitEmpty, Always, TestOutputList (
1840       [["sfdiskM"; "/dev/sda"; ","];
1841        ["pvcreate"; "/dev/sda1"];
1842        ["vgcreate"; "VG"; "/dev/sda1"];
1843        ["lvcreate"; "LV1"; "VG"; "50"];
1844        ["lvcreate"; "LV2"; "VG"; "50"];
1845        ["lvremove"; "/dev/VG"];
1846        ["lvs"]], []);
1847     InitEmpty, Always, TestOutputList (
1848       [["sfdiskM"; "/dev/sda"; ","];
1849        ["pvcreate"; "/dev/sda1"];
1850        ["vgcreate"; "VG"; "/dev/sda1"];
1851        ["lvcreate"; "LV1"; "VG"; "50"];
1852        ["lvcreate"; "LV2"; "VG"; "50"];
1853        ["lvremove"; "/dev/VG"];
1854        ["vgs"]], ["VG"])],
1855    "remove an LVM logical volume",
1856    "\
1857 Remove an LVM logical volume C<device>, where C<device> is
1858 the path to the LV, such as C</dev/VG/LV>.
1859
1860 You can also remove all LVs in a volume group by specifying
1861 the VG name, C</dev/VG>.");
1862
1863   ("vgremove", (RErr, [String "vgname"]), 78, [],
1864    [InitEmpty, Always, TestOutputList (
1865       [["sfdiskM"; "/dev/sda"; ","];
1866        ["pvcreate"; "/dev/sda1"];
1867        ["vgcreate"; "VG"; "/dev/sda1"];
1868        ["lvcreate"; "LV1"; "VG"; "50"];
1869        ["lvcreate"; "LV2"; "VG"; "50"];
1870        ["vgremove"; "VG"];
1871        ["lvs"]], []);
1872     InitEmpty, Always, TestOutputList (
1873       [["sfdiskM"; "/dev/sda"; ","];
1874        ["pvcreate"; "/dev/sda1"];
1875        ["vgcreate"; "VG"; "/dev/sda1"];
1876        ["lvcreate"; "LV1"; "VG"; "50"];
1877        ["lvcreate"; "LV2"; "VG"; "50"];
1878        ["vgremove"; "VG"];
1879        ["vgs"]], [])],
1880    "remove an LVM volume group",
1881    "\
1882 Remove an LVM volume group C<vgname>, (for example C<VG>).
1883
1884 This also forcibly removes all logical volumes in the volume
1885 group (if any).");
1886
1887   ("pvremove", (RErr, [String "device"]), 79, [],
1888    [InitEmpty, Always, TestOutputListOfDevices (
1889       [["sfdiskM"; "/dev/sda"; ","];
1890        ["pvcreate"; "/dev/sda1"];
1891        ["vgcreate"; "VG"; "/dev/sda1"];
1892        ["lvcreate"; "LV1"; "VG"; "50"];
1893        ["lvcreate"; "LV2"; "VG"; "50"];
1894        ["vgremove"; "VG"];
1895        ["pvremove"; "/dev/sda1"];
1896        ["lvs"]], []);
1897     InitEmpty, Always, TestOutputListOfDevices (
1898       [["sfdiskM"; "/dev/sda"; ","];
1899        ["pvcreate"; "/dev/sda1"];
1900        ["vgcreate"; "VG"; "/dev/sda1"];
1901        ["lvcreate"; "LV1"; "VG"; "50"];
1902        ["lvcreate"; "LV2"; "VG"; "50"];
1903        ["vgremove"; "VG"];
1904        ["pvremove"; "/dev/sda1"];
1905        ["vgs"]], []);
1906     InitEmpty, Always, TestOutputListOfDevices (
1907       [["sfdiskM"; "/dev/sda"; ","];
1908        ["pvcreate"; "/dev/sda1"];
1909        ["vgcreate"; "VG"; "/dev/sda1"];
1910        ["lvcreate"; "LV1"; "VG"; "50"];
1911        ["lvcreate"; "LV2"; "VG"; "50"];
1912        ["vgremove"; "VG"];
1913        ["pvremove"; "/dev/sda1"];
1914        ["pvs"]], [])],
1915    "remove an LVM physical volume",
1916    "\
1917 This wipes a physical volume C<device> so that LVM will no longer
1918 recognise it.
1919
1920 The implementation uses the C<pvremove> command which refuses to
1921 wipe physical volumes that contain any volume groups, so you have
1922 to remove those first.");
1923
1924   ("set_e2label", (RErr, [String "device"; String "label"]), 80, [],
1925    [InitBasicFS, Always, TestOutput (
1926       [["set_e2label"; "/dev/sda1"; "testlabel"];
1927        ["get_e2label"; "/dev/sda1"]], "testlabel")],
1928    "set the ext2/3/4 filesystem label",
1929    "\
1930 This sets the ext2/3/4 filesystem label of the filesystem on
1931 C<device> to C<label>.  Filesystem labels are limited to
1932 16 characters.
1933
1934 You can use either C<guestfs_tune2fs_l> or C<guestfs_get_e2label>
1935 to return the existing label on a filesystem.");
1936
1937   ("get_e2label", (RString "label", [String "device"]), 81, [],
1938    [],
1939    "get the ext2/3/4 filesystem label",
1940    "\
1941 This returns the ext2/3/4 filesystem label of the filesystem on
1942 C<device>.");
1943
1944   ("set_e2uuid", (RErr, [String "device"; String "uuid"]), 82, [],
1945    [InitBasicFS, Always, TestOutput (
1946       [["set_e2uuid"; "/dev/sda1"; "a3a61220-882b-4f61-89f4-cf24dcc7297d"];
1947        ["get_e2uuid"; "/dev/sda1"]], "a3a61220-882b-4f61-89f4-cf24dcc7297d");
1948     InitBasicFS, Always, TestOutput (
1949       [["set_e2uuid"; "/dev/sda1"; "clear"];
1950        ["get_e2uuid"; "/dev/sda1"]], "");
1951     (* We can't predict what UUIDs will be, so just check the commands run. *)
1952     InitBasicFS, Always, TestRun (
1953       [["set_e2uuid"; "/dev/sda1"; "random"]]);
1954     InitBasicFS, Always, TestRun (
1955       [["set_e2uuid"; "/dev/sda1"; "time"]])],
1956    "set the ext2/3/4 filesystem UUID",
1957    "\
1958 This sets the ext2/3/4 filesystem UUID of the filesystem on
1959 C<device> to C<uuid>.  The format of the UUID and alternatives
1960 such as C<clear>, C<random> and C<time> are described in the
1961 L<tune2fs(8)> manpage.
1962
1963 You can use either C<guestfs_tune2fs_l> or C<guestfs_get_e2uuid>
1964 to return the existing UUID of a filesystem.");
1965
1966   ("get_e2uuid", (RString "uuid", [String "device"]), 83, [],
1967    [],
1968    "get the ext2/3/4 filesystem UUID",
1969    "\
1970 This returns the ext2/3/4 filesystem UUID of the filesystem on
1971 C<device>.");
1972
1973   ("fsck", (RInt "status", [String "fstype"; String "device"]), 84, [],
1974    [InitBasicFS, Always, TestOutputInt (
1975       [["umount"; "/dev/sda1"];
1976        ["fsck"; "ext2"; "/dev/sda1"]], 0);
1977     InitBasicFS, Always, TestOutputInt (
1978       [["umount"; "/dev/sda1"];
1979        ["zero"; "/dev/sda1"];
1980        ["fsck"; "ext2"; "/dev/sda1"]], 8)],
1981    "run the filesystem checker",
1982    "\
1983 This runs the filesystem checker (fsck) on C<device> which
1984 should have filesystem type C<fstype>.
1985
1986 The returned integer is the status.  See L<fsck(8)> for the
1987 list of status codes from C<fsck>.
1988
1989 Notes:
1990
1991 =over 4
1992
1993 =item *
1994
1995 Multiple status codes can be summed together.
1996
1997 =item *
1998
1999 A non-zero return code can mean \"success\", for example if
2000 errors have been corrected on the filesystem.
2001
2002 =item *
2003
2004 Checking or repairing NTFS volumes is not supported
2005 (by linux-ntfs).
2006
2007 =back
2008
2009 This command is entirely equivalent to running C<fsck -a -t fstype device>.");
2010
2011   ("zero", (RErr, [String "device"]), 85, [],
2012    [InitBasicFS, Always, TestOutput (
2013       [["umount"; "/dev/sda1"];
2014        ["zero"; "/dev/sda1"];
2015        ["file"; "/dev/sda1"]], "data")],
2016    "write zeroes to the device",
2017    "\
2018 This command writes zeroes over the first few blocks of C<device>.
2019
2020 How many blocks are zeroed isn't specified (but it's I<not> enough
2021 to securely wipe the device).  It should be sufficient to remove
2022 any partition tables, filesystem superblocks and so on.
2023
2024 See also: C<guestfs_scrub_device>.");
2025
2026   ("grub_install", (RErr, [String "root"; String "device"]), 86, [],
2027    (* Test disabled because grub-install incompatible with virtio-blk driver.
2028     * See also: https://bugzilla.redhat.com/show_bug.cgi?id=479760
2029     *)
2030    [InitBasicFS, Disabled, TestOutputTrue (
2031       [["grub_install"; "/"; "/dev/sda1"];
2032        ["is_dir"; "/boot"]])],
2033    "install GRUB",
2034    "\
2035 This command installs GRUB (the Grand Unified Bootloader) on
2036 C<device>, with the root directory being C<root>.");
2037
2038   ("cp", (RErr, [String "src"; String "dest"]), 87, [],
2039    [InitBasicFS, Always, TestOutput (
2040       [["write_file"; "/old"; "file content"; "0"];
2041        ["cp"; "/old"; "/new"];
2042        ["cat"; "/new"]], "file content");
2043     InitBasicFS, Always, TestOutputTrue (
2044       [["write_file"; "/old"; "file content"; "0"];
2045        ["cp"; "/old"; "/new"];
2046        ["is_file"; "/old"]]);
2047     InitBasicFS, Always, TestOutput (
2048       [["write_file"; "/old"; "file content"; "0"];
2049        ["mkdir"; "/dir"];
2050        ["cp"; "/old"; "/dir/new"];
2051        ["cat"; "/dir/new"]], "file content")],
2052    "copy a file",
2053    "\
2054 This copies a file from C<src> to C<dest> where C<dest> is
2055 either a destination filename or destination directory.");
2056
2057   ("cp_a", (RErr, [String "src"; String "dest"]), 88, [],
2058    [InitBasicFS, Always, TestOutput (
2059       [["mkdir"; "/olddir"];
2060        ["mkdir"; "/newdir"];
2061        ["write_file"; "/olddir/file"; "file content"; "0"];
2062        ["cp_a"; "/olddir"; "/newdir"];
2063        ["cat"; "/newdir/olddir/file"]], "file content")],
2064    "copy a file or directory recursively",
2065    "\
2066 This copies a file or directory from C<src> to C<dest>
2067 recursively using the C<cp -a> command.");
2068
2069   ("mv", (RErr, [String "src"; String "dest"]), 89, [],
2070    [InitBasicFS, Always, TestOutput (
2071       [["write_file"; "/old"; "file content"; "0"];
2072        ["mv"; "/old"; "/new"];
2073        ["cat"; "/new"]], "file content");
2074     InitBasicFS, Always, TestOutputFalse (
2075       [["write_file"; "/old"; "file content"; "0"];
2076        ["mv"; "/old"; "/new"];
2077        ["is_file"; "/old"]])],
2078    "move a file",
2079    "\
2080 This moves a file from C<src> to C<dest> where C<dest> is
2081 either a destination filename or destination directory.");
2082
2083   ("drop_caches", (RErr, [Int "whattodrop"]), 90, [],
2084    [InitEmpty, Always, TestRun (
2085       [["drop_caches"; "3"]])],
2086    "drop kernel page cache, dentries and inodes",
2087    "\
2088 This instructs the guest kernel to drop its page cache,
2089 and/or dentries and inode caches.  The parameter C<whattodrop>
2090 tells the kernel what precisely to drop, see
2091 L<http://linux-mm.org/Drop_Caches>
2092
2093 Setting C<whattodrop> to 3 should drop everything.
2094
2095 This automatically calls L<sync(2)> before the operation,
2096 so that the maximum guest memory is freed.");
2097
2098   ("dmesg", (RString "kmsgs", []), 91, [],
2099    [InitEmpty, Always, TestRun (
2100       [["dmesg"]])],
2101    "return kernel messages",
2102    "\
2103 This returns the kernel messages (C<dmesg> output) from
2104 the guest kernel.  This is sometimes useful for extended
2105 debugging of problems.
2106
2107 Another way to get the same information is to enable
2108 verbose messages with C<guestfs_set_verbose> or by setting
2109 the environment variable C<LIBGUESTFS_DEBUG=1> before
2110 running the program.");
2111
2112   ("ping_daemon", (RErr, []), 92, [],
2113    [InitEmpty, Always, TestRun (
2114       [["ping_daemon"]])],
2115    "ping the guest daemon",
2116    "\
2117 This is a test probe into the guestfs daemon running inside
2118 the qemu subprocess.  Calling this function checks that the
2119 daemon responds to the ping message, without affecting the daemon
2120 or attached block device(s) in any other way.");
2121
2122   ("equal", (RBool "equality", [String "file1"; String "file2"]), 93, [],
2123    [InitBasicFS, Always, TestOutputTrue (
2124       [["write_file"; "/file1"; "contents of a file"; "0"];
2125        ["cp"; "/file1"; "/file2"];
2126        ["equal"; "/file1"; "/file2"]]);
2127     InitBasicFS, Always, TestOutputFalse (
2128       [["write_file"; "/file1"; "contents of a file"; "0"];
2129        ["write_file"; "/file2"; "contents of another file"; "0"];
2130        ["equal"; "/file1"; "/file2"]]);
2131     InitBasicFS, Always, TestLastFail (
2132       [["equal"; "/file1"; "/file2"]])],
2133    "test if two files have equal contents",
2134    "\
2135 This compares the two files C<file1> and C<file2> and returns
2136 true if their content is exactly equal, or false otherwise.
2137
2138 The external L<cmp(1)> program is used for the comparison.");
2139
2140   ("strings", (RStringList "stringsout", [String "path"]), 94, [ProtocolLimitWarning],
2141    [InitBasicFS, Always, TestOutputList (
2142       [["write_file"; "/new"; "hello\nworld\n"; "0"];
2143        ["strings"; "/new"]], ["hello"; "world"]);
2144     InitBasicFS, Always, TestOutputList (
2145       [["touch"; "/new"];
2146        ["strings"; "/new"]], [])],
2147    "print the printable strings in a file",
2148    "\
2149 This runs the L<strings(1)> command on a file and returns
2150 the list of printable strings found.");
2151
2152   ("strings_e", (RStringList "stringsout", [String "encoding"; String "path"]), 95, [ProtocolLimitWarning],
2153    [InitBasicFS, Always, TestOutputList (
2154       [["write_file"; "/new"; "hello\nworld\n"; "0"];
2155        ["strings_e"; "b"; "/new"]], []);
2156     InitBasicFS, Disabled, TestOutputList (
2157       [["write_file"; "/new"; "\000h\000e\000l\000l\000o\000\n\000w\000o\000r\000l\000d\000\n"; "24"];
2158        ["strings_e"; "b"; "/new"]], ["hello"; "world"])],
2159    "print the printable strings in a file",
2160    "\
2161 This is like the C<guestfs_strings> command, but allows you to
2162 specify the encoding.
2163
2164 See the L<strings(1)> manpage for the full list of encodings.
2165
2166 Commonly useful encodings are C<l> (lower case L) which will
2167 show strings inside Windows/x86 files.
2168
2169 The returned strings are transcoded to UTF-8.");
2170
2171   ("hexdump", (RString "dump", [String "path"]), 96, [ProtocolLimitWarning],
2172    [InitBasicFS, Always, TestOutput (
2173       [["write_file"; "/new"; "hello\nworld\n"; "12"];
2174        ["hexdump"; "/new"]], "00000000  68 65 6c 6c 6f 0a 77 6f  72 6c 64 0a              |hello.world.|\n0000000c\n");
2175     (* Test for RHBZ#501888c2 regression which caused large hexdump
2176      * commands to segfault.
2177      *)
2178     InitBasicFS, Always, TestRun (
2179       [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2180        ["hexdump"; "/100krandom"]])],
2181    "dump a file in hexadecimal",
2182    "\
2183 This runs C<hexdump -C> on the given C<path>.  The result is
2184 the human-readable, canonical hex dump of the file.");
2185
2186   ("zerofree", (RErr, [String "device"]), 97, [],
2187    [InitNone, Always, TestOutput (
2188       [["sfdiskM"; "/dev/sda"; ","];
2189        ["mkfs"; "ext3"; "/dev/sda1"];
2190        ["mount"; "/dev/sda1"; "/"];
2191        ["write_file"; "/new"; "test file"; "0"];
2192        ["umount"; "/dev/sda1"];
2193        ["zerofree"; "/dev/sda1"];
2194        ["mount"; "/dev/sda1"; "/"];
2195        ["cat"; "/new"]], "test file")],
2196    "zero unused inodes and disk blocks on ext2/3 filesystem",
2197    "\
2198 This runs the I<zerofree> program on C<device>.  This program
2199 claims to zero unused inodes and disk blocks on an ext2/3
2200 filesystem, thus making it possible to compress the filesystem
2201 more effectively.
2202
2203 You should B<not> run this program if the filesystem is
2204 mounted.
2205
2206 It is possible that using this program can damage the filesystem
2207 or data on the filesystem.");
2208
2209   ("pvresize", (RErr, [String "device"]), 98, [],
2210    [],
2211    "resize an LVM physical volume",
2212    "\
2213 This resizes (expands or shrinks) an existing LVM physical
2214 volume to match the new size of the underlying device.");
2215
2216   ("sfdisk_N", (RErr, [String "device"; Int "partnum";
2217                        Int "cyls"; Int "heads"; Int "sectors";
2218                        String "line"]), 99, [DangerWillRobinson],
2219    [],
2220    "modify a single partition on a block device",
2221    "\
2222 This runs L<sfdisk(8)> option to modify just the single
2223 partition C<n> (note: C<n> counts from 1).
2224
2225 For other parameters, see C<guestfs_sfdisk>.  You should usually
2226 pass C<0> for the cyls/heads/sectors parameters.");
2227
2228   ("sfdisk_l", (RString "partitions", [String "device"]), 100, [],
2229    [],
2230    "display the partition table",
2231    "\
2232 This displays the partition table on C<device>, in the
2233 human-readable output of the L<sfdisk(8)> command.  It is
2234 not intended to be parsed.");
2235
2236   ("sfdisk_kernel_geometry", (RString "partitions", [String "device"]), 101, [],
2237    [],
2238    "display the kernel geometry",
2239    "\
2240 This displays the kernel's idea of the geometry of C<device>.
2241
2242 The result is in human-readable format, and not designed to
2243 be parsed.");
2244
2245   ("sfdisk_disk_geometry", (RString "partitions", [String "device"]), 102, [],
2246    [],
2247    "display the disk geometry from the partition table",
2248    "\
2249 This displays the disk geometry of C<device> read from the
2250 partition table.  Especially in the case where the underlying
2251 block device has been resized, this can be different from the
2252 kernel's idea of the geometry (see C<guestfs_sfdisk_kernel_geometry>).
2253
2254 The result is in human-readable format, and not designed to
2255 be parsed.");
2256
2257   ("vg_activate_all", (RErr, [Bool "activate"]), 103, [],
2258    [],
2259    "activate or deactivate all volume groups",
2260    "\
2261 This command activates or (if C<activate> is false) deactivates
2262 all logical volumes in all volume groups.
2263 If activated, then they are made known to the
2264 kernel, ie. they appear as C</dev/mapper> devices.  If deactivated,
2265 then those devices disappear.
2266
2267 This command is the same as running C<vgchange -a y|n>");
2268
2269   ("vg_activate", (RErr, [Bool "activate"; StringList "volgroups"]), 104, [],
2270    [],
2271    "activate or deactivate some volume groups",
2272    "\
2273 This command activates or (if C<activate> is false) deactivates
2274 all logical volumes in the listed volume groups C<volgroups>.
2275 If activated, then they are made known to the
2276 kernel, ie. they appear as C</dev/mapper> devices.  If deactivated,
2277 then those devices disappear.
2278
2279 This command is the same as running C<vgchange -a y|n volgroups...>
2280
2281 Note that if C<volgroups> is an empty list then B<all> volume groups
2282 are activated or deactivated.");
2283
2284   ("lvresize", (RErr, [String "device"; Int "mbytes"]), 105, [],
2285    [InitNone, Always, TestOutput (
2286     [["sfdiskM"; "/dev/sda"; ","];
2287      ["pvcreate"; "/dev/sda1"];
2288      ["vgcreate"; "VG"; "/dev/sda1"];
2289      ["lvcreate"; "LV"; "VG"; "10"];
2290      ["mkfs"; "ext2"; "/dev/VG/LV"];
2291      ["mount"; "/dev/VG/LV"; "/"];
2292      ["write_file"; "/new"; "test content"; "0"];
2293      ["umount"; "/"];
2294      ["lvresize"; "/dev/VG/LV"; "20"];
2295      ["e2fsck_f"; "/dev/VG/LV"];
2296      ["resize2fs"; "/dev/VG/LV"];
2297      ["mount"; "/dev/VG/LV"; "/"];
2298      ["cat"; "/new"]], "test content")],
2299    "resize an LVM logical volume",
2300    "\
2301 This resizes (expands or shrinks) an existing LVM logical
2302 volume to C<mbytes>.  When reducing, data in the reduced part
2303 is lost.");
2304
2305   ("resize2fs", (RErr, [String "device"]), 106, [],
2306    [], (* lvresize tests this *)
2307    "resize an ext2/ext3 filesystem",
2308    "\
2309 This resizes an ext2 or ext3 filesystem to match the size of
2310 the underlying device.
2311
2312 I<Note:> It is sometimes required that you run C<guestfs_e2fsck_f>
2313 on the C<device> before calling this command.  For unknown reasons
2314 C<resize2fs> sometimes gives an error about this and sometimes not.
2315 In any case, it is always safe to call C<guestfs_e2fsck_f> before
2316 calling this function.");
2317
2318   ("find", (RStringList "names", [String "directory"]), 107, [],
2319    [InitBasicFS, Always, TestOutputList (
2320       [["find"; "/"]], ["lost+found"]);
2321     InitBasicFS, Always, TestOutputList (
2322       [["touch"; "/a"];
2323        ["mkdir"; "/b"];
2324        ["touch"; "/b/c"];
2325        ["find"; "/"]], ["a"; "b"; "b/c"; "lost+found"]);
2326     InitBasicFS, Always, TestOutputList (
2327       [["mkdir_p"; "/a/b/c"];
2328        ["touch"; "/a/b/c/d"];
2329        ["find"; "/a/b/"]], ["c"; "c/d"])],
2330    "find all files and directories",
2331    "\
2332 This command lists out all files and directories, recursively,
2333 starting at C<directory>.  It is essentially equivalent to
2334 running the shell command C<find directory -print> but some
2335 post-processing happens on the output, described below.
2336
2337 This returns a list of strings I<without any prefix>.  Thus
2338 if the directory structure was:
2339
2340  /tmp/a
2341  /tmp/b
2342  /tmp/c/d
2343
2344 then the returned list from C<guestfs_find> C</tmp> would be
2345 4 elements:
2346
2347  a
2348  b
2349  c
2350  c/d
2351
2352 If C<directory> is not a directory, then this command returns
2353 an error.
2354
2355 The returned list is sorted.");
2356
2357   ("e2fsck_f", (RErr, [String "device"]), 108, [],
2358    [], (* lvresize tests this *)
2359    "check an ext2/ext3 filesystem",
2360    "\
2361 This runs C<e2fsck -p -f device>, ie. runs the ext2/ext3
2362 filesystem checker on C<device>, noninteractively (C<-p>),
2363 even if the filesystem appears to be clean (C<-f>).
2364
2365 This command is only needed because of C<guestfs_resize2fs>
2366 (q.v.).  Normally you should use C<guestfs_fsck>.");
2367
2368   ("sleep", (RErr, [Int "secs"]), 109, [],
2369    [InitNone, Always, TestRun (
2370     [["sleep"; "1"]])],
2371    "sleep for some seconds",
2372    "\
2373 Sleep for C<secs> seconds.");
2374
2375   ("ntfs_3g_probe", (RInt "status", [Bool "rw"; String "device"]), 110, [],
2376    [InitNone, Always, TestOutputInt (
2377       [["sfdiskM"; "/dev/sda"; ","];
2378        ["mkfs"; "ntfs"; "/dev/sda1"];
2379        ["ntfs_3g_probe"; "true"; "/dev/sda1"]], 0);
2380     InitNone, Always, TestOutputInt (
2381       [["sfdiskM"; "/dev/sda"; ","];
2382        ["mkfs"; "ext2"; "/dev/sda1"];
2383        ["ntfs_3g_probe"; "true"; "/dev/sda1"]], 12)],
2384    "probe NTFS volume",
2385    "\
2386 This command runs the L<ntfs-3g.probe(8)> command which probes
2387 an NTFS C<device> for mountability.  (Not all NTFS volumes can
2388 be mounted read-write, and some cannot be mounted at all).
2389
2390 C<rw> is a boolean flag.  Set it to true if you want to test
2391 if the volume can be mounted read-write.  Set it to false if
2392 you want to test if the volume can be mounted read-only.
2393
2394 The return value is an integer which C<0> if the operation
2395 would succeed, or some non-zero value documented in the
2396 L<ntfs-3g.probe(8)> manual page.");
2397
2398   ("sh", (RString "output", [String "command"]), 111, [],
2399    [], (* XXX needs tests *)
2400    "run a command via the shell",
2401    "\
2402 This call runs a command from the guest filesystem via the
2403 guest's C</bin/sh>.
2404
2405 This is like C<guestfs_command>, but passes the command to:
2406
2407  /bin/sh -c \"command\"
2408
2409 Depending on the guest's shell, this usually results in
2410 wildcards being expanded, shell expressions being interpolated
2411 and so on.
2412
2413 All the provisos about C<guestfs_command> apply to this call.");
2414
2415   ("sh_lines", (RStringList "lines", [String "command"]), 112, [],
2416    [], (* XXX needs tests *)
2417    "run a command via the shell returning lines",
2418    "\
2419 This is the same as C<guestfs_sh>, but splits the result
2420 into a list of lines.
2421
2422 See also: C<guestfs_command_lines>");
2423
2424   ("glob_expand", (RStringList "paths", [String "pattern"]), 113, [],
2425    [InitBasicFS, Always, TestOutputList (
2426       [["mkdir_p"; "/a/b/c"];
2427        ["touch"; "/a/b/c/d"];
2428        ["touch"; "/a/b/c/e"];
2429        ["glob_expand"; "/a/b/c/*"]], ["/a/b/c/d"; "/a/b/c/e"]);
2430     InitBasicFS, Always, TestOutputList (
2431       [["mkdir_p"; "/a/b/c"];
2432        ["touch"; "/a/b/c/d"];
2433        ["touch"; "/a/b/c/e"];
2434        ["glob_expand"; "/a/*/c/*"]], ["/a/b/c/d"; "/a/b/c/e"]);
2435     InitBasicFS, Always, TestOutputList (
2436       [["mkdir_p"; "/a/b/c"];
2437        ["touch"; "/a/b/c/d"];
2438        ["touch"; "/a/b/c/e"];
2439        ["glob_expand"; "/a/*/x/*"]], [])],
2440    "expand a wildcard path",
2441    "\
2442 This command searches for all the pathnames matching
2443 C<pattern> according to the wildcard expansion rules
2444 used by the shell.
2445
2446 If no paths match, then this returns an empty list
2447 (note: not an error).
2448
2449 It is just a wrapper around the C L<glob(3)> function
2450 with flags C<GLOB_MARK|GLOB_BRACE>.
2451 See that manual page for more details.");
2452
2453   ("scrub_device", (RErr, [String "device"]), 114, [DangerWillRobinson],
2454    [InitNone, Always, TestRun ( (* use /dev/sdc because it's smaller *)
2455       [["scrub_device"; "/dev/sdc"]])],
2456    "scrub (securely wipe) a device",
2457    "\
2458 This command writes patterns over C<device> to make data retrieval
2459 more difficult.
2460
2461 It is an interface to the L<scrub(1)> program.  See that
2462 manual page for more details.");
2463
2464   ("scrub_file", (RErr, [String "file"]), 115, [],
2465    [InitBasicFS, Always, TestRun (
2466       [["write_file"; "/file"; "content"; "0"];
2467        ["scrub_file"; "/file"]])],
2468    "scrub (securely wipe) a file",
2469    "\
2470 This command writes patterns over a file to make data retrieval
2471 more difficult.
2472
2473 The file is I<removed> after scrubbing.
2474
2475 It is an interface to the L<scrub(1)> program.  See that
2476 manual page for more details.");
2477
2478   ("scrub_freespace", (RErr, [String "dir"]), 116, [],
2479    [], (* XXX needs testing *)
2480    "scrub (securely wipe) free space",
2481    "\
2482 This command creates the directory C<dir> and then fills it
2483 with files until the filesystem is full, and scrubs the files
2484 as for C<guestfs_scrub_file>, and deletes them.
2485 The intention is to scrub any free space on the partition
2486 containing C<dir>.
2487
2488 It is an interface to the L<scrub(1)> program.  See that
2489 manual page for more details.");
2490
2491   ("mkdtemp", (RString "dir", [String "template"]), 117, [],
2492    [InitBasicFS, Always, TestRun (
2493       [["mkdir"; "/tmp"];
2494        ["mkdtemp"; "/tmp/tmpXXXXXX"]])],
2495    "create a temporary directory",
2496    "\
2497 This command creates a temporary directory.  The
2498 C<template> parameter should be a full pathname for the
2499 temporary directory name with the final six characters being
2500 \"XXXXXX\".
2501
2502 For example: \"/tmp/myprogXXXXXX\" or \"/Temp/myprogXXXXXX\",
2503 the second one being suitable for Windows filesystems.
2504
2505 The name of the temporary directory that was created
2506 is returned.
2507
2508 The temporary directory is created with mode 0700
2509 and is owned by root.
2510
2511 The caller is responsible for deleting the temporary
2512 directory and its contents after use.
2513
2514 See also: L<mkdtemp(3)>");
2515
2516   ("wc_l", (RInt "lines", [String "path"]), 118, [],
2517    [InitBasicFS, Always, TestOutputInt (
2518       [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2519        ["wc_l"; "/10klines"]], 10000)],
2520    "count lines in a file",
2521    "\
2522 This command counts the lines in a file, using the
2523 C<wc -l> external command.");
2524
2525   ("wc_w", (RInt "words", [String "path"]), 119, [],
2526    [InitBasicFS, Always, TestOutputInt (
2527       [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2528        ["wc_w"; "/10klines"]], 10000)],
2529    "count words in a file",
2530    "\
2531 This command counts the words in a file, using the
2532 C<wc -w> external command.");
2533
2534   ("wc_c", (RInt "chars", [String "path"]), 120, [],
2535    [InitBasicFS, Always, TestOutputInt (
2536       [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2537        ["wc_c"; "/100kallspaces"]], 102400)],
2538    "count characters in a file",
2539    "\
2540 This command counts the characters in a file, using the
2541 C<wc -c> external command.");
2542
2543   ("head", (RStringList "lines", [String "path"]), 121, [ProtocolLimitWarning],
2544    [InitBasicFS, Always, TestOutputList (
2545       [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2546        ["head"; "/10klines"]], ["0abcdefghijklmnopqrstuvwxyz";"1abcdefghijklmnopqrstuvwxyz";"2abcdefghijklmnopqrstuvwxyz";"3abcdefghijklmnopqrstuvwxyz";"4abcdefghijklmnopqrstuvwxyz";"5abcdefghijklmnopqrstuvwxyz";"6abcdefghijklmnopqrstuvwxyz";"7abcdefghijklmnopqrstuvwxyz";"8abcdefghijklmnopqrstuvwxyz";"9abcdefghijklmnopqrstuvwxyz"])],
2547    "return first 10 lines of a file",
2548    "\
2549 This command returns up to the first 10 lines of a file as
2550 a list of strings.");
2551
2552   ("head_n", (RStringList "lines", [Int "nrlines"; String "path"]), 122, [ProtocolLimitWarning],
2553    [InitBasicFS, Always, TestOutputList (
2554       [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2555        ["head_n"; "3"; "/10klines"]], ["0abcdefghijklmnopqrstuvwxyz";"1abcdefghijklmnopqrstuvwxyz";"2abcdefghijklmnopqrstuvwxyz"]);
2556     InitBasicFS, Always, TestOutputList (
2557       [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2558        ["head_n"; "-9997"; "/10klines"]], ["0abcdefghijklmnopqrstuvwxyz";"1abcdefghijklmnopqrstuvwxyz";"2abcdefghijklmnopqrstuvwxyz"]);
2559     InitBasicFS, Always, TestOutputList (
2560       [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2561        ["head_n"; "0"; "/10klines"]], [])],
2562    "return first N lines of a file",
2563    "\
2564 If the parameter C<nrlines> is a positive number, this returns the first
2565 C<nrlines> lines of the file C<path>.
2566
2567 If the parameter C<nrlines> is a negative number, this returns lines
2568 from the file C<path>, excluding the last C<nrlines> lines.
2569
2570 If the parameter C<nrlines> is zero, this returns an empty list.");
2571
2572   ("tail", (RStringList "lines", [String "path"]), 123, [ProtocolLimitWarning],
2573    [InitBasicFS, Always, TestOutputList (
2574       [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2575        ["tail"; "/10klines"]], ["9990abcdefghijklmnopqrstuvwxyz";"9991abcdefghijklmnopqrstuvwxyz";"9992abcdefghijklmnopqrstuvwxyz";"9993abcdefghijklmnopqrstuvwxyz";"9994abcdefghijklmnopqrstuvwxyz";"9995abcdefghijklmnopqrstuvwxyz";"9996abcdefghijklmnopqrstuvwxyz";"9997abcdefghijklmnopqrstuvwxyz";"9998abcdefghijklmnopqrstuvwxyz";"9999abcdefghijklmnopqrstuvwxyz"])],
2576    "return last 10 lines of a file",
2577    "\
2578 This command returns up to the last 10 lines of a file as
2579 a list of strings.");
2580
2581   ("tail_n", (RStringList "lines", [Int "nrlines"; String "path"]), 124, [ProtocolLimitWarning],
2582    [InitBasicFS, Always, TestOutputList (
2583       [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2584        ["tail_n"; "3"; "/10klines"]], ["9997abcdefghijklmnopqrstuvwxyz";"9998abcdefghijklmnopqrstuvwxyz";"9999abcdefghijklmnopqrstuvwxyz"]);
2585     InitBasicFS, Always, TestOutputList (
2586       [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2587        ["tail_n"; "-9998"; "/10klines"]], ["9997abcdefghijklmnopqrstuvwxyz";"9998abcdefghijklmnopqrstuvwxyz";"9999abcdefghijklmnopqrstuvwxyz"]);
2588     InitBasicFS, Always, TestOutputList (
2589       [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2590        ["tail_n"; "0"; "/10klines"]], [])],
2591    "return last N lines of a file",
2592    "\
2593 If the parameter C<nrlines> is a positive number, this returns the last
2594 C<nrlines> lines of the file C<path>.
2595
2596 If the parameter C<nrlines> is a negative number, this returns lines
2597 from the file C<path>, starting with the C<-nrlines>th line.
2598
2599 If the parameter C<nrlines> is zero, this returns an empty list.");
2600
2601   ("df", (RString "output", []), 125, [],
2602    [], (* XXX Tricky to test because it depends on the exact format
2603         * of the 'df' command and other imponderables.
2604         *)
2605    "report file system disk space usage",
2606    "\
2607 This command runs the C<df> command to report disk space used.
2608
2609 This command is mostly useful for interactive sessions.  It
2610 is I<not> intended that you try to parse the output string.
2611 Use C<statvfs> from programs.");
2612
2613   ("df_h", (RString "output", []), 126, [],
2614    [], (* XXX Tricky to test because it depends on the exact format
2615         * of the 'df' command and other imponderables.
2616         *)
2617    "report file system disk space usage (human readable)",
2618    "\
2619 This command runs the C<df -h> command to report disk space used
2620 in human-readable format.
2621
2622 This command is mostly useful for interactive sessions.  It
2623 is I<not> intended that you try to parse the output string.
2624 Use C<statvfs> from programs.");
2625
2626   ("du", (RInt64 "sizekb", [String "path"]), 127, [],
2627    [InitBasicFS, Always, TestOutputInt (
2628       [["mkdir"; "/p"];
2629        ["du"; "/p"]], 1 (* ie. 1 block, so depends on ext3 blocksize *))],
2630    "estimate file space usage",
2631    "\
2632 This command runs the C<du -s> command to estimate file space
2633 usage for C<path>.
2634
2635 C<path> can be a file or a directory.  If C<path> is a directory
2636 then the estimate includes the contents of the directory and all
2637 subdirectories (recursively).
2638
2639 The result is the estimated size in I<kilobytes>
2640 (ie. units of 1024 bytes).");
2641
2642   ("initrd_list", (RStringList "filenames", [String "path"]), 128, [],
2643    [InitBasicFS, Always, TestOutputList (
2644       [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2645        ["initrd_list"; "/initrd"]], ["empty";"known-1";"known-2";"known-3"])],
2646    "list files in an initrd",
2647    "\
2648 This command lists out files contained in an initrd.
2649
2650 The files are listed without any initial C</> character.  The
2651 files are listed in the order they appear (not necessarily
2652 alphabetical).  Directory names are listed as separate items.
2653
2654 Old Linux kernels (2.4 and earlier) used a compressed ext2
2655 filesystem as initrd.  We I<only> support the newer initramfs
2656 format (compressed cpio files).");
2657
2658   ("mount_loop", (RErr, [String "file"; String "mountpoint"]), 129, [],
2659    [],
2660    "mount a file using the loop device",
2661    "\
2662 This command lets you mount C<file> (a filesystem image
2663 in a file) on a mount point.  It is entirely equivalent to
2664 the command C<mount -o loop file mountpoint>.");
2665
2666   ("mkswap", (RErr, [String "device"]), 130, [],
2667    [InitEmpty, Always, TestRun (
2668       [["sfdiskM"; "/dev/sda"; ","];
2669        ["mkswap"; "/dev/sda1"]])],
2670    "create a swap partition",
2671    "\
2672 Create a swap partition on C<device>.");
2673
2674   ("mkswap_L", (RErr, [String "label"; String "device"]), 131, [],
2675    [InitEmpty, Always, TestRun (
2676       [["sfdiskM"; "/dev/sda"; ","];
2677        ["mkswap_L"; "hello"; "/dev/sda1"]])],
2678    "create a swap partition with a label",
2679    "\
2680 Create a swap partition on C<device> with label C<label>.");
2681
2682   ("mkswap_U", (RErr, [String "uuid"; String "device"]), 132, [],
2683    [InitEmpty, Always, TestRun (
2684       [["sfdiskM"; "/dev/sda"; ","];
2685        ["mkswap_U"; "a3a61220-882b-4f61-89f4-cf24dcc7297d"; "/dev/sda1"]])],
2686    "create a swap partition with an explicit UUID",
2687    "\
2688 Create a swap partition on C<device> with UUID C<uuid>.");
2689
2690   ("mknod", (RErr, [Int "mode"; Int "devmajor"; Int "devminor"; String "path"]), 133, [],
2691    [InitBasicFS, Always, TestOutputStruct (
2692       [["mknod"; "0o10777"; "0"; "0"; "/node"];
2693        (* NB: default umask 022 means 0777 -> 0755 in these tests *)
2694        ["stat"; "/node"]], [CompareWithInt ("mode", 0o10755)]);
2695     InitBasicFS, Always, TestOutputStruct (
2696       [["mknod"; "0o60777"; "66"; "99"; "/node"];
2697        ["stat"; "/node"]], [CompareWithInt ("mode", 0o60755)])],
2698    "make block, character or FIFO devices",
2699    "\
2700 This call creates block or character special devices, or
2701 named pipes (FIFOs).
2702
2703 The C<mode> parameter should be the mode, using the standard
2704 constants.  C<devmajor> and C<devminor> are the
2705 device major and minor numbers, only used when creating block
2706 and character special devices.");
2707
2708   ("mkfifo", (RErr, [Int "mode"; String "path"]), 134, [],
2709    [InitBasicFS, Always, TestOutputStruct (
2710       [["mkfifo"; "0o777"; "/node"];
2711        ["stat"; "/node"]], [CompareWithInt ("mode", 0o10755)])],
2712    "make FIFO (named pipe)",
2713    "\
2714 This call creates a FIFO (named pipe) called C<path> with
2715 mode C<mode>.  It is just a convenient wrapper around
2716 C<guestfs_mknod>.");
2717
2718   ("mknod_b", (RErr, [Int "mode"; Int "devmajor"; Int "devminor"; String "path"]), 135, [],
2719    [InitBasicFS, Always, TestOutputStruct (
2720       [["mknod_b"; "0o777"; "99"; "66"; "/node"];
2721        ["stat"; "/node"]], [CompareWithInt ("mode", 0o60755)])],
2722    "make block device node",
2723    "\
2724 This call creates a block device node called C<path> with
2725 mode C<mode> and device major/minor C<devmajor> and C<devminor>.
2726 It is just a convenient wrapper around C<guestfs_mknod>.");
2727
2728   ("mknod_c", (RErr, [Int "mode"; Int "devmajor"; Int "devminor"; String "path"]), 136, [],
2729    [InitBasicFS, Always, TestOutputStruct (
2730       [["mknod_c"; "0o777"; "99"; "66"; "/node"];
2731        ["stat"; "/node"]], [CompareWithInt ("mode", 0o20755)])],
2732    "make char device node",
2733    "\
2734 This call creates a char device node called C<path> with
2735 mode C<mode> and device major/minor C<devmajor> and C<devminor>.
2736 It is just a convenient wrapper around C<guestfs_mknod>.");
2737
2738   ("umask", (RInt "oldmask", [Int "mask"]), 137, [],
2739    [], (* XXX umask is one of those stateful things that we should
2740         * reset between each test.
2741         *)
2742    "set file mode creation mask (umask)",
2743    "\
2744 This function sets the mask used for creating new files and
2745 device nodes to C<mask & 0777>.
2746
2747 Typical umask values would be C<022> which creates new files
2748 with permissions like \"-rw-r--r--\" or \"-rwxr-xr-x\", and
2749 C<002> which creates new files with permissions like
2750 \"-rw-rw-r--\" or \"-rwxrwxr-x\".
2751
2752 The default umask is C<022>.  This is important because it
2753 means that directories and device nodes will be created with
2754 C<0644> or C<0755> mode even if you specify C<0777>.
2755
2756 See also L<umask(2)>, C<guestfs_mknod>, C<guestfs_mkdir>.
2757
2758 This call returns the previous umask.");
2759
2760   ("readdir", (RStructList ("entries", "dirent"), [String "dir"]), 138, [],
2761    [],
2762    "read directories entries",
2763    "\
2764 This returns the list of directory entries in directory C<dir>.
2765
2766 All entries in the directory are returned, including C<.> and
2767 C<..>.  The entries are I<not> sorted, but returned in the same
2768 order as the underlying filesystem.
2769
2770 This function is primarily intended for use by programs.  To
2771 get a simple list of names, use C<guestfs_ls>.  To get a printable
2772 directory for human consumption, use C<guestfs_ll>.");
2773
2774   ("sfdiskM", (RErr, [String "device"; StringList "lines"]), 139, [DangerWillRobinson],
2775    [],
2776    "create partitions on a block device",
2777    "\
2778 This is a simplified interface to the C<guestfs_sfdisk>
2779 command, where partition sizes are specified in megabytes
2780 only (rounded to the nearest cylinder) and you don't need
2781 to specify the cyls, heads and sectors parameters which
2782 were rarely if ever used anyway.
2783
2784 See also C<guestfs_sfdisk> and the L<sfdisk(8)> manpage.");
2785
2786 ]
2787
2788 let all_functions = non_daemon_functions @ daemon_functions
2789
2790 (* In some places we want the functions to be displayed sorted
2791  * alphabetically, so this is useful:
2792  *)
2793 let all_functions_sorted =
2794   List.sort (fun (n1,_,_,_,_,_,_) (n2,_,_,_,_,_,_) ->
2795                compare n1 n2) all_functions
2796
2797 (* Field types for structures. *)
2798 type field =
2799   | FChar                       (* C 'char' (really, a 7 bit byte). *)
2800   | FString                     (* nul-terminated ASCII string. *)
2801   | FUInt32
2802   | FInt32
2803   | FUInt64
2804   | FInt64
2805   | FBytes                      (* Any int measure that counts bytes. *)
2806   | FUUID                       (* 32 bytes long, NOT nul-terminated. *)
2807   | FOptPercent                 (* [0..100], or -1 meaning "not present". *)
2808
2809 (* Because we generate extra parsing code for LVM command line tools,
2810  * we have to pull out the LVM columns separately here.
2811  *)
2812 let lvm_pv_cols = [
2813   "pv_name", FString;
2814   "pv_uuid", FUUID;
2815   "pv_fmt", FString;
2816   "pv_size", FBytes;
2817   "dev_size", FBytes;
2818   "pv_free", FBytes;
2819   "pv_used", FBytes;
2820   "pv_attr", FString (* XXX *);
2821   "pv_pe_count", FInt64;
2822   "pv_pe_alloc_count", FInt64;
2823   "pv_tags", FString;
2824   "pe_start", FBytes;
2825   "pv_mda_count", FInt64;
2826   "pv_mda_free", FBytes;
2827   (* Not in Fedora 10:
2828      "pv_mda_size", FBytes;
2829   *)
2830 ]
2831 let lvm_vg_cols = [
2832   "vg_name", FString;
2833   "vg_uuid", FUUID;
2834   "vg_fmt", FString;
2835   "vg_attr", FString (* XXX *);
2836   "vg_size", FBytes;
2837   "vg_free", FBytes;
2838   "vg_sysid", FString;
2839   "vg_extent_size", FBytes;
2840   "vg_extent_count", FInt64;
2841   "vg_free_count", FInt64;
2842   "max_lv", FInt64;
2843   "max_pv", FInt64;
2844   "pv_count", FInt64;
2845   "lv_count", FInt64;
2846   "snap_count", FInt64;
2847   "vg_seqno", FInt64;
2848   "vg_tags", FString;
2849   "vg_mda_count", FInt64;
2850   "vg_mda_free", FBytes;
2851   (* Not in Fedora 10:
2852      "vg_mda_size", FBytes;
2853   *)
2854 ]
2855 let lvm_lv_cols = [
2856   "lv_name", FString;
2857   "lv_uuid", FUUID;
2858   "lv_attr", FString (* XXX *);
2859   "lv_major", FInt64;
2860   "lv_minor", FInt64;
2861   "lv_kernel_major", FInt64;
2862   "lv_kernel_minor", FInt64;
2863   "lv_size", FBytes;
2864   "seg_count", FInt64;
2865   "origin", FString;
2866   "snap_percent", FOptPercent;
2867   "copy_percent", FOptPercent;
2868   "move_pv", FString;
2869   "lv_tags", FString;
2870   "mirror_log", FString;
2871   "modules", FString;
2872 ]
2873
2874 (* Names and fields in all structures (in RStruct and RStructList)
2875  * that we support.
2876  *)
2877 let structs = [
2878   (* The old RIntBool return type, only ever used for aug_defnode.  Do
2879    * not use this struct in any new code.
2880    *)
2881   "int_bool", [
2882     "i", FInt32;                (* for historical compatibility *)
2883     "b", FInt32;                (* for historical compatibility *)
2884   ];
2885
2886   (* LVM PVs, VGs, LVs. *)
2887   "lvm_pv", lvm_pv_cols;
2888   "lvm_vg", lvm_vg_cols;
2889   "lvm_lv", lvm_lv_cols;
2890
2891   (* Column names and types from stat structures.
2892    * NB. Can't use things like 'st_atime' because glibc header files
2893    * define some of these as macros.  Ugh.
2894    *)
2895   "stat", [
2896     "dev", FInt64;
2897     "ino", FInt64;
2898     "mode", FInt64;
2899     "nlink", FInt64;
2900     "uid", FInt64;
2901     "gid", FInt64;
2902     "rdev", FInt64;
2903     "size", FInt64;
2904     "blksize", FInt64;
2905     "blocks", FInt64;
2906     "atime", FInt64;
2907     "mtime", FInt64;
2908     "ctime", FInt64;
2909   ];
2910   "statvfs", [
2911     "bsize", FInt64;
2912     "frsize", FInt64;
2913     "blocks", FInt64;
2914     "bfree", FInt64;
2915     "bavail", FInt64;
2916     "files", FInt64;
2917     "ffree", FInt64;
2918     "favail", FInt64;
2919     "fsid", FInt64;
2920     "flag", FInt64;
2921     "namemax", FInt64;
2922   ];
2923
2924   (* Column names in dirent structure. *)
2925   "dirent", [
2926     "ino", FInt64;
2927     (* 'b' 'c' 'd' 'f' (FIFO) 'l' 'r' (regular file) 's' 'u' '?' *)
2928     "ftyp", FChar;
2929     "name", FString;
2930   ];
2931 ] (* end of structs *)
2932
2933 (* Ugh, Java has to be different ..
2934  * These names are also used by the Haskell bindings.
2935  *)
2936 let java_structs = [
2937   "int_bool", "IntBool";
2938   "lvm_pv", "PV";
2939   "lvm_vg", "VG";
2940   "lvm_lv", "LV";
2941   "stat", "Stat";
2942   "statvfs", "StatVFS";
2943   "dirent", "Dirent"
2944 ]
2945
2946 (* Used for testing language bindings. *)
2947 type callt =
2948   | CallString of string
2949   | CallOptString of string option
2950   | CallStringList of string list
2951   | CallInt of int
2952   | CallBool of bool
2953
2954 (* Used to memoize the result of pod2text. *)
2955 let pod2text_memo_filename = "src/.pod2text.data"
2956 let pod2text_memo : ((int * string * string), string list) Hashtbl.t =
2957   try
2958     let chan = open_in pod2text_memo_filename in
2959     let v = input_value chan in
2960     close_in chan;
2961     v
2962   with
2963     _ -> Hashtbl.create 13
2964
2965 (* Useful functions.
2966  * Note we don't want to use any external OCaml libraries which
2967  * makes this a bit harder than it should be.
2968  *)
2969 let failwithf fs = ksprintf failwith fs
2970
2971 let replace_char s c1 c2 =
2972   let s2 = String.copy s in
2973   let r = ref false in
2974   for i = 0 to String.length s2 - 1 do
2975     if String.unsafe_get s2 i = c1 then (
2976       String.unsafe_set s2 i c2;
2977       r := true
2978     )
2979   done;
2980   if not !r then s else s2
2981
2982 let isspace c =
2983   c = ' '
2984   (* || c = '\f' *) || c = '\n' || c = '\r' || c = '\t' (* || c = '\v' *)
2985
2986 let triml ?(test = isspace) str =
2987   let i = ref 0 in
2988   let n = ref (String.length str) in
2989   while !n > 0 && test str.[!i]; do
2990     decr n;
2991     incr i
2992   done;
2993   if !i = 0 then str
2994   else String.sub str !i !n
2995
2996 let trimr ?(test = isspace) str =
2997   let n = ref (String.length str) in
2998   while !n > 0 && test str.[!n-1]; do
2999     decr n
3000   done;
3001   if !n = String.length str then str
3002   else String.sub str 0 !n
3003
3004 let trim ?(test = isspace) str =
3005   trimr ~test (triml ~test str)
3006
3007 let rec find s sub =
3008   let len = String.length s in
3009   let sublen = String.length sub in
3010   let rec loop i =
3011     if i <= len-sublen then (
3012       let rec loop2 j =
3013         if j < sublen then (
3014           if s.[i+j] = sub.[j] then loop2 (j+1)
3015           else -1
3016         ) else
3017           i (* found *)
3018       in
3019       let r = loop2 0 in
3020       if r = -1 then loop (i+1) else r
3021     ) else
3022       -1 (* not found *)
3023   in
3024   loop 0
3025
3026 let rec replace_str s s1 s2 =
3027   let len = String.length s in
3028   let sublen = String.length s1 in
3029   let i = find s s1 in
3030   if i = -1 then s
3031   else (
3032     let s' = String.sub s 0 i in
3033     let s'' = String.sub s (i+sublen) (len-i-sublen) in
3034     s' ^ s2 ^ replace_str s'' s1 s2
3035   )
3036
3037 let rec string_split sep str =
3038   let len = String.length str in
3039   let seplen = String.length sep in
3040   let i = find str sep in
3041   if i = -1 then [str]
3042   else (
3043     let s' = String.sub str 0 i in
3044     let s'' = String.sub str (i+seplen) (len-i-seplen) in
3045     s' :: string_split sep s''
3046   )
3047
3048 let files_equal n1 n2 =
3049   let cmd = sprintf "cmp -s %s %s" (Filename.quote n1) (Filename.quote n2) in
3050   match Sys.command cmd with
3051   | 0 -> true
3052   | 1 -> false
3053   | i -> failwithf "%s: failed with error code %d" cmd i
3054
3055 let rec find_map f = function
3056   | [] -> raise Not_found
3057   | x :: xs ->
3058       match f x with
3059       | Some y -> y
3060       | None -> find_map f xs
3061
3062 let iteri f xs =
3063   let rec loop i = function
3064     | [] -> ()
3065     | x :: xs -> f i x; loop (i+1) xs
3066   in
3067   loop 0 xs
3068
3069 let mapi f xs =
3070   let rec loop i = function
3071     | [] -> []
3072     | x :: xs -> let r = f i x in r :: loop (i+1) xs
3073   in
3074   loop 0 xs
3075
3076 let name_of_argt = function
3077   | String n | OptString n | StringList n | Bool n | Int n
3078   | FileIn n | FileOut n -> n
3079
3080 let java_name_of_struct typ =
3081   try List.assoc typ java_structs
3082   with Not_found ->
3083     failwithf
3084       "java_name_of_struct: no java_structs entry corresponding to %s" typ
3085
3086 let cols_of_struct typ =
3087   try List.assoc typ structs
3088   with Not_found ->
3089     failwithf "cols_of_struct: unknown struct %s" typ
3090
3091 let seq_of_test = function
3092   | TestRun s | TestOutput (s, _) | TestOutputList (s, _)
3093   | TestOutputListOfDevices (s, _)
3094   | TestOutputInt (s, _) | TestOutputTrue s | TestOutputFalse s
3095   | TestOutputLength (s, _) | TestOutputStruct (s, _)
3096   | TestLastFail s -> s
3097
3098 (* Check function names etc. for consistency. *)
3099 let check_functions () =
3100   let contains_uppercase str =
3101     let len = String.length str in
3102     let rec loop i =
3103       if i >= len then false
3104       else (
3105         let c = str.[i] in
3106         if c >= 'A' && c <= 'Z' then true
3107         else loop (i+1)
3108       )
3109     in
3110     loop 0
3111   in
3112
3113   (* Check function names. *)
3114   List.iter (
3115     fun (name, _, _, _, _, _, _) ->
3116       if String.length name >= 7 && String.sub name 0 7 = "guestfs" then
3117         failwithf "function name %s does not need 'guestfs' prefix" name;
3118       if name = "" then
3119         failwithf "function name is empty";
3120       if name.[0] < 'a' || name.[0] > 'z' then
3121         failwithf "function name %s must start with lowercase a-z" name;
3122       if String.contains name '-' then
3123         failwithf "function name %s should not contain '-', use '_' instead."
3124           name
3125   ) all_functions;
3126
3127   (* Check function parameter/return names. *)
3128   List.iter (
3129     fun (name, style, _, _, _, _, _) ->
3130       let check_arg_ret_name n =
3131         if contains_uppercase n then
3132           failwithf "%s param/ret %s should not contain uppercase chars"
3133             name n;
3134         if String.contains n '-' || String.contains n '_' then
3135           failwithf "%s param/ret %s should not contain '-' or '_'"
3136             name n;
3137         if n = "value" then
3138           failwithf "%s has a param/ret called 'value', which causes conflicts in the OCaml bindings, use something like 'val' or a more descriptive name" name;
3139         if n = "int" || n = "char" || n = "short" || n = "long" then
3140           failwithf "%s has a param/ret which conflicts with a C type (eg. 'int', 'char' etc.)" name;
3141         if n = "i" || n = "n" then
3142           failwithf "%s has a param/ret called 'i' or 'n', which will cause some conflicts in the generated code" name;
3143         if n = "argv" || n = "args" then
3144           failwithf "%s has a param/ret called 'argv' or 'args', which will cause some conflicts in the generated code" name
3145       in
3146
3147       (match fst style with
3148        | RErr -> ()
3149        | RInt n | RInt64 n | RBool n | RConstString n | RString n
3150        | RStringList n | RStruct (n, _) | RStructList (n, _)
3151        | RHashtable n ->
3152            check_arg_ret_name n
3153       );
3154       List.iter (fun arg -> check_arg_ret_name (name_of_argt arg)) (snd style)
3155   ) all_functions;
3156
3157   (* Check short descriptions. *)
3158   List.iter (
3159     fun (name, _, _, _, _, shortdesc, _) ->
3160       if shortdesc.[0] <> Char.lowercase shortdesc.[0] then
3161         failwithf "short description of %s should begin with lowercase." name;
3162       let c = shortdesc.[String.length shortdesc-1] in
3163       if c = '\n' || c = '.' then
3164         failwithf "short description of %s should not end with . or \\n." name
3165   ) all_functions;
3166
3167   (* Check long dscriptions. *)
3168   List.iter (
3169     fun (name, _, _, _, _, _, longdesc) ->
3170       if longdesc.[String.length longdesc-1] = '\n' then
3171         failwithf "long description of %s should not end with \\n." name
3172   ) all_functions;
3173
3174   (* Check proc_nrs. *)
3175   List.iter (
3176     fun (name, _, proc_nr, _, _, _, _) ->
3177       if proc_nr <= 0 then
3178         failwithf "daemon function %s should have proc_nr > 0" name
3179   ) daemon_functions;
3180
3181   List.iter (
3182     fun (name, _, proc_nr, _, _, _, _) ->
3183       if proc_nr <> -1 then
3184         failwithf "non-daemon function %s should have proc_nr -1" name
3185   ) non_daemon_functions;
3186
3187   let proc_nrs =
3188     List.map (fun (name, _, proc_nr, _, _, _, _) -> name, proc_nr)
3189       daemon_functions in
3190   let proc_nrs =
3191     List.sort (fun (_,nr1) (_,nr2) -> compare nr1 nr2) proc_nrs in
3192   let rec loop = function
3193     | [] -> ()
3194     | [_] -> ()
3195     | (name1,nr1) :: ((name2,nr2) :: _ as rest) when nr1 < nr2 ->
3196         loop rest
3197     | (name1,nr1) :: (name2,nr2) :: _ ->
3198         failwithf "%s and %s have conflicting procedure numbers (%d, %d)"
3199           name1 name2 nr1 nr2
3200   in
3201   loop proc_nrs;
3202
3203   (* Check tests. *)
3204   List.iter (
3205     function
3206       (* Ignore functions that have no tests.  We generate a
3207        * warning when the user does 'make check' instead.
3208        *)
3209     | name, _, _, _, [], _, _ -> ()
3210     | name, _, _, _, tests, _, _ ->
3211         let funcs =
3212           List.map (
3213             fun (_, _, test) ->
3214               match seq_of_test test with
3215               | [] ->
3216                   failwithf "%s has a test containing an empty sequence" name
3217               | cmds -> List.map List.hd cmds
3218           ) tests in
3219         let funcs = List.flatten funcs in
3220
3221         let tested = List.mem name funcs in
3222
3223         if not tested then
3224           failwithf "function %s has tests but does not test itself" name
3225   ) all_functions
3226
3227 (* 'pr' prints to the current output file. *)
3228 let chan = ref stdout
3229 let pr fs = ksprintf (output_string !chan) fs
3230
3231 (* Generate a header block in a number of standard styles. *)
3232 type comment_style = CStyle | HashStyle | OCamlStyle | HaskellStyle
3233 type license = GPLv2 | LGPLv2
3234
3235 let generate_header comment license =
3236   let c = match comment with
3237     | CStyle ->     pr "/* "; " *"
3238     | HashStyle ->  pr "# ";  "#"
3239     | OCamlStyle -> pr "(* "; " *"
3240     | HaskellStyle -> pr "{- "; "  " in
3241   pr "libguestfs generated file\n";
3242   pr "%s WARNING: THIS FILE IS GENERATED BY 'src/generator.ml'.\n" c;
3243   pr "%s ANY CHANGES YOU MAKE TO THIS FILE WILL BE LOST.\n" c;
3244   pr "%s\n" c;
3245   pr "%s Copyright (C) 2009 Red Hat Inc.\n" c;
3246   pr "%s\n" c;
3247   (match license with
3248    | GPLv2 ->
3249        pr "%s This program is free software; you can redistribute it and/or modify\n" c;
3250        pr "%s it under the terms of the GNU General Public License as published by\n" c;
3251        pr "%s the Free Software Foundation; either version 2 of the License, or\n" c;
3252        pr "%s (at your option) any later version.\n" c;
3253        pr "%s\n" c;
3254        pr "%s This program is distributed in the hope that it will be useful,\n" c;
3255        pr "%s but WITHOUT ANY WARRANTY; without even the implied warranty of\n" c;
3256        pr "%s MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n" c;
3257        pr "%s GNU General Public License for more details.\n" c;
3258        pr "%s\n" c;
3259        pr "%s You should have received a copy of the GNU General Public License along\n" c;
3260        pr "%s with this program; if not, write to the Free Software Foundation, Inc.,\n" c;
3261        pr "%s 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\n" c;
3262
3263    | LGPLv2 ->
3264        pr "%s This library is free software; you can redistribute it and/or\n" c;
3265        pr "%s modify it under the terms of the GNU Lesser General Public\n" c;
3266        pr "%s License as published by the Free Software Foundation; either\n" c;
3267        pr "%s version 2 of the License, or (at your option) any later version.\n" c;
3268        pr "%s\n" c;
3269        pr "%s This library is distributed in the hope that it will be useful,\n" c;
3270        pr "%s but WITHOUT ANY WARRANTY; without even the implied warranty of\n" c;
3271        pr "%s MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n" c;
3272        pr "%s Lesser General Public License for more details.\n" c;
3273        pr "%s\n" c;
3274        pr "%s You should have received a copy of the GNU Lesser General Public\n" c;
3275        pr "%s License along with this library; if not, write to the Free Software\n" c;
3276        pr "%s Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n" c;
3277   );
3278   (match comment with
3279    | CStyle -> pr " */\n"
3280    | HashStyle -> ()
3281    | OCamlStyle -> pr " *)\n"
3282    | HaskellStyle -> pr "-}\n"
3283   );
3284   pr "\n"
3285
3286 (* Start of main code generation functions below this line. *)
3287
3288 (* Generate the pod documentation for the C API. *)
3289 let rec generate_actions_pod () =
3290   List.iter (
3291     fun (shortname, style, _, flags, _, _, longdesc) ->
3292       if not (List.mem NotInDocs flags) then (
3293         let name = "guestfs_" ^ shortname in
3294         pr "=head2 %s\n\n" name;
3295         pr " ";
3296         generate_prototype ~extern:false ~handle:"handle" name style;
3297         pr "\n\n";
3298         pr "%s\n\n" longdesc;
3299         (match fst style with
3300          | RErr ->
3301              pr "This function returns 0 on success or -1 on error.\n\n"
3302          | RInt _ ->
3303              pr "On error this function returns -1.\n\n"
3304          | RInt64 _ ->
3305              pr "On error this function returns -1.\n\n"
3306          | RBool _ ->
3307              pr "This function returns a C truth value on success or -1 on error.\n\n"
3308          | RConstString _ ->
3309              pr "This function returns a string, or NULL on error.
3310 The string is owned by the guest handle and must I<not> be freed.\n\n"
3311          | RString _ ->
3312              pr "This function returns a string, or NULL on error.
3313 I<The caller must free the returned string after use>.\n\n"
3314          | RStringList _ ->
3315              pr "This function returns a NULL-terminated array of strings
3316 (like L<environ(3)>), or NULL if there was an error.
3317 I<The caller must free the strings and the array after use>.\n\n"
3318          | RStruct (_, typ) ->
3319              pr "This function returns a C<struct guestfs_%s *>,
3320 or NULL if there was an error.
3321 I<The caller must call C<guestfs_free_%s> after use>.\n\n" typ typ
3322          | RStructList (_, typ) ->
3323              pr "This function returns a C<struct guestfs_%s_list *>
3324 (see E<lt>guestfs-structs.hE<gt>),
3325 or NULL if there was an error.
3326 I<The caller must call C<guestfs_free_%s_list> after use>.\n\n" typ typ
3327          | RHashtable _ ->
3328              pr "This function returns a NULL-terminated array of
3329 strings, or NULL if there was an error.
3330 The array of strings will always have length C<2n+1>, where
3331 C<n> keys and values alternate, followed by the trailing NULL entry.
3332 I<The caller must free the strings and the array after use>.\n\n"
3333         );
3334         if List.mem ProtocolLimitWarning flags then
3335           pr "%s\n\n" protocol_limit_warning;
3336         if List.mem DangerWillRobinson flags then
3337           pr "%s\n\n" danger_will_robinson
3338       )
3339   ) all_functions_sorted
3340
3341 and generate_structs_pod () =
3342   (* Structs documentation. *)
3343   List.iter (
3344     fun (typ, cols) ->
3345       pr "=head2 guestfs_%s\n" typ;
3346       pr "\n";
3347       pr " struct guestfs_%s {\n" typ;
3348       List.iter (
3349         function
3350         | name, FChar -> pr "   char %s;\n" name
3351         | name, FUInt32 -> pr "   uint32_t %s;\n" name
3352         | name, FInt32 -> pr "   int32_t %s;\n" name
3353         | name, (FUInt64|FBytes) -> pr "   uint64_t %s;\n" name
3354         | name, FInt64 -> pr "   int64_t %s;\n" name
3355         | name, FString -> pr "   char *%s;\n" name
3356         | name, FUUID ->
3357             pr "   /* The next field is NOT nul-terminated, be careful when printing it: */\n";
3358             pr "   char %s[32];\n" name
3359         | name, FOptPercent ->
3360             pr "   /* The next field is [0..100] or -1 meaning 'not present': */\n";
3361             pr "   float %s;\n" name
3362       ) cols;
3363       pr " };\n";
3364       pr " \n";
3365       pr " struct guestfs_%s_list {\n" typ;
3366       pr "   uint32_t len; /* Number of elements in list. */\n";
3367       pr "   struct guestfs_%s *val; /* Elements. */\n" typ;
3368       pr " };\n";
3369       pr " \n";
3370       pr " void guestfs_free_%s (struct guestfs_free_%s *);\n" typ typ;
3371       pr " void guestfs_free_%s_list (struct guestfs_free_%s_list *);\n"
3372         typ typ;
3373       pr "\n"
3374   ) structs
3375
3376 (* Generate the protocol (XDR) file, 'guestfs_protocol.x' and
3377  * indirectly 'guestfs_protocol.h' and 'guestfs_protocol.c'.
3378  *
3379  * We have to use an underscore instead of a dash because otherwise
3380  * rpcgen generates incorrect code.
3381  *
3382  * This header is NOT exported to clients, but see also generate_structs_h.
3383  *)
3384 and generate_xdr () =
3385   generate_header CStyle LGPLv2;
3386
3387   (* This has to be defined to get around a limitation in Sun's rpcgen. *)
3388   pr "typedef string str<>;\n";
3389   pr "\n";
3390
3391   (* Internal structures. *)
3392   List.iter (
3393     function
3394     | typ, cols ->
3395         pr "struct guestfs_int_%s {\n" typ;
3396         List.iter (function
3397                    | name, FChar -> pr "  char %s;\n" name
3398                    | name, FString -> pr "  string %s<>;\n" name
3399                    | name, FUUID -> pr "  opaque %s[32];\n" name
3400                    | name, (FInt32|FUInt32) -> pr "  int %s;\n" name
3401                    | name, (FInt64|FUInt64|FBytes) -> pr "  hyper %s;\n" name
3402                    | name, FOptPercent -> pr "  float %s;\n" name
3403                   ) cols;
3404         pr "};\n";
3405         pr "\n";
3406         pr "typedef struct guestfs_int_%s guestfs_int_%s_list<>;\n" typ typ;
3407         pr "\n";
3408   ) structs;
3409
3410   List.iter (
3411     fun (shortname, style, _, _, _, _, _) ->
3412       let name = "guestfs_" ^ shortname in
3413
3414       (match snd style with
3415        | [] -> ()
3416        | args ->
3417            pr "struct %s_args {\n" name;
3418            List.iter (
3419              function
3420              | String n -> pr "  string %s<>;\n" n
3421              | OptString n -> pr "  str *%s;\n" n
3422              | StringList n -> pr "  str %s<>;\n" n
3423              | Bool n -> pr "  bool %s;\n" n
3424              | Int n -> pr "  int %s;\n" n
3425              | FileIn _ | FileOut _ -> ()
3426            ) args;
3427            pr "};\n\n"
3428       );
3429       (match fst style with
3430        | RErr -> ()
3431        | RInt n ->
3432            pr "struct %s_ret {\n" name;
3433            pr "  int %s;\n" n;
3434            pr "};\n\n"
3435        | RInt64 n ->
3436            pr "struct %s_ret {\n" name;
3437            pr "  hyper %s;\n" n;
3438            pr "};\n\n"
3439        | RBool n ->
3440            pr "struct %s_ret {\n" name;
3441            pr "  bool %s;\n" n;
3442            pr "};\n\n"
3443        | RConstString _ ->
3444            failwithf "RConstString cannot be returned from a daemon function"
3445        | RString n ->
3446            pr "struct %s_ret {\n" name;
3447            pr "  string %s<>;\n" n;
3448            pr "};\n\n"
3449        | RStringList n ->
3450            pr "struct %s_ret {\n" name;
3451            pr "  str %s<>;\n" n;
3452            pr "};\n\n"
3453        | RStruct (n, typ) ->
3454            pr "struct %s_ret {\n" name;
3455            pr "  guestfs_int_%s %s;\n" typ n;
3456            pr "};\n\n"
3457        | RStructList (n, typ) ->
3458            pr "struct %s_ret {\n" name;
3459            pr "  guestfs_int_%s_list %s;\n" typ n;
3460            pr "};\n\n"
3461        | RHashtable n ->
3462            pr "struct %s_ret {\n" name;
3463            pr "  str %s<>;\n" n;
3464            pr "};\n\n"
3465       );
3466   ) daemon_functions;
3467
3468   (* Table of procedure numbers. *)
3469   pr "enum guestfs_procedure {\n";
3470   List.iter (
3471     fun (shortname, _, proc_nr, _, _, _, _) ->
3472       pr "  GUESTFS_PROC_%s = %d,\n" (String.uppercase shortname) proc_nr
3473   ) daemon_functions;
3474   pr "  GUESTFS_PROC_NR_PROCS\n";
3475   pr "};\n";
3476   pr "\n";
3477
3478   (* Having to choose a maximum message size is annoying for several
3479    * reasons (it limits what we can do in the API), but it (a) makes
3480    * the protocol a lot simpler, and (b) provides a bound on the size
3481    * of the daemon which operates in limited memory space.  For large
3482    * file transfers you should use FTP.
3483    *)
3484   pr "const GUESTFS_MESSAGE_MAX = %d;\n" (4 * 1024 * 1024);
3485   pr "\n";
3486
3487   (* Message header, etc. *)
3488   pr "\
3489 /* The communication protocol is now documented in the guestfs(3)
3490  * manpage.
3491  */
3492
3493 const GUESTFS_PROGRAM = 0x2000F5F5;
3494 const GUESTFS_PROTOCOL_VERSION = 1;
3495
3496 /* These constants must be larger than any possible message length. */
3497 const GUESTFS_LAUNCH_FLAG = 0xf5f55ff5;
3498 const GUESTFS_CANCEL_FLAG = 0xffffeeee;
3499
3500 enum guestfs_message_direction {
3501   GUESTFS_DIRECTION_CALL = 0,        /* client -> daemon */
3502   GUESTFS_DIRECTION_REPLY = 1        /* daemon -> client */
3503 };
3504
3505 enum guestfs_message_status {
3506   GUESTFS_STATUS_OK = 0,
3507   GUESTFS_STATUS_ERROR = 1
3508 };
3509
3510 const GUESTFS_ERROR_LEN = 256;
3511
3512 struct guestfs_message_error {
3513   string error_message<GUESTFS_ERROR_LEN>;
3514 };
3515
3516 struct guestfs_message_header {
3517   unsigned prog;                     /* GUESTFS_PROGRAM */
3518   unsigned vers;                     /* GUESTFS_PROTOCOL_VERSION */
3519   guestfs_procedure proc;            /* GUESTFS_PROC_x */
3520   guestfs_message_direction direction;
3521   unsigned serial;                   /* message serial number */
3522   guestfs_message_status status;
3523 };
3524
3525 const GUESTFS_MAX_CHUNK_SIZE = 8192;
3526
3527 struct guestfs_chunk {
3528   int cancel;                        /* if non-zero, transfer is cancelled */
3529   /* data size is 0 bytes if the transfer has finished successfully */
3530   opaque data<GUESTFS_MAX_CHUNK_SIZE>;
3531 };
3532 "
3533
3534 (* Generate the guestfs-structs.h file. *)
3535 and generate_structs_h () =
3536   generate_header CStyle LGPLv2;
3537
3538   (* This is a public exported header file containing various
3539    * structures.  The structures are carefully written to have
3540    * exactly the same in-memory format as the XDR structures that
3541    * we use on the wire to the daemon.  The reason for creating
3542    * copies of these structures here is just so we don't have to
3543    * export the whole of guestfs_protocol.h (which includes much
3544    * unrelated and XDR-dependent stuff that we don't want to be
3545    * public, or required by clients).
3546    *
3547    * To reiterate, we will pass these structures to and from the
3548    * client with a simple assignment or memcpy, so the format
3549    * must be identical to what rpcgen / the RFC defines.
3550    *)
3551
3552   (* Public structures. *)
3553   List.iter (
3554     fun (typ, cols) ->
3555       pr "struct guestfs_%s {\n" typ;
3556       List.iter (
3557         function
3558         | name, FChar -> pr "  char %s;\n" name
3559         | name, FString -> pr "  char *%s;\n" name
3560         | name, FUUID -> pr "  char %s[32]; /* this is NOT nul-terminated, be careful when printing */\n" name
3561         | name, FUInt32 -> pr "  uint32_t %s;\n" name
3562         | name, FInt32 -> pr "  int32_t %s;\n" name
3563         | name, (FUInt64|FBytes) -> pr "  uint64_t %s;\n" name
3564         | name, FInt64 -> pr "  int64_t %s;\n" name
3565         | name, FOptPercent -> pr "  float %s; /* [0..100] or -1 */\n" name
3566       ) cols;
3567       pr "};\n";
3568       pr "\n";
3569       pr "struct guestfs_%s_list {\n" typ;
3570       pr "  uint32_t len;\n";
3571       pr "  struct guestfs_%s *val;\n" typ;
3572       pr "};\n";
3573       pr "\n";
3574       pr "extern void guestfs_free_%s (struct guestfs_%s *);\n" typ typ;
3575       pr "extern void guestfs_free_%s_list (struct guestfs_%s_list *);\n" typ typ;
3576       pr "\n"
3577   ) structs
3578
3579 (* Generate the guestfs-actions.h file. *)
3580 and generate_actions_h () =
3581   generate_header CStyle LGPLv2;
3582   List.iter (
3583     fun (shortname, style, _, _, _, _, _) ->
3584       let name = "guestfs_" ^ shortname in
3585       generate_prototype ~single_line:true ~newline:true ~handle:"handle"
3586         name style
3587   ) all_functions
3588
3589 (* Generate the client-side dispatch stubs. *)
3590 and generate_client_actions () =
3591   generate_header CStyle LGPLv2;
3592
3593   pr "\
3594 #include <stdio.h>
3595 #include <stdlib.h>
3596
3597 #include \"guestfs.h\"
3598 #include \"guestfs_protocol.h\"
3599
3600 #define error guestfs_error
3601 #define perrorf guestfs_perrorf
3602 #define safe_malloc guestfs_safe_malloc
3603 #define safe_realloc guestfs_safe_realloc
3604 #define safe_strdup guestfs_safe_strdup
3605 #define safe_memdup guestfs_safe_memdup
3606
3607 /* Check the return message from a call for validity. */
3608 static int
3609 check_reply_header (guestfs_h *g,
3610                     const struct guestfs_message_header *hdr,
3611                     int proc_nr, int serial)
3612 {
3613   if (hdr->prog != GUESTFS_PROGRAM) {
3614     error (g, \"wrong program (%%d/%%d)\", hdr->prog, GUESTFS_PROGRAM);
3615     return -1;
3616   }
3617   if (hdr->vers != GUESTFS_PROTOCOL_VERSION) {
3618     error (g, \"wrong protocol version (%%d/%%d)\",
3619            hdr->vers, GUESTFS_PROTOCOL_VERSION);
3620     return -1;
3621   }
3622   if (hdr->direction != GUESTFS_DIRECTION_REPLY) {
3623     error (g, \"unexpected message direction (%%d/%%d)\",
3624            hdr->direction, GUESTFS_DIRECTION_REPLY);
3625     return -1;
3626   }
3627   if (hdr->proc != proc_nr) {
3628     error (g, \"unexpected procedure number (%%d/%%d)\", hdr->proc, proc_nr);
3629     return -1;
3630   }
3631   if (hdr->serial != serial) {
3632     error (g, \"unexpected serial (%%d/%%d)\", hdr->serial, serial);
3633     return -1;
3634   }
3635
3636   return 0;
3637 }
3638
3639 /* Check we are in the right state to run a high-level action. */
3640 static int
3641 check_state (guestfs_h *g, const char *caller)
3642 {
3643   if (!guestfs_is_ready (g)) {
3644     if (guestfs_is_config (g))
3645       error (g, \"%%s: call launch() before using this function\",
3646         caller);
3647     else if (guestfs_is_launching (g))
3648       error (g, \"%%s: call wait_ready() before using this function\",
3649         caller);
3650     else
3651       error (g, \"%%s called from the wrong state, %%d != READY\",
3652         caller, guestfs_get_state (g));
3653     return -1;
3654   }
3655   return 0;
3656 }
3657
3658 ";
3659
3660   (* Client-side stubs for each function. *)
3661   List.iter (
3662     fun (shortname, style, _, _, _, _, _) ->
3663       let name = "guestfs_" ^ shortname in
3664
3665       (* Generate the context struct which stores the high-level
3666        * state between callback functions.
3667        *)
3668       pr "struct %s_ctx {\n" shortname;
3669       pr "  /* This flag is set by the callbacks, so we know we've done\n";
3670       pr "   * the callbacks as expected, and in the right sequence.\n";
3671       pr "   * 0 = not called, 1 = reply_cb called.\n";
3672       pr "   */\n";
3673       pr "  int cb_sequence;\n";
3674       pr "  struct guestfs_message_header hdr;\n";
3675       pr "  struct guestfs_message_error err;\n";
3676       (match fst style with
3677        | RErr -> ()
3678        | RConstString _ ->
3679            failwithf "RConstString cannot be returned from a daemon function"
3680        | RInt _ | RInt64 _
3681        | RBool _ | RString _ | RStringList _
3682        | RStruct _ | RStructList _
3683        | RHashtable _ ->
3684            pr "  struct %s_ret ret;\n" name
3685       );
3686       pr "};\n";
3687       pr "\n";
3688
3689       (* Generate the reply callback function. *)
3690       pr "static void %s_reply_cb (guestfs_h *g, void *data, XDR *xdr)\n" shortname;
3691       pr "{\n";
3692       pr "  guestfs_main_loop *ml = guestfs_get_main_loop (g);\n";
3693       pr "  struct %s_ctx *ctx = (struct %s_ctx *) data;\n" shortname shortname;
3694       pr "\n";
3695       pr "  /* This should definitely not happen. */\n";
3696       pr "  if (ctx->cb_sequence != 0) {\n";
3697       pr "    ctx->cb_sequence = 9999;\n";
3698       pr "    error (g, \"%%s: internal error: reply callback called twice\", \"%s\");\n" name;
3699       pr "    return;\n";
3700       pr "  }\n";
3701       pr "\n";
3702       pr "  ml->main_loop_quit (ml, g);\n";
3703       pr "\n";
3704       pr "  if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {\n";
3705       pr "    error (g, \"%%s: failed to parse reply header\", \"%s\");\n" name;
3706       pr "    return;\n";
3707       pr "  }\n";
3708       pr "  if (ctx->hdr.status == GUESTFS_STATUS_ERROR) {\n";
3709       pr "    if (!xdr_guestfs_message_error (xdr, &ctx->err)) {\n";
3710       pr "      error (g, \"%%s: failed to parse reply error\", \"%s\");\n"
3711         name;
3712       pr "      return;\n";
3713       pr "    }\n";
3714       pr "    goto done;\n";
3715       pr "  }\n";
3716
3717       (match fst style with
3718        | RErr -> ()
3719        | RConstString _ ->
3720            failwithf "RConstString cannot be returned from a daemon function"
3721        | RInt _ | RInt64 _
3722        | RBool _ | RString _ | RStringList _
3723        | RStruct _ | RStructList _
3724        | RHashtable _ ->
3725            pr "  if (!xdr_%s_ret (xdr, &ctx->ret)) {\n" name;
3726            pr "    error (g, \"%%s: failed to parse reply\", \"%s\");\n" name;
3727            pr "    return;\n";
3728            pr "  }\n";
3729       );
3730
3731       pr " done:\n";
3732       pr "  ctx->cb_sequence = 1;\n";
3733       pr "}\n\n";
3734
3735       (* Generate the action stub. *)
3736       generate_prototype ~extern:false ~semicolon:false ~newline:true
3737         ~handle:"g" name style;
3738
3739       let error_code =
3740         match fst style with
3741         | RErr | RInt _ | RInt64 _ | RBool _ -> "-1"
3742         | RConstString _ ->
3743             failwithf "RConstString cannot be returned from a daemon function"
3744         | RString _ | RStringList _
3745         | RStruct _ | RStructList _
3746         | RHashtable _ ->
3747             "NULL" in
3748
3749       pr "{\n";
3750
3751       (match snd style with
3752        | [] -> ()
3753        | _ -> pr "  struct %s_args args;\n" name
3754       );
3755
3756       pr "  struct %s_ctx ctx;\n" shortname;
3757       pr "  guestfs_main_loop *ml = guestfs_get_main_loop (g);\n";
3758       pr "  int serial;\n";
3759       pr "\n";
3760       pr "  if (check_state (g, \"%s\") == -1) return %s;\n" name error_code;
3761       pr "  guestfs_set_busy (g);\n";
3762       pr "\n";
3763       pr "  memset (&ctx, 0, sizeof ctx);\n";
3764       pr "\n";
3765
3766       (* Send the main header and arguments. *)
3767       (match snd style with
3768        | [] ->
3769            pr "  serial = guestfs__send_sync (g, GUESTFS_PROC_%s, NULL, NULL);\n"
3770              (String.uppercase shortname)
3771        | args ->
3772            List.iter (
3773              function
3774              | String n ->
3775                  pr "  args.%s = (char *) %s;\n" n n
3776              | OptString n ->
3777                  pr "  args.%s = %s ? (char **) &%s : NULL;\n" n n n
3778              | StringList n ->
3779                  pr "  args.%s.%s_val = (char **) %s;\n" n n n;
3780                  pr "  for (args.%s.%s_len = 0; %s[args.%s.%s_len]; args.%s.%s_len++) ;\n" n n n n n n n;
3781              | Bool n ->
3782                  pr "  args.%s = %s;\n" n n
3783              | Int n ->
3784                  pr "  args.%s = %s;\n" n n
3785              | FileIn _ | FileOut _ -> ()
3786            ) args;
3787            pr "  serial = guestfs__send_sync (g, GUESTFS_PROC_%s,\n"
3788              (String.uppercase shortname);
3789            pr "        (xdrproc_t) xdr_%s_args, (char *) &args);\n"
3790              name;
3791       );
3792       pr "  if (serial == -1) {\n";
3793       pr "    guestfs_end_busy (g);\n";
3794       pr "    return %s;\n" error_code;
3795       pr "  }\n";
3796       pr "\n";
3797
3798       (* Send any additional files (FileIn) requested. *)
3799       let need_read_reply_label = ref false in
3800       List.iter (
3801         function
3802         | FileIn n ->
3803             pr "  {\n";
3804             pr "    int r;\n";
3805             pr "\n";
3806             pr "    r = guestfs__send_file_sync (g, %s);\n" n;
3807             pr "    if (r == -1) {\n";
3808             pr "      guestfs_end_busy (g);\n";
3809             pr "      return %s;\n" error_code;
3810             pr "    }\n";
3811             pr "    if (r == -2) /* daemon cancelled */\n";
3812             pr "      goto read_reply;\n";
3813             need_read_reply_label := true;
3814             pr "  }\n";
3815             pr "\n";
3816         | _ -> ()
3817       ) (snd style);
3818
3819       (* Wait for the reply from the remote end. *)
3820       if !need_read_reply_label then pr " read_reply:\n";
3821       pr "  guestfs__switch_to_receiving (g);\n";
3822       pr "  ctx.cb_sequence = 0;\n";
3823       pr "  guestfs_set_reply_callback (g, %s_reply_cb, &ctx);\n" shortname;
3824       pr "  (void) ml->main_loop_run (ml, g);\n";
3825       pr "  guestfs_set_reply_callback (g, NULL, NULL);\n";
3826       pr "  if (ctx.cb_sequence != 1) {\n";
3827       pr "    error (g, \"%%s reply failed, see earlier error messages\", \"%s\");\n" name;
3828       pr "    guestfs_end_busy (g);\n";
3829       pr "    return %s;\n" error_code;
3830       pr "  }\n";
3831       pr "\n";
3832
3833       pr "  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_%s, serial) == -1) {\n"
3834         (String.uppercase shortname);
3835       pr "    guestfs_end_busy (g);\n";
3836       pr "    return %s;\n" error_code;
3837       pr "  }\n";
3838       pr "\n";
3839
3840       pr "  if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {\n";
3841       pr "    error (g, \"%%s\", ctx.err.error_message);\n";
3842       pr "    free (ctx.err.error_message);\n";
3843       pr "    guestfs_end_busy (g);\n";
3844       pr "    return %s;\n" error_code;
3845       pr "  }\n";
3846       pr "\n";
3847
3848       (* Expecting to receive further files (FileOut)? *)
3849       List.iter (
3850         function
3851         | FileOut n ->
3852             pr "  if (guestfs__receive_file_sync (g, %s) == -1) {\n" n;
3853             pr "    guestfs_end_busy (g);\n";
3854             pr "    return %s;\n" error_code;
3855             pr "  }\n";
3856             pr "\n";
3857         | _ -> ()
3858       ) (snd style);
3859
3860       pr "  guestfs_end_busy (g);\n";
3861
3862       (match fst style with
3863        | RErr -> pr "  return 0;\n"
3864        | RInt n | RInt64 n | RBool n ->
3865            pr "  return ctx.ret.%s;\n" n
3866        | RConstString _ ->
3867            failwithf "RConstString cannot be returned from a daemon function"
3868        | RString n ->
3869            pr "  return ctx.ret.%s; /* caller will free */\n" n
3870        | RStringList n | RHashtable n ->
3871            pr "  /* caller will free this, but we need to add a NULL entry */\n";
3872            pr "  ctx.ret.%s.%s_val =\n" n n;
3873            pr "    safe_realloc (g, ctx.ret.%s.%s_val,\n" n n;
3874            pr "                  sizeof (char *) * (ctx.ret.%s.%s_len + 1));\n"
3875              n n;
3876            pr "  ctx.ret.%s.%s_val[ctx.ret.%s.%s_len] = NULL;\n" n n n n;
3877            pr "  return ctx.ret.%s.%s_val;\n" n n
3878        | RStruct (n, _) ->
3879            pr "  /* caller will free this */\n";
3880            pr "  return safe_memdup (g, &ctx.ret.%s, sizeof (ctx.ret.%s));\n" n n
3881        | RStructList (n, _) ->
3882            pr "  /* caller will free this */\n";
3883            pr "  return safe_memdup (g, &ctx.ret.%s, sizeof (ctx.ret.%s));\n" n n
3884       );
3885
3886       pr "}\n\n"
3887   ) daemon_functions;
3888
3889   (* Functions to free structures. *)
3890   pr "/* Structure-freeing functions.  These rely on the fact that the\n";
3891   pr " * structure format is identical to the XDR format.  See note in\n";
3892   pr " * generator.ml.\n";
3893   pr " */\n";
3894   pr "\n";
3895
3896   List.iter (
3897     fun (typ, _) ->
3898       pr "void\n";
3899       pr "guestfs_free_%s (struct guestfs_%s *x)\n" typ typ;
3900       pr "{\n";
3901       pr "  xdr_free ((xdrproc_t) xdr_guestfs_int_%s, (char *) x);\n" typ;
3902       pr "  free (x);\n";
3903       pr "}\n";
3904       pr "\n";
3905
3906       pr "void\n";
3907       pr "guestfs_free_%s_list (struct guestfs_%s_list *x)\n" typ typ;
3908       pr "{\n";
3909       pr "  xdr_free ((xdrproc_t) xdr_guestfs_int_%s_list, (char *) x);\n" typ;
3910       pr "  free (x);\n";
3911       pr "}\n";
3912       pr "\n";
3913
3914   ) structs;
3915
3916 (* Generate daemon/actions.h. *)
3917 and generate_daemon_actions_h () =
3918   generate_header CStyle GPLv2;
3919
3920   pr "#include \"../src/guestfs_protocol.h\"\n";
3921   pr "\n";
3922
3923   List.iter (
3924     fun (name, style, _, _, _, _, _) ->
3925       generate_prototype
3926         ~single_line:true ~newline:true ~in_daemon:true ~prefix:"do_"
3927         name style;
3928   ) daemon_functions
3929
3930 (* Generate the server-side stubs. *)
3931 and generate_daemon_actions () =
3932   generate_header CStyle GPLv2;
3933
3934   pr "#include <config.h>\n";
3935   pr "\n";
3936   pr "#include <stdio.h>\n";
3937   pr "#include <stdlib.h>\n";
3938   pr "#include <string.h>\n";
3939   pr "#include <inttypes.h>\n";
3940   pr "#include <ctype.h>\n";
3941   pr "#include <rpc/types.h>\n";
3942   pr "#include <rpc/xdr.h>\n";
3943   pr "\n";
3944   pr "#include \"daemon.h\"\n";
3945   pr "#include \"../src/guestfs_protocol.h\"\n";
3946   pr "#include \"actions.h\"\n";
3947   pr "\n";
3948
3949   List.iter (
3950     fun (name, style, _, _, _, _, _) ->
3951       (* Generate server-side stubs. *)
3952       pr "static void %s_stub (XDR *xdr_in)\n" name;
3953       pr "{\n";
3954       let error_code =
3955         match fst style with
3956         | RErr | RInt _ -> pr "  int r;\n"; "-1"
3957         | RInt64 _ -> pr "  int64_t r;\n"; "-1"
3958         | RBool _ -> pr "  int r;\n"; "-1"
3959         | RConstString _ ->
3960             failwithf "RConstString cannot be returned from a daemon function"
3961         | RString _ -> pr "  char *r;\n"; "NULL"
3962         | RStringList _ | RHashtable _ -> pr "  char **r;\n"; "NULL"
3963         | RStruct (_, typ) -> pr "  guestfs_int_%s *r;\n" typ; "NULL"
3964         | RStructList (_, typ) -> pr "  guestfs_int_%s_list *r;\n" typ; "NULL" in
3965
3966       (match snd style with
3967        | [] -> ()
3968        | args ->
3969            pr "  struct guestfs_%s_args args;\n" name;
3970            List.iter (
3971              function
3972                (* Note we allow the string to be writable, in order to
3973                 * allow device name translation.  This is safe because
3974                 * we can modify the string (passed from RPC).
3975                 *)
3976              | String n
3977              | OptString n -> pr "  char *%s;\n" n
3978              | StringList n -> pr "  char **%s;\n" n
3979              | Bool n -> pr "  int %s;\n" n
3980              | Int n -> pr "  int %s;\n" n
3981              | FileIn _ | FileOut _ -> ()
3982            ) args
3983       );
3984       pr "\n";
3985
3986       (match snd style with
3987        | [] -> ()
3988        | args ->
3989            pr "  memset (&args, 0, sizeof args);\n";
3990            pr "\n";
3991            pr "  if (!xdr_guestfs_%s_args (xdr_in, &args)) {\n" name;
3992            pr "    reply_with_error (\"%%s: daemon failed to decode procedure arguments\", \"%s\");\n" name;
3993            pr "    return;\n";
3994            pr "  }\n";
3995            List.iter (
3996              function
3997              | String n -> pr "  %s = args.%s;\n" n n
3998              | OptString n -> pr "  %s = args.%s ? *args.%s : NULL;\n" n n n
3999              | StringList n ->
4000                  pr "  %s = realloc (args.%s.%s_val,\n" n n n;
4001                  pr "                sizeof (char *) * (args.%s.%s_len+1));\n" n n;
4002                  pr "  if (%s == NULL) {\n" n;
4003                  pr "    reply_with_perror (\"realloc\");\n";
4004                  pr "    goto done;\n";
4005                  pr "  }\n";
4006                  pr "  %s[args.%s.%s_len] = NULL;\n" n n n;
4007                  pr "  args.%s.%s_val = %s;\n" n n n;
4008              | Bool n -> pr "  %s = args.%s;\n" n n
4009              | Int n -> pr "  %s = args.%s;\n" n n
4010              | FileIn _ | FileOut _ -> ()
4011            ) args;
4012            pr "\n"
4013       );
4014
4015       (* Don't want to call the impl with any FileIn or FileOut
4016        * parameters, since these go "outside" the RPC protocol.
4017        *)
4018       let argsnofile =
4019         List.filter (function FileIn _ | FileOut _ -> false | _ -> true)
4020           (snd style) in
4021       pr "  r = do_%s " name;
4022       generate_call_args argsnofile;
4023       pr ";\n";
4024
4025       pr "  if (r == %s)\n" error_code;
4026       pr "    /* do_%s has already called reply_with_error */\n" name;
4027       pr "    goto done;\n";
4028       pr "\n";
4029
4030       (* If there are any FileOut parameters, then the impl must
4031        * send its own reply.
4032        *)
4033       let no_reply =
4034         List.exists (function FileOut _ -> true | _ -> false) (snd style) in
4035       if no_reply then
4036         pr "  /* do_%s has already sent a reply */\n" name
4037       else (
4038         match fst style with
4039         | RErr -> pr "  reply (NULL, NULL);\n"
4040         | RInt n | RInt64 n | RBool n ->
4041             pr "  struct guestfs_%s_ret ret;\n" name;
4042             pr "  ret.%s = r;\n" n;
4043             pr "  reply ((xdrproc_t) &xdr_guestfs_%s_ret, (char *) &ret);\n"
4044               name
4045         | RConstString _ ->
4046             failwithf "RConstString cannot be returned from a daemon function"
4047         | RString n ->
4048             pr "  struct guestfs_%s_ret ret;\n" name;
4049             pr "  ret.%s = r;\n" n;
4050             pr "  reply ((xdrproc_t) &xdr_guestfs_%s_ret, (char *) &ret);\n"
4051               name;
4052             pr "  free (r);\n"
4053         | RStringList n | RHashtable n ->
4054             pr "  struct guestfs_%s_ret ret;\n" name;
4055             pr "  ret.%s.%s_len = count_strings (r);\n" n n;
4056             pr "  ret.%s.%s_val = r;\n" n n;
4057             pr "  reply ((xdrproc_t) &xdr_guestfs_%s_ret, (char *) &ret);\n"
4058               name;
4059             pr "  free_strings (r);\n"
4060         | RStruct (n, _) ->
4061             pr "  struct guestfs_%s_ret ret;\n" name;
4062             pr "  ret.%s = *r;\n" n;
4063             pr "  reply ((xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret);\n"
4064               name;
4065             pr "  xdr_free ((xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret);\n"
4066               name
4067         | RStructList (n, _) ->
4068             pr "  struct guestfs_%s_ret ret;\n" name;
4069             pr "  ret.%s = *r;\n" n;
4070             pr "  reply ((xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret);\n"
4071               name;
4072             pr "  xdr_free ((xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret);\n"
4073               name
4074       );
4075
4076       (* Free the args. *)
4077       (match snd style with
4078        | [] ->
4079            pr "done: ;\n";
4080        | _ ->
4081            pr "done:\n";
4082            pr "  xdr_free ((xdrproc_t) xdr_guestfs_%s_args, (char *) &args);\n"
4083              name
4084       );
4085
4086       pr "}\n\n";
4087   ) daemon_functions;
4088
4089   (* Dispatch function. *)
4090   pr "void dispatch_incoming_message (XDR *xdr_in)\n";
4091   pr "{\n";
4092   pr "  switch (proc_nr) {\n";
4093
4094   List.iter (
4095     fun (name, style, _, _, _, _, _) ->
4096       pr "    case GUESTFS_PROC_%s:\n" (String.uppercase name);
4097       pr "      %s_stub (xdr_in);\n" name;
4098       pr "      break;\n"
4099   ) daemon_functions;
4100
4101   pr "    default:\n";
4102   pr "      reply_with_error (\"dispatch_incoming_message: unknown procedure number %%d, set LIBGUESTFS_PATH to point to the matching libguestfs appliance directory\", proc_nr);\n";
4103   pr "  }\n";
4104   pr "}\n";
4105   pr "\n";
4106
4107   (* LVM columns and tokenization functions. *)
4108   (* XXX This generates crap code.  We should rethink how we
4109    * do this parsing.
4110    *)
4111   List.iter (
4112     function
4113     | typ, cols ->
4114         pr "static const char *lvm_%s_cols = \"%s\";\n"
4115           typ (String.concat "," (List.map fst cols));
4116         pr "\n";
4117
4118         pr "static int lvm_tokenize_%s (char *str, guestfs_int_lvm_%s *r)\n" typ typ;
4119         pr "{\n";
4120         pr "  char *tok, *p, *next;\n";
4121         pr "  int i, j;\n";
4122         pr "\n";
4123         (*
4124           pr "  fprintf (stderr, \"%%s: <<%%s>>\\n\", __func__, str);\n";
4125           pr "\n";
4126         *)
4127         pr "  if (!str) {\n";
4128         pr "    fprintf (stderr, \"%%s: failed: passed a NULL string\\n\", __func__);\n";
4129         pr "    return -1;\n";
4130         pr "  }\n";
4131         pr "  if (!*str || isspace (*str)) {\n";
4132         pr "    fprintf (stderr, \"%%s: failed: passed a empty string or one beginning with whitespace\\n\", __func__);\n";
4133         pr "    return -1;\n";
4134         pr "  }\n";
4135         pr "  tok = str;\n";
4136         List.iter (
4137           fun (name, coltype) ->
4138             pr "  if (!tok) {\n";
4139             pr "    fprintf (stderr, \"%%s: failed: string finished early, around token %%s\\n\", __func__, \"%s\");\n" name;
4140             pr "    return -1;\n";
4141             pr "  }\n";
4142             pr "  p = strchrnul (tok, ',');\n";
4143             pr "  if (*p) next = p+1; else next = NULL;\n";
4144             pr "  *p = '\\0';\n";
4145             (match coltype with
4146              | FString ->
4147                  pr "  r->%s = strdup (tok);\n" name;
4148                  pr "  if (r->%s == NULL) {\n" name;
4149                  pr "    perror (\"strdup\");\n";
4150                  pr "    return -1;\n";
4151                  pr "  }\n"
4152              | FUUID ->
4153                  pr "  for (i = j = 0; i < 32; ++j) {\n";
4154                  pr "    if (tok[j] == '\\0') {\n";
4155                  pr "      fprintf (stderr, \"%%s: failed to parse UUID from '%%s'\\n\", __func__, tok);\n";
4156                  pr "      return -1;\n";
4157                  pr "    } else if (tok[j] != '-')\n";
4158                  pr "      r->%s[i++] = tok[j];\n" name;
4159                  pr "  }\n";
4160              | FBytes ->
4161                  pr "  if (sscanf (tok, \"%%\"SCNu64, &r->%s) != 1) {\n" name;
4162                  pr "    fprintf (stderr, \"%%s: failed to parse size '%%s' from token %%s\\n\", __func__, tok, \"%s\");\n" name;
4163                  pr "    return -1;\n";
4164                  pr "  }\n";
4165              | FInt64 ->
4166                  pr "  if (sscanf (tok, \"%%\"SCNi64, &r->%s) != 1) {\n" name;
4167                  pr "    fprintf (stderr, \"%%s: failed to parse int '%%s' from token %%s\\n\", __func__, tok, \"%s\");\n" name;
4168                  pr "    return -1;\n";
4169                  pr "  }\n";
4170              | FOptPercent ->
4171                  pr "  if (tok[0] == '\\0')\n";
4172                  pr "    r->%s = -1;\n" name;
4173                  pr "  else if (sscanf (tok, \"%%f\", &r->%s) != 1) {\n" name;
4174                  pr "    fprintf (stderr, \"%%s: failed to parse float '%%s' from token %%s\\n\", __func__, tok, \"%s\");\n" name;
4175                  pr "    return -1;\n";
4176                  pr "  }\n";
4177              | FInt32 | FUInt32 | FUInt64 | FChar ->
4178                  assert false (* can never be an LVM column *)
4179             );
4180             pr "  tok = next;\n";
4181         ) cols;
4182
4183         pr "  if (tok != NULL) {\n";
4184         pr "    fprintf (stderr, \"%%s: failed: extra tokens at end of string\\n\", __func__);\n";
4185         pr "    return -1;\n";
4186         pr "  }\n";
4187         pr "  return 0;\n";
4188         pr "}\n";
4189         pr "\n";
4190
4191         pr "guestfs_int_lvm_%s_list *\n" typ;
4192         pr "parse_command_line_%ss (void)\n" typ;
4193         pr "{\n";
4194         pr "  char *out, *err;\n";
4195         pr "  char *p, *pend;\n";
4196         pr "  int r, i;\n";
4197         pr "  guestfs_int_lvm_%s_list *ret;\n" typ;
4198         pr "  void *newp;\n";
4199         pr "\n";
4200         pr "  ret = malloc (sizeof *ret);\n";
4201         pr "  if (!ret) {\n";
4202         pr "    reply_with_perror (\"malloc\");\n";
4203         pr "    return NULL;\n";
4204         pr "  }\n";
4205         pr "\n";
4206         pr "  ret->guestfs_int_lvm_%s_list_len = 0;\n" typ;
4207         pr "  ret->guestfs_int_lvm_%s_list_val = NULL;\n" typ;
4208         pr "\n";
4209         pr "  r = command (&out, &err,\n";
4210         pr "           \"/sbin/lvm\", \"%ss\",\n" typ;
4211         pr "           \"-o\", lvm_%s_cols, \"--unbuffered\", \"--noheadings\",\n" typ;
4212         pr "           \"--nosuffix\", \"--separator\", \",\", \"--units\", \"b\", NULL);\n";
4213         pr "  if (r == -1) {\n";
4214         pr "    reply_with_error (\"%%s\", err);\n";
4215         pr "    free (out);\n";
4216         pr "    free (err);\n";
4217         pr "    free (ret);\n";
4218         pr "    return NULL;\n";
4219         pr "  }\n";
4220         pr "\n";
4221         pr "  free (err);\n";
4222         pr "\n";
4223         pr "  /* Tokenize each line of the output. */\n";
4224         pr "  p = out;\n";
4225         pr "  i = 0;\n";
4226         pr "  while (p) {\n";
4227         pr "    pend = strchr (p, '\\n');       /* Get the next line of output. */\n";
4228         pr "    if (pend) {\n";
4229         pr "      *pend = '\\0';\n";
4230         pr "      pend++;\n";
4231         pr "    }\n";
4232         pr "\n";
4233         pr "    while (*p && isspace (*p))      /* Skip any leading whitespace. */\n";
4234         pr "      p++;\n";
4235         pr "\n";
4236         pr "    if (!*p) {                      /* Empty line?  Skip it. */\n";
4237         pr "      p = pend;\n";
4238         pr "      continue;\n";
4239         pr "    }\n";
4240         pr "\n";
4241         pr "    /* Allocate some space to store this next entry. */\n";
4242         pr "    newp = realloc (ret->guestfs_int_lvm_%s_list_val,\n" typ;
4243         pr "                sizeof (guestfs_int_lvm_%s) * (i+1));\n" typ;
4244         pr "    if (newp == NULL) {\n";
4245         pr "      reply_with_perror (\"realloc\");\n";
4246         pr "      free (ret->guestfs_int_lvm_%s_list_val);\n" typ;
4247         pr "      free (ret);\n";
4248         pr "      free (out);\n";
4249         pr "      return NULL;\n";
4250         pr "    }\n";
4251         pr "    ret->guestfs_int_lvm_%s_list_val = newp;\n" typ;
4252         pr "\n";
4253         pr "    /* Tokenize the next entry. */\n";
4254         pr "    r = lvm_tokenize_%s (p, &ret->guestfs_int_lvm_%s_list_val[i]);\n" typ typ;
4255         pr "    if (r == -1) {\n";
4256         pr "      reply_with_error (\"failed to parse output of '%ss' command\");\n" typ;
4257         pr "      free (ret->guestfs_int_lvm_%s_list_val);\n" typ;
4258         pr "      free (ret);\n";
4259         pr "      free (out);\n";
4260         pr "      return NULL;\n";
4261         pr "    }\n";
4262         pr "\n";
4263         pr "    ++i;\n";
4264         pr "    p = pend;\n";
4265         pr "  }\n";
4266         pr "\n";
4267         pr "  ret->guestfs_int_lvm_%s_list_len = i;\n" typ;
4268         pr "\n";
4269         pr "  free (out);\n";
4270         pr "  return ret;\n";
4271         pr "}\n"
4272
4273   ) ["pv", lvm_pv_cols; "vg", lvm_vg_cols; "lv", lvm_lv_cols]
4274
4275 (* Generate a list of function names, for debugging in the daemon.. *)
4276 and generate_daemon_names () =
4277   generate_header CStyle GPLv2;
4278
4279   pr "#include <config.h>\n";
4280   pr "\n";
4281   pr "#include \"daemon.h\"\n";
4282   pr "\n";
4283
4284   pr "/* This array is indexed by proc_nr.  See guestfs_protocol.x. */\n";
4285   pr "const char *function_names[] = {\n";
4286   List.iter (
4287     fun (name, _, proc_nr, _, _, _, _) -> pr "  [%d] = \"%s\",\n" proc_nr name
4288   ) daemon_functions;
4289   pr "};\n";
4290
4291 (* Generate the tests. *)
4292 and generate_tests () =
4293   generate_header CStyle GPLv2;
4294
4295   pr "\
4296 #include <stdio.h>
4297 #include <stdlib.h>
4298 #include <string.h>
4299 #include <unistd.h>
4300 #include <sys/types.h>
4301 #include <fcntl.h>
4302
4303 #include \"guestfs.h\"
4304
4305 static guestfs_h *g;
4306 static int suppress_error = 0;
4307
4308 static void print_error (guestfs_h *g, void *data, const char *msg)
4309 {
4310   if (!suppress_error)
4311     fprintf (stderr, \"%%s\\n\", msg);
4312 }
4313
4314 static void print_strings (char * const * const argv)
4315 {
4316   int argc;
4317
4318   for (argc = 0; argv[argc] != NULL; ++argc)
4319     printf (\"\\t%%s\\n\", argv[argc]);
4320 }
4321
4322 /*
4323 static void print_table (char * const * const argv)
4324 {
4325   int i;
4326
4327   for (i = 0; argv[i] != NULL; i += 2)
4328     printf (\"%%s: %%s\\n\", argv[i], argv[i+1]);
4329 }
4330 */
4331
4332 static void no_test_warnings (void)
4333 {
4334 ";
4335
4336   List.iter (
4337     function
4338     | name, _, _, _, [], _, _ ->
4339         pr "  fprintf (stderr, \"warning: \\\"guestfs_%s\\\" has no tests\\n\");\n" name
4340     | name, _, _, _, tests, _, _ -> ()
4341   ) all_functions;
4342
4343   pr "}\n";
4344   pr "\n";
4345
4346   (* Generate the actual tests.  Note that we generate the tests
4347    * in reverse order, deliberately, so that (in general) the
4348    * newest tests run first.  This makes it quicker and easier to
4349    * debug them.
4350    *)
4351   let test_names =
4352     List.map (
4353       fun (name, _, _, _, tests, _, _) ->
4354         mapi (generate_one_test name) tests
4355     ) (List.rev all_functions) in
4356   let test_names = List.concat test_names in
4357   let nr_tests = List.length test_names in
4358
4359   pr "\
4360 int main (int argc, char *argv[])
4361 {
4362   char c = 0;
4363   int failed = 0;
4364   const char *filename;
4365   int fd;
4366   int nr_tests, test_num = 0;
4367
4368   setbuf (stdout, NULL);
4369
4370   no_test_warnings ();
4371
4372   g = guestfs_create ();
4373   if (g == NULL) {
4374     printf (\"guestfs_create FAILED\\n\");
4375     exit (1);
4376   }
4377
4378   guestfs_set_error_handler (g, print_error, NULL);
4379
4380   guestfs_set_path (g, \"../appliance\");
4381
4382   filename = \"test1.img\";
4383   fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666);
4384   if (fd == -1) {
4385     perror (filename);
4386     exit (1);
4387   }
4388   if (lseek (fd, %d, SEEK_SET) == -1) {
4389     perror (\"lseek\");
4390     close (fd);
4391     unlink (filename);
4392     exit (1);
4393   }
4394   if (write (fd, &c, 1) == -1) {
4395     perror (\"write\");
4396     close (fd);
4397     unlink (filename);
4398     exit (1);
4399   }
4400   if (close (fd) == -1) {
4401     perror (filename);
4402     unlink (filename);
4403     exit (1);
4404   }
4405   if (guestfs_add_drive (g, filename) == -1) {
4406     printf (\"guestfs_add_drive %%s FAILED\\n\", filename);
4407     exit (1);
4408   }
4409
4410   filename = \"test2.img\";
4411   fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666);
4412   if (fd == -1) {
4413     perror (filename);
4414     exit (1);
4415   }
4416   if (lseek (fd, %d, SEEK_SET) == -1) {
4417     perror (\"lseek\");
4418     close (fd);
4419     unlink (filename);
4420     exit (1);
4421   }
4422   if (write (fd, &c, 1) == -1) {
4423     perror (\"write\");
4424     close (fd);
4425     unlink (filename);
4426     exit (1);
4427   }
4428   if (close (fd) == -1) {
4429     perror (filename);
4430     unlink (filename);
4431     exit (1);
4432   }
4433   if (guestfs_add_drive (g, filename) == -1) {
4434     printf (\"guestfs_add_drive %%s FAILED\\n\", filename);
4435     exit (1);
4436   }
4437
4438   filename = \"test3.img\";
4439   fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666);
4440   if (fd == -1) {
4441     perror (filename);
4442     exit (1);
4443   }
4444   if (lseek (fd, %d, SEEK_SET) == -1) {
4445     perror (\"lseek\");
4446     close (fd);
4447     unlink (filename);
4448     exit (1);
4449   }
4450   if (write (fd, &c, 1) == -1) {
4451     perror (\"write\");
4452     close (fd);
4453     unlink (filename);
4454     exit (1);
4455   }
4456   if (close (fd) == -1) {
4457     perror (filename);
4458     unlink (filename);
4459     exit (1);
4460   }
4461   if (guestfs_add_drive (g, filename) == -1) {
4462     printf (\"guestfs_add_drive %%s FAILED\\n\", filename);
4463     exit (1);
4464   }
4465
4466   if (guestfs_add_drive_ro (g, \"../images/test.sqsh\") == -1) {
4467     printf (\"guestfs_add_drive_ro ../images/test.sqsh FAILED\\n\");
4468     exit (1);
4469   }
4470
4471   if (guestfs_launch (g) == -1) {
4472     printf (\"guestfs_launch FAILED\\n\");
4473     exit (1);
4474   }
4475
4476   /* Set a timeout in case qemu hangs during launch (RHBZ#505329). */
4477   alarm (600);
4478
4479   if (guestfs_wait_ready (g) == -1) {
4480     printf (\"guestfs_wait_ready FAILED\\n\");
4481     exit (1);
4482   }
4483
4484   /* Cancel previous alarm. */
4485   alarm (0);
4486
4487   nr_tests = %d;
4488
4489 " (500 * 1024 * 1024) (50 * 1024 * 1024) (10 * 1024 * 1024) nr_tests;
4490
4491   iteri (
4492     fun i test_name ->
4493       pr "  test_num++;\n";
4494       pr "  printf (\"%%3d/%%3d %s\\n\", test_num, nr_tests);\n" test_name;
4495       pr "  if (%s () == -1) {\n" test_name;
4496       pr "    printf (\"%s FAILED\\n\");\n" test_name;
4497       pr "    failed++;\n";
4498       pr "  }\n";
4499   ) test_names;
4500   pr "\n";
4501
4502   pr "  guestfs_close (g);\n";
4503   pr "  unlink (\"test1.img\");\n";
4504   pr "  unlink (\"test2.img\");\n";
4505   pr "  unlink (\"test3.img\");\n";
4506   pr "\n";
4507
4508   pr "  if (failed > 0) {\n";
4509   pr "    printf (\"***** %%d / %%d tests FAILED *****\\n\", failed, nr_tests);\n";
4510   pr "    exit (1);\n";
4511   pr "  }\n";
4512   pr "\n";
4513
4514   pr "  exit (0);\n";
4515   pr "}\n"
4516
4517 and generate_one_test name i (init, prereq, test) =
4518   let test_name = sprintf "test_%s_%d" name i in
4519
4520   pr "\
4521 static int %s_skip (void)
4522 {
4523   const char *str;
4524
4525   str = getenv (\"TEST_ONLY\");
4526   if (str)
4527     return strstr (str, \"%s\") == NULL;
4528   str = getenv (\"SKIP_%s\");
4529   if (str && strcmp (str, \"1\") == 0) return 1;
4530   str = getenv (\"SKIP_TEST_%s\");
4531   if (str && strcmp (str, \"1\") == 0) return 1;
4532   return 0;
4533 }
4534
4535 " test_name name (String.uppercase test_name) (String.uppercase name);
4536
4537   (match prereq with
4538    | Disabled | Always -> ()
4539    | If code | Unless code ->
4540        pr "static int %s_prereq (void)\n" test_name;
4541        pr "{\n";
4542        pr "  %s\n" code;
4543        pr "}\n";
4544        pr "\n";
4545   );
4546
4547   pr "\
4548 static int %s (void)
4549 {
4550   if (%s_skip ()) {
4551     printf (\"        %%s skipped (reason: environment variable set)\\n\", \"%s\");
4552     return 0;
4553   }
4554
4555 " test_name test_name test_name;
4556
4557   (match prereq with
4558    | Disabled ->
4559        pr "  printf (\"        %%s skipped (reason: test disabled in generator)\\n\", \"%s\");\n" test_name
4560    | If _ ->
4561        pr "  if (! %s_prereq ()) {\n" test_name;
4562        pr "    printf (\"        %%s skipped (reason: test prerequisite)\\n\", \"%s\");\n" test_name;
4563        pr "    return 0;\n";
4564        pr "  }\n";
4565        pr "\n";
4566        generate_one_test_body name i test_name init test;
4567    | Unless _ ->
4568        pr "  if (%s_prereq ()) {\n" test_name;
4569        pr "    printf (\"        %%s skipped (reason: test prerequisite)\\n\", \"%s\");\n" test_name;
4570        pr "    return 0;\n";
4571        pr "  }\n";
4572        pr "\n";
4573        generate_one_test_body name i test_name init test;
4574    | Always ->
4575        generate_one_test_body name i test_name init test
4576   );
4577
4578   pr "  return 0;\n";
4579   pr "}\n";
4580   pr "\n";
4581   test_name
4582
4583 and generate_one_test_body name i test_name init test =
4584   (match init with
4585    | InitNone
4586    | InitEmpty ->
4587        pr "  /* InitNone|InitEmpty for %s */\n" test_name;
4588        List.iter (generate_test_command_call test_name)
4589          [["blockdev_setrw"; "/dev/sda"];
4590           ["umount_all"];
4591           ["lvm_remove_all"]]
4592    | InitBasicFS ->
4593        pr "  /* InitBasicFS for %s: create ext2 on /dev/sda1 */\n" test_name;
4594        List.iter (generate_test_command_call test_name)
4595          [["blockdev_setrw"; "/dev/sda"];
4596           ["umount_all"];
4597           ["lvm_remove_all"];
4598           ["sfdiskM"; "/dev/sda"; ","];
4599           ["mkfs"; "ext2"; "/dev/sda1"];
4600           ["mount"; "/dev/sda1"; "/"]]
4601    | InitBasicFSonLVM ->
4602        pr "  /* InitBasicFSonLVM for %s: create ext2 on /dev/VG/LV */\n"
4603          test_name;
4604        List.iter (generate_test_command_call test_name)
4605          [["blockdev_setrw"; "/dev/sda"];
4606           ["umount_all"];
4607           ["lvm_remove_all"];
4608           ["sfdiskM"; "/dev/sda"; ","];
4609           ["pvcreate"; "/dev/sda1"];
4610           ["vgcreate"; "VG"; "/dev/sda1"];
4611           ["lvcreate"; "LV"; "VG"; "8"];
4612           ["mkfs"; "ext2"; "/dev/VG/LV"];
4613           ["mount"; "/dev/VG/LV"; "/"]]
4614   );
4615
4616   let get_seq_last = function
4617     | [] ->
4618         failwithf "%s: you cannot use [] (empty list) when expecting a command"
4619           test_name
4620     | seq ->
4621         let seq = List.rev seq in
4622         List.rev (List.tl seq), List.hd seq
4623   in
4624
4625   match test with
4626   | TestRun seq ->
4627       pr "  /* TestRun for %s (%d) */\n" name i;
4628       List.iter (generate_test_command_call test_name) seq
4629   | TestOutput (seq, expected) ->
4630       pr "  /* TestOutput for %s (%d) */\n" name i;
4631       pr "  const char *expected = \"%s\";\n" (c_quote expected);
4632       let seq, last = get_seq_last seq in
4633       let test () =
4634         pr "    if (strcmp (r, expected) != 0) {\n";
4635         pr "      fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r);\n" test_name;
4636         pr "      return -1;\n";
4637         pr "    }\n"
4638       in
4639       List.iter (generate_test_command_call test_name) seq;
4640       generate_test_command_call ~test test_name last
4641   | TestOutputList (seq, expected) ->
4642       pr "  /* TestOutputList for %s (%d) */\n" name i;
4643       let seq, last = get_seq_last seq in
4644       let test () =
4645         iteri (
4646           fun i str ->
4647             pr "    if (!r[%d]) {\n" i;
4648             pr "      fprintf (stderr, \"%s: short list returned from command\\n\");\n" test_name;
4649             pr "      print_strings (r);\n";
4650             pr "      return -1;\n";
4651             pr "    }\n";
4652             pr "    {\n";
4653             pr "      const char *expected = \"%s\";\n" (c_quote str);
4654             pr "      if (strcmp (r[%d], expected) != 0) {\n" i;
4655             pr "        fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r[%d]);\n" test_name i;
4656             pr "        return -1;\n";
4657             pr "      }\n";
4658             pr "    }\n"
4659         ) expected;
4660         pr "    if (r[%d] != NULL) {\n" (List.length expected);
4661         pr "      fprintf (stderr, \"%s: extra elements returned from command\\n\");\n"
4662           test_name;
4663         pr "      print_strings (r);\n";
4664         pr "      return -1;\n";
4665         pr "    }\n"
4666       in
4667       List.iter (generate_test_command_call test_name) seq;
4668       generate_test_command_call ~test test_name last
4669   | TestOutputListOfDevices (seq, expected) ->
4670       pr "  /* TestOutputListOfDevices for %s (%d) */\n" name i;
4671       let seq, last = get_seq_last seq in
4672       let test () =
4673         iteri (
4674           fun i str ->
4675             pr "    if (!r[%d]) {\n" i;
4676             pr "      fprintf (stderr, \"%s: short list returned from command\\n\");\n" test_name;
4677             pr "      print_strings (r);\n";
4678             pr "      return -1;\n";
4679             pr "    }\n";
4680             pr "    {\n";
4681             pr "      const char *expected = \"%s\";\n" (c_quote str);
4682             pr "      r[%d][5] = 's';\n" i;
4683             pr "      if (strcmp (r[%d], expected) != 0) {\n" i;
4684             pr "        fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r[%d]);\n" test_name i;
4685             pr "        return -1;\n";
4686             pr "      }\n";
4687             pr "    }\n"
4688         ) expected;
4689         pr "    if (r[%d] != NULL) {\n" (List.length expected);
4690         pr "      fprintf (stderr, \"%s: extra elements returned from command\\n\");\n"
4691           test_name;
4692         pr "      print_strings (r);\n";
4693         pr "      return -1;\n";
4694         pr "    }\n"
4695       in
4696       List.iter (generate_test_command_call test_name) seq;
4697       generate_test_command_call ~test test_name last
4698   | TestOutputInt (seq, expected) ->
4699       pr "  /* TestOutputInt for %s (%d) */\n" name i;
4700       let seq, last = get_seq_last seq in
4701       let test () =
4702         pr "    if (r != %d) {\n" expected;
4703         pr "      fprintf (stderr, \"%s: expected %d but got %%d\\n\","
4704           test_name expected;
4705         pr "               (int) r);\n";
4706         pr "      return -1;\n";
4707         pr "    }\n"
4708       in
4709       List.iter (generate_test_command_call test_name) seq;
4710       generate_test_command_call ~test test_name last
4711   | TestOutputTrue seq ->
4712       pr "  /* TestOutputTrue for %s (%d) */\n" name i;
4713       let seq, last = get_seq_last seq in
4714       let test () =
4715         pr "    if (!r) {\n";
4716         pr "      fprintf (stderr, \"%s: expected true, got false\\n\");\n"
4717           test_name;
4718         pr "      return -1;\n";
4719         pr "    }\n"
4720       in
4721       List.iter (generate_test_command_call test_name) seq;
4722       generate_test_command_call ~test test_name last
4723   | TestOutputFalse seq ->
4724       pr "  /* TestOutputFalse for %s (%d) */\n" name i;
4725       let seq, last = get_seq_last seq in
4726       let test () =
4727         pr "    if (r) {\n";
4728         pr "      fprintf (stderr, \"%s: expected false, got true\\n\");\n"
4729           test_name;
4730         pr "      return -1;\n";
4731         pr "    }\n"
4732       in
4733       List.iter (generate_test_command_call test_name) seq;
4734       generate_test_command_call ~test test_name last
4735   | TestOutputLength (seq, expected) ->
4736       pr "  /* TestOutputLength for %s (%d) */\n" name i;
4737       let seq, last = get_seq_last seq in
4738       let test () =
4739         pr "    int j;\n";
4740         pr "    for (j = 0; j < %d; ++j)\n" expected;
4741         pr "      if (r[j] == NULL) {\n";
4742         pr "        fprintf (stderr, \"%s: short list returned\\n\");\n"
4743           test_name;
4744         pr "        print_strings (r);\n";
4745         pr "        return -1;\n";
4746         pr "      }\n";
4747         pr "    if (r[j] != NULL) {\n";
4748         pr "      fprintf (stderr, \"%s: long list returned\\n\");\n"
4749           test_name;
4750         pr "      print_strings (r);\n";
4751         pr "      return -1;\n";
4752         pr "    }\n"
4753       in
4754       List.iter (generate_test_command_call test_name) seq;
4755       generate_test_command_call ~test test_name last
4756   | TestOutputStruct (seq, checks) ->
4757       pr "  /* TestOutputStruct for %s (%d) */\n" name i;
4758       let seq, last = get_seq_last seq in
4759       let test () =
4760         List.iter (
4761           function
4762           | CompareWithInt (field, expected) ->
4763               pr "    if (r->%s != %d) {\n" field expected;
4764               pr "      fprintf (stderr, \"%s: %s was %%d, expected %d\\n\",\n"
4765                 test_name field expected;
4766               pr "               (int) r->%s);\n" field;
4767               pr "      return -1;\n";
4768               pr "    }\n"
4769           | CompareWithString (field, expected) ->
4770               pr "    if (strcmp (r->%s, \"%s\") != 0) {\n" field expected;
4771               pr "      fprintf (stderr, \"%s: %s was \"%%s\", expected \"%s\"\\n\",\n"
4772                 test_name field expected;
4773               pr "               r->%s);\n" field;
4774               pr "      return -1;\n";
4775               pr "    }\n"
4776           | CompareFieldsIntEq (field1, field2) ->
4777               pr "    if (r->%s != r->%s) {\n" field1 field2;
4778               pr "      fprintf (stderr, \"%s: %s (%%d) <> %s (%%d)\\n\",\n"
4779                 test_name field1 field2;
4780               pr "               (int) r->%s, (int) r->%s);\n" field1 field2;
4781               pr "      return -1;\n";
4782               pr "    }\n"
4783           | CompareFieldsStrEq (field1, field2) ->
4784               pr "    if (strcmp (r->%s, r->%s) != 0) {\n" field1 field2;
4785               pr "      fprintf (stderr, \"%s: %s (\"%%s\") <> %s (\"%%s\")\\n\",\n"
4786                 test_name field1 field2;
4787               pr "               r->%s, r->%s);\n" field1 field2;
4788               pr "      return -1;\n";
4789               pr "    }\n"
4790         ) checks
4791       in
4792       List.iter (generate_test_command_call test_name) seq;
4793       generate_test_command_call ~test test_name last
4794   | TestLastFail seq ->
4795       pr "  /* TestLastFail for %s (%d) */\n" name i;
4796       let seq, last = get_seq_last seq in
4797       List.iter (generate_test_command_call test_name) seq;
4798       generate_test_command_call test_name ~expect_error:true last
4799
4800 (* Generate the code to run a command, leaving the result in 'r'.
4801  * If you expect to get an error then you should set expect_error:true.
4802  *)
4803 and generate_test_command_call ?(expect_error = false) ?test test_name cmd =
4804   match cmd with
4805   | [] -> assert false
4806   | name :: args ->
4807       (* Look up the command to find out what args/ret it has. *)
4808       let style =
4809         try
4810           let _, style, _, _, _, _, _ =
4811             List.find (fun (n, _, _, _, _, _, _) -> n = name) all_functions in
4812           style
4813         with Not_found ->
4814           failwithf "%s: in test, command %s was not found" test_name name in
4815
4816       if List.length (snd style) <> List.length args then
4817         failwithf "%s: in test, wrong number of args given to %s"
4818           test_name name;
4819
4820       pr "  {\n";
4821
4822       List.iter (
4823         function
4824         | OptString n, "NULL" -> ()
4825         | String n, arg
4826         | OptString n, arg ->
4827             pr "    const char *%s = \"%s\";\n" n (c_quote arg);
4828         | Int _, _
4829         | Bool _, _
4830         | FileIn _, _ | FileOut _, _ -> ()
4831         | StringList n, arg ->
4832             let strs = string_split " " arg in
4833             iteri (
4834               fun i str ->
4835                 pr "    const char *%s_%d = \"%s\";\n" n i (c_quote str);
4836             ) strs;
4837             pr "    const char *%s[] = {\n" n;
4838             iteri (
4839               fun i _ -> pr "      %s_%d,\n" n i
4840             ) strs;
4841             pr "      NULL\n";
4842             pr "    };\n";
4843       ) (List.combine (snd style) args);
4844
4845       let error_code =
4846         match fst style with
4847         | RErr | RInt _ | RBool _ -> pr "    int r;\n"; "-1"
4848         | RInt64 _ -> pr "    int64_t r;\n"; "-1"
4849         | RConstString _ -> pr "    const char *r;\n"; "NULL"
4850         | RString _ -> pr "    char *r;\n"; "NULL"
4851         | RStringList _ | RHashtable _ ->
4852             pr "    char **r;\n";
4853             pr "    int i;\n";
4854             "NULL"
4855         | RStruct (_, typ) ->
4856             pr "    struct guestfs_%s *r;\n" typ; "NULL"
4857         | RStructList (_, typ) ->
4858             pr "    struct guestfs_%s_list *r;\n" typ; "NULL" in
4859
4860       pr "    suppress_error = %d;\n" (if expect_error then 1 else 0);
4861       pr "    r = guestfs_%s (g" name;
4862
4863       (* Generate the parameters. *)
4864       List.iter (
4865         function
4866         | OptString _, "NULL" -> pr ", NULL"
4867         | String n, _
4868         | OptString n, _ ->
4869             pr ", %s" n
4870         | FileIn _, arg | FileOut _, arg ->
4871             pr ", \"%s\"" (c_quote arg)
4872         | StringList n, _ ->
4873             pr ", %s" n
4874         | Int _, arg ->
4875             let i =
4876               try int_of_string arg
4877               with Failure "int_of_string" ->
4878                 failwithf "%s: expecting an int, but got '%s'" test_name arg in
4879             pr ", %d" i
4880         | Bool _, arg ->
4881             let b = bool_of_string arg in pr ", %d" (if b then 1 else 0)
4882       ) (List.combine (snd style) args);
4883
4884       pr ");\n";
4885       if not expect_error then
4886         pr "    if (r == %s)\n" error_code
4887       else
4888         pr "    if (r != %s)\n" error_code;
4889       pr "      return -1;\n";
4890
4891       (* Insert the test code. *)
4892       (match test with
4893        | None -> ()
4894        | Some f -> f ()
4895       );
4896
4897       (match fst style with
4898        | RErr | RInt _ | RInt64 _ | RBool _ | RConstString _ -> ()
4899        | RString _ -> pr "    free (r);\n"
4900        | RStringList _ | RHashtable _ ->
4901            pr "    for (i = 0; r[i] != NULL; ++i)\n";
4902            pr "      free (r[i]);\n";
4903            pr "    free (r);\n"
4904        | RStruct (_, typ) ->
4905            pr "    guestfs_free_%s (r);\n" typ
4906        | RStructList (_, typ) ->
4907            pr "    guestfs_free_%s_list (r);\n" typ
4908       );
4909
4910       pr "  }\n"
4911
4912 and c_quote str =
4913   let str = replace_str str "\r" "\\r" in
4914   let str = replace_str str "\n" "\\n" in
4915   let str = replace_str str "\t" "\\t" in
4916   let str = replace_str str "\000" "\\0" in
4917   str
4918
4919 (* Generate a lot of different functions for guestfish. *)
4920 and generate_fish_cmds () =
4921   generate_header CStyle GPLv2;
4922
4923   let all_functions =
4924     List.filter (
4925       fun (_, _, _, flags, _, _, _) -> not (List.mem NotInFish flags)
4926     ) all_functions in
4927   let all_functions_sorted =
4928     List.filter (
4929       fun (_, _, _, flags, _, _, _) -> not (List.mem NotInFish flags)
4930     ) all_functions_sorted in
4931
4932   pr "#include <stdio.h>\n";
4933   pr "#include <stdlib.h>\n";
4934   pr "#include <string.h>\n";
4935   pr "#include <inttypes.h>\n";
4936   pr "\n";
4937   pr "#include <guestfs.h>\n";
4938   pr "#include \"fish.h\"\n";
4939   pr "\n";
4940
4941   (* list_commands function, which implements guestfish -h *)
4942   pr "void list_commands (void)\n";
4943   pr "{\n";
4944   pr "  printf (\"    %%-16s     %%s\\n\", \"Command\", \"Description\");\n";
4945   pr "  list_builtin_commands ();\n";
4946   List.iter (
4947     fun (name, _, _, flags, _, shortdesc, _) ->
4948       let name = replace_char name '_' '-' in
4949       pr "  printf (\"%%-20s %%s\\n\", \"%s\", \"%s\");\n"
4950         name shortdesc
4951   ) all_functions_sorted;
4952   pr "  printf (\"    Use -h <cmd> / help <cmd> to show detailed help for a command.\\n\");\n";
4953   pr "}\n";
4954   pr "\n";
4955
4956   (* display_command function, which implements guestfish -h cmd *)
4957   pr "void display_command (const char *cmd)\n";
4958   pr "{\n";
4959   List.iter (
4960     fun (name, style, _, flags, _, shortdesc, longdesc) ->
4961       let name2 = replace_char name '_' '-' in
4962       let alias =
4963         try find_map (function FishAlias n -> Some n | _ -> None) flags
4964         with Not_found -> name in
4965       let longdesc = replace_str longdesc "C<guestfs_" "C<" in
4966       let synopsis =
4967         match snd style with
4968         | [] -> name2
4969         | args ->
4970             sprintf "%s <%s>"
4971               name2 (String.concat "> <" (List.map name_of_argt args)) in
4972
4973       let warnings =
4974         if List.mem ProtocolLimitWarning flags then
4975           ("\n\n" ^ protocol_limit_warning)
4976         else "" in
4977
4978       (* For DangerWillRobinson commands, we should probably have
4979        * guestfish prompt before allowing you to use them (especially
4980        * in interactive mode). XXX
4981        *)
4982       let warnings =
4983         warnings ^
4984           if List.mem DangerWillRobinson flags then
4985             ("\n\n" ^ danger_will_robinson)
4986           else "" in
4987
4988       let describe_alias =
4989         if name <> alias then
4990           sprintf "\n\nYou can use '%s' as an alias for this command." alias
4991         else "" in
4992
4993       pr "  if (";
4994       pr "strcasecmp (cmd, \"%s\") == 0" name;
4995       if name <> name2 then
4996         pr " || strcasecmp (cmd, \"%s\") == 0" name2;
4997       if name <> alias then
4998         pr " || strcasecmp (cmd, \"%s\") == 0" alias;
4999       pr ")\n";
5000       pr "    pod2text (\"%s - %s\", %S);\n"
5001         name2 shortdesc
5002         (" " ^ synopsis ^ "\n\n" ^ longdesc ^ warnings ^ describe_alias);
5003       pr "  else\n"
5004   ) all_functions;
5005   pr "    display_builtin_command (cmd);\n";
5006   pr "}\n";
5007   pr "\n";
5008
5009   (* print_* functions *)
5010   List.iter (
5011     fun (typ, cols) ->
5012       let needs_i =
5013         List.exists (function (_, FUUID) -> true | _ -> false) cols in
5014
5015       pr "static void print_%s (struct guestfs_%s *%s)\n" typ typ typ;
5016       pr "{\n";
5017       if needs_i then (
5018         pr "  int i;\n";
5019         pr "\n"
5020       );
5021       List.iter (
5022         function
5023         | name, FString ->
5024             pr "  printf (\"%s: %%s\\n\", %s->%s);\n" name typ name
5025         | name, FUUID ->
5026             pr "  printf (\"%s: \");\n" name;
5027             pr "  for (i = 0; i < 32; ++i)\n";
5028             pr "    printf (\"%%c\", %s->%s[i]);\n" typ name;
5029             pr "  printf (\"\\n\");\n"
5030         | name, (FUInt64|FBytes) ->
5031             pr "  printf (\"%s: %%\" PRIu64 \"\\n\", %s->%s);\n" name typ name
5032         | name, FInt64 ->
5033             pr "  printf (\"%s: %%\" PRIi64 \"\\n\", %s->%s);\n" name typ name
5034         | name, FUInt32 ->
5035             pr "  printf (\"%s: %%\" PRIu32 \"\\n\", %s->%s);\n" name typ name
5036         | name, FInt32 ->
5037             pr "  printf (\"%s: %%\" PRIi32 \"\\n\", %s->%s);\n" name typ name
5038         | name, FChar ->
5039             pr "  printf (\"%s: %%c\\n\", %s->%s);\n" name typ name
5040         | name, FOptPercent ->
5041             pr "  if (%s->%s >= 0) printf (\"%s: %%g %%%%\\n\", %s->%s);\n"
5042               typ name name typ name;
5043             pr "  else printf (\"%s: \\n\");\n" name
5044       ) cols;
5045       pr "}\n";
5046       pr "\n";
5047       pr "static void print_%s_list (struct guestfs_%s_list *%ss)\n"
5048         typ typ typ;
5049       pr "{\n";
5050       pr "  int i;\n";
5051       pr "\n";
5052       pr "  for (i = 0; i < %ss->len; ++i)\n" typ;
5053       pr "    print_%s (&%ss->val[i]);\n" typ typ;
5054       pr "}\n";
5055       pr "\n";
5056   ) structs;
5057
5058   (* run_<action> actions *)
5059   List.iter (
5060     fun (name, style, _, flags, _, _, _) ->
5061       pr "static int run_%s (const char *cmd, int argc, char *argv[])\n" name;
5062       pr "{\n";
5063       (match fst style with
5064        | RErr
5065        | RInt _
5066        | RBool _ -> pr "  int r;\n"
5067        | RInt64 _ -> pr "  int64_t r;\n"
5068        | RConstString _ -> pr "  const char *r;\n"
5069        | RString _ -> pr "  char *r;\n"
5070        | RStringList _ | RHashtable _ -> pr "  char **r;\n"
5071        | RStruct (_, typ) -> pr "  struct guestfs_%s *r;\n" typ
5072        | RStructList (_, typ) -> pr "  struct guestfs_%s_list *r;\n" typ
5073       );
5074       List.iter (
5075         function
5076         | String n
5077         | OptString n
5078         | FileIn n
5079         | FileOut n -> pr "  const char *%s;\n" n
5080         | StringList n -> pr "  char **%s;\n" n
5081         | Bool n -> pr "  int %s;\n" n
5082         | Int n -> pr "  int %s;\n" n
5083       ) (snd style);
5084
5085       (* Check and convert parameters. *)
5086       let argc_expected = List.length (snd style) in
5087       pr "  if (argc != %d) {\n" argc_expected;
5088       pr "    fprintf (stderr, \"%%s should have %d parameter(s)\\n\", cmd);\n"
5089         argc_expected;
5090       pr "    fprintf (stderr, \"type 'help %%s' for help on %%s\\n\", cmd, cmd);\n";
5091       pr "    return -1;\n";
5092       pr "  }\n";
5093       iteri (
5094         fun i ->
5095           function
5096           | String name -> pr "  %s = argv[%d];\n" name i
5097           | OptString name ->
5098               pr "  %s = strcmp (argv[%d], \"\") != 0 ? argv[%d] : NULL;\n"
5099                 name i i
5100           | FileIn name ->
5101               pr "  %s = strcmp (argv[%d], \"-\") != 0 ? argv[%d] : \"/dev/stdin\";\n"
5102                 name i i
5103           | FileOut name ->
5104               pr "  %s = strcmp (argv[%d], \"-\") != 0 ? argv[%d] : \"/dev/stdout\";\n"
5105                 name i i
5106           | StringList name ->
5107               pr "  %s = parse_string_list (argv[%d]);\n" name i
5108           | Bool name ->
5109               pr "  %s = is_true (argv[%d]) ? 1 : 0;\n" name i
5110           | Int name ->
5111               pr "  %s = atoi (argv[%d]);\n" name i
5112       ) (snd style);
5113
5114       (* Call C API function. *)
5115       let fn =
5116         try find_map (function FishAction n -> Some n | _ -> None) flags
5117         with Not_found -> sprintf "guestfs_%s" name in
5118       pr "  r = %s " fn;
5119       generate_call_args ~handle:"g" (snd style);
5120       pr ";\n";
5121
5122       (* Check return value for errors and display command results. *)
5123       (match fst style with
5124        | RErr -> pr "  return r;\n"
5125        | RInt _ ->
5126            pr "  if (r == -1) return -1;\n";
5127            pr "  printf (\"%%d\\n\", r);\n";
5128            pr "  return 0;\n"
5129        | RInt64 _ ->
5130            pr "  if (r == -1) return -1;\n";
5131            pr "  printf (\"%%\" PRIi64 \"\\n\", r);\n";
5132            pr "  return 0;\n"
5133        | RBool _ ->
5134            pr "  if (r == -1) return -1;\n";
5135            pr "  if (r) printf (\"true\\n\"); else printf (\"false\\n\");\n";
5136            pr "  return 0;\n"
5137        | RConstString _ ->
5138            pr "  if (r == NULL) return -1;\n";
5139            pr "  printf (\"%%s\\n\", r);\n";
5140            pr "  return 0;\n"
5141        | RString _ ->
5142            pr "  if (r == NULL) return -1;\n";
5143            pr "  printf (\"%%s\\n\", r);\n";
5144            pr "  free (r);\n";
5145            pr "  return 0;\n"
5146        | RStringList _ ->
5147            pr "  if (r == NULL) return -1;\n";
5148            pr "  print_strings (r);\n";
5149            pr "  free_strings (r);\n";
5150            pr "  return 0;\n"
5151        | RStruct (_, typ) ->
5152            pr "  if (r == NULL) return -1;\n";
5153            pr "  print_%s (r);\n" typ;
5154            pr "  guestfs_free_%s (r);\n" typ;
5155            pr "  return 0;\n"
5156        | RStructList (_, typ) ->
5157            pr "  if (r == NULL) return -1;\n";
5158            pr "  print_%s_list (r);\n" typ;
5159            pr "  guestfs_free_%s_list (r);\n" typ;
5160            pr "  return 0;\n"
5161        | RHashtable _ ->
5162            pr "  if (r == NULL) return -1;\n";
5163            pr "  print_table (r);\n";
5164            pr "  free_strings (r);\n";
5165            pr "  return 0;\n"
5166       );
5167       pr "}\n";
5168       pr "\n"
5169   ) all_functions;
5170
5171   (* run_action function *)
5172   pr "int run_action (const char *cmd, int argc, char *argv[])\n";
5173   pr "{\n";
5174   List.iter (
5175     fun (name, _, _, flags, _, _, _) ->
5176       let name2 = replace_char name '_' '-' in
5177       let alias =
5178         try find_map (function FishAlias n -> Some n | _ -> None) flags
5179         with Not_found -> name in
5180       pr "  if (";
5181       pr "strcasecmp (cmd, \"%s\") == 0" name;
5182       if name <> name2 then
5183         pr " || strcasecmp (cmd, \"%s\") == 0" name2;
5184       if name <> alias then
5185         pr " || strcasecmp (cmd, \"%s\") == 0" alias;
5186       pr ")\n";
5187       pr "    return run_%s (cmd, argc, argv);\n" name;
5188       pr "  else\n";
5189   ) all_functions;
5190   pr "    {\n";
5191   pr "      fprintf (stderr, \"%%s: unknown command\\n\", cmd);\n";
5192   pr "      return -1;\n";
5193   pr "    }\n";
5194   pr "  return 0;\n";
5195   pr "}\n";
5196   pr "\n"
5197
5198 (* Readline completion for guestfish. *)
5199 and generate_fish_completion () =
5200   generate_header CStyle GPLv2;
5201
5202   let all_functions =
5203     List.filter (
5204       fun (_, _, _, flags, _, _, _) -> not (List.mem NotInFish flags)
5205     ) all_functions in
5206
5207   pr "\
5208 #include <config.h>
5209
5210 #include <stdio.h>
5211 #include <stdlib.h>
5212 #include <string.h>
5213
5214 #ifdef HAVE_LIBREADLINE
5215 #include <readline/readline.h>
5216 #endif
5217
5218 #include \"fish.h\"
5219
5220 #ifdef HAVE_LIBREADLINE
5221
5222 static const char *const commands[] = {
5223   BUILTIN_COMMANDS_FOR_COMPLETION,
5224 ";
5225
5226   (* Get the commands, including the aliases.  They don't need to be
5227    * sorted - the generator() function just does a dumb linear search.
5228    *)
5229   let commands =
5230     List.map (
5231       fun (name, _, _, flags, _, _, _) ->
5232         let name2 = replace_char name '_' '-' in
5233         let alias =
5234           try find_map (function FishAlias n -> Some n | _ -> None) flags
5235           with Not_found -> name in
5236
5237         if name <> alias then [name2; alias] else [name2]
5238     ) all_functions in
5239   let commands = List.flatten commands in
5240
5241   List.iter (pr "  \"%s\",\n") commands;
5242
5243   pr "  NULL
5244 };
5245
5246 static char *
5247 generator (const char *text, int state)
5248 {
5249   static int index, len;
5250   const char *name;
5251
5252   if (!state) {
5253     index = 0;
5254     len = strlen (text);
5255   }
5256
5257   rl_attempted_completion_over = 1;
5258
5259   while ((name = commands[index]) != NULL) {
5260     index++;
5261     if (strncasecmp (name, text, len) == 0)
5262       return strdup (name);
5263   }
5264
5265   return NULL;
5266 }
5267
5268 #endif /* HAVE_LIBREADLINE */
5269
5270 char **do_completion (const char *text, int start, int end)
5271 {
5272   char **matches = NULL;
5273
5274 #ifdef HAVE_LIBREADLINE
5275   rl_completion_append_character = ' ';
5276
5277   if (start == 0)
5278     matches = rl_completion_matches (text, generator);
5279   else if (complete_dest_paths)
5280     matches = rl_completion_matches (text, complete_dest_paths_generator);
5281 #endif
5282
5283   return matches;
5284 }
5285 ";
5286
5287 (* Generate the POD documentation for guestfish. *)
5288 and generate_fish_actions_pod () =
5289   let all_functions_sorted =
5290     List.filter (
5291       fun (_, _, _, flags, _, _, _) ->
5292         not (List.mem NotInFish flags || List.mem NotInDocs flags)
5293     ) all_functions_sorted in
5294
5295   let rex = Str.regexp "C<guestfs_\\([^>]+\\)>" in
5296
5297   List.iter (
5298     fun (name, style, _, flags, _, _, longdesc) ->
5299       let longdesc =
5300         Str.global_substitute rex (
5301           fun s ->
5302             let sub =
5303               try Str.matched_group 1 s
5304               with Not_found ->
5305                 failwithf "error substituting C<guestfs_...> in longdesc of function %s" name in
5306             "C<" ^ replace_char sub '_' '-' ^ ">"
5307         ) longdesc in
5308       let name = replace_char name '_' '-' in
5309       let alias =
5310         try find_map (function FishAlias n -> Some n | _ -> None) flags
5311         with Not_found -> name in
5312
5313       pr "=head2 %s" name;
5314       if name <> alias then
5315         pr " | %s" alias;
5316       pr "\n";
5317       pr "\n";
5318       pr " %s" name;
5319       List.iter (
5320         function
5321         | String n -> pr " %s" n
5322         | OptString n -> pr " %s" n
5323         | StringList n -> pr " '%s ...'" n
5324         | Bool _ -> pr " true|false"
5325         | Int n -> pr " %s" n
5326         | FileIn n | FileOut n -> pr " (%s|-)" n
5327       ) (snd style);
5328       pr "\n";
5329       pr "\n";
5330       pr "%s\n\n" longdesc;
5331
5332       if List.exists (function FileIn _ | FileOut _ -> true
5333                       | _ -> false) (snd style) then
5334         pr "Use C<-> instead of a filename to read/write from stdin/stdout.\n\n";
5335
5336       if List.mem ProtocolLimitWarning flags then
5337         pr "%s\n\n" protocol_limit_warning;
5338
5339       if List.mem DangerWillRobinson flags then
5340         pr "%s\n\n" danger_will_robinson
5341   ) all_functions_sorted
5342
5343 (* Generate a C function prototype. *)
5344 and generate_prototype ?(extern = true) ?(static = false) ?(semicolon = true)
5345     ?(single_line = false) ?(newline = false) ?(in_daemon = false)
5346     ?(prefix = "")
5347     ?handle name style =
5348   if extern then pr "extern ";
5349   if static then pr "static ";
5350   (match fst style with
5351    | RErr -> pr "int "
5352    | RInt _ -> pr "int "
5353    | RInt64 _ -> pr "int64_t "
5354    | RBool _ -> pr "int "
5355    | RConstString _ -> pr "const char *"
5356    | RString _ -> pr "char *"
5357    | RStringList _ | RHashtable _ -> pr "char **"
5358    | RStruct (_, typ) ->
5359        if not in_daemon then pr "struct guestfs_%s *" typ
5360        else pr "guestfs_int_%s *" typ
5361    | RStructList (_, typ) ->
5362        if not in_daemon then pr "struct guestfs_%s_list *" typ
5363        else pr "guestfs_int_%s_list *" typ
5364   );
5365   pr "%s%s (" prefix name;
5366   if handle = None && List.length (snd style) = 0 then
5367     pr "void"
5368   else (
5369     let comma = ref false in
5370     (match handle with
5371      | None -> ()
5372      | Some handle -> pr "guestfs_h *%s" handle; comma := true
5373     );
5374     let next () =
5375       if !comma then (
5376         if single_line then pr ", " else pr ",\n\t\t"
5377       );
5378       comma := true
5379     in
5380     List.iter (
5381       function
5382       | String n
5383       | OptString n ->
5384           next ();
5385           if not in_daemon then pr "const char *%s" n
5386           else pr "char *%s" n
5387       | StringList n ->
5388           next ();
5389           if not in_daemon then pr "char * const* const %s" n
5390           else pr "char **%s" n
5391       | Bool n -> next (); pr "int %s" n
5392       | Int n -> next (); pr "int %s" n
5393       | FileIn n
5394       | FileOut n ->
5395           if not in_daemon then (next (); pr "const char *%s" n)
5396     ) (snd style);
5397   );
5398   pr ")";
5399   if semicolon then pr ";";
5400   if newline then pr "\n"
5401
5402 (* Generate C call arguments, eg "(handle, foo, bar)" *)
5403 and generate_call_args ?handle args =
5404   pr "(";
5405   let comma = ref false in
5406   (match handle with
5407    | None -> ()
5408    | Some handle -> pr "%s" handle; comma := true
5409   );
5410   List.iter (
5411     fun arg ->
5412       if !comma then pr ", ";
5413       comma := true;
5414       pr "%s" (name_of_argt arg)
5415   ) args;
5416   pr ")"
5417
5418 (* Generate the OCaml bindings interface. *)
5419 and generate_ocaml_mli () =
5420   generate_header OCamlStyle LGPLv2;
5421
5422   pr "\
5423 (** For API documentation you should refer to the C API
5424     in the guestfs(3) manual page.  The OCaml API uses almost
5425     exactly the same calls. *)
5426
5427 type t
5428 (** A [guestfs_h] handle. *)
5429
5430 exception Error of string
5431 (** This exception is raised when there is an error. *)
5432
5433 val create : unit -> t
5434
5435 val close : t -> unit
5436 (** Handles are closed by the garbage collector when they become
5437     unreferenced, but callers can also call this in order to
5438     provide predictable cleanup. *)
5439
5440 ";
5441   generate_ocaml_structure_decls ();
5442
5443   (* The actions. *)
5444   List.iter (
5445     fun (name, style, _, _, _, shortdesc, _) ->
5446       generate_ocaml_prototype name style;
5447       pr "(** %s *)\n" shortdesc;
5448       pr "\n"
5449   ) all_functions
5450
5451 (* Generate the OCaml bindings implementation. *)
5452 and generate_ocaml_ml () =
5453   generate_header OCamlStyle LGPLv2;
5454
5455   pr "\
5456 type t
5457 exception Error of string
5458 external create : unit -> t = \"ocaml_guestfs_create\"
5459 external close : t -> unit = \"ocaml_guestfs_close\"
5460
5461 let () =
5462   Callback.register_exception \"ocaml_guestfs_error\" (Error \"\")
5463
5464 ";
5465
5466   generate_ocaml_structure_decls ();
5467
5468   (* The actions. *)
5469   List.iter (
5470     fun (name, style, _, _, _, shortdesc, _) ->
5471       generate_ocaml_prototype ~is_external:true name style;
5472   ) all_functions
5473
5474 (* Generate the OCaml bindings C implementation. *)
5475 and generate_ocaml_c () =
5476   generate_header CStyle LGPLv2;
5477
5478   pr "\
5479 #include <stdio.h>
5480 #include <stdlib.h>
5481 #include <string.h>
5482
5483 #include <caml/config.h>
5484 #include <caml/alloc.h>
5485 #include <caml/callback.h>
5486 #include <caml/fail.h>
5487 #include <caml/memory.h>
5488 #include <caml/mlvalues.h>
5489 #include <caml/signals.h>
5490
5491 #include <guestfs.h>
5492
5493 #include \"guestfs_c.h\"
5494
5495 /* Copy a hashtable of string pairs into an assoc-list.  We return
5496  * the list in reverse order, but hashtables aren't supposed to be
5497  * ordered anyway.
5498  */
5499 static CAMLprim value
5500 copy_table (char * const * argv)
5501 {
5502   CAMLparam0 ();
5503   CAMLlocal5 (rv, pairv, kv, vv, cons);
5504   int i;
5505
5506   rv = Val_int (0);
5507   for (i = 0; argv[i] != NULL; i += 2) {
5508     kv = caml_copy_string (argv[i]);
5509     vv = caml_copy_string (argv[i+1]);
5510     pairv = caml_alloc (2, 0);
5511     Store_field (pairv, 0, kv);
5512     Store_field (pairv, 1, vv);
5513     cons = caml_alloc (2, 0);
5514     Store_field (cons, 1, rv);
5515     rv = cons;
5516     Store_field (cons, 0, pairv);
5517   }
5518
5519   CAMLreturn (rv);
5520 }
5521
5522 ";
5523
5524   (* Struct copy functions. *)
5525   List.iter (
5526     fun (typ, cols) ->
5527       let has_optpercent_col =
5528         List.exists (function (_, FOptPercent) -> true | _ -> false) cols in
5529
5530       pr "static CAMLprim value\n";
5531       pr "copy_%s (const struct guestfs_%s *%s)\n" typ typ typ;
5532       pr "{\n";
5533       pr "  CAMLparam0 ();\n";
5534       if has_optpercent_col then
5535         pr "  CAMLlocal3 (rv, v, v2);\n"
5536       else
5537         pr "  CAMLlocal2 (rv, v);\n";
5538       pr "\n";
5539       pr "  rv = caml_alloc (%d, 0);\n" (List.length cols);
5540       iteri (
5541         fun i col ->
5542           (match col with
5543            | name, FString ->
5544                pr "  v = caml_copy_string (%s->%s);\n" typ name
5545            | name, FUUID ->
5546                pr "  v = caml_alloc_string (32);\n";
5547                pr "  memcpy (String_val (v), %s->%s, 32);\n" typ name
5548            | name, (FBytes|FInt64|FUInt64) ->
5549                pr "  v = caml_copy_int64 (%s->%s);\n" typ name
5550            | name, (FInt32|FUInt32) ->
5551                pr "  v = caml_copy_int32 (%s->%s);\n" typ name
5552            | name, FOptPercent ->
5553                pr "  if (%s->%s >= 0) { /* Some %s */\n" typ name name;
5554                pr "    v2 = caml_copy_double (%s->%s);\n" typ name;
5555                pr "    v = caml_alloc (1, 0);\n";
5556                pr "    Store_field (v, 0, v2);\n";
5557                pr "  } else /* None */\n";
5558                pr "    v = Val_int (0);\n";
5559            | name, FChar ->
5560                pr "  v = Val_int (%s->%s);\n" typ name
5561           );
5562           pr "  Store_field (rv, %d, v);\n" i
5563       ) cols;
5564       pr "  CAMLreturn (rv);\n";
5565       pr "}\n";
5566       pr "\n";
5567
5568       pr "static CAMLprim value\n";
5569       pr "copy_%s_list (const struct guestfs_%s_list *%ss)\n"
5570         typ typ typ;
5571       pr "{\n";
5572       pr "  CAMLparam0 ();\n";
5573       pr "  CAMLlocal2 (rv, v);\n";
5574       pr "  int i;\n";
5575       pr "\n";
5576       pr "  if (%ss->len == 0)\n" typ;
5577       pr "    CAMLreturn (Atom (0));\n";
5578       pr "  else {\n";
5579       pr "    rv = caml_alloc (%ss->len, 0);\n" typ;
5580       pr "    for (i = 0; i < %ss->len; ++i) {\n" typ;
5581       pr "      v = copy_%s (&%ss->val[i]);\n" typ typ;
5582       pr "      caml_modify (&Field (rv, i), v);\n";
5583       pr "    }\n";
5584       pr "    CAMLreturn (rv);\n";
5585       pr "  }\n";
5586       pr "}\n";
5587       pr "\n";
5588   ) structs;
5589
5590   (* The wrappers. *)
5591   List.iter (
5592     fun (name, style, _, _, _, _, _) ->
5593       let params =
5594         "gv" :: List.map (fun arg -> name_of_argt arg ^ "v") (snd style) in
5595
5596       pr "CAMLprim value\n";
5597       pr "ocaml_guestfs_%s (value %s" name (List.hd params);
5598       List.iter (pr ", value %s") (List.tl params);
5599       pr ")\n";
5600       pr "{\n";
5601
5602       (match params with
5603        | [p1; p2; p3; p4; p5] ->
5604            pr "  CAMLparam5 (%s);\n" (String.concat ", " params)
5605        | p1 :: p2 :: p3 :: p4 :: p5 :: rest ->
5606            pr "  CAMLparam5 (%s);\n" (String.concat ", " [p1; p2; p3; p4; p5]);
5607            pr "  CAMLxparam%d (%s);\n"
5608              (List.length rest) (String.concat ", " rest)
5609        | ps ->
5610            pr "  CAMLparam%d (%s);\n" (List.length ps) (String.concat ", " ps)
5611       );
5612       pr "  CAMLlocal1 (rv);\n";
5613       pr "\n";
5614
5615       pr "  guestfs_h *g = Guestfs_val (gv);\n";
5616       pr "  if (g == NULL)\n";
5617       pr "    caml_failwith (\"%s: used handle after closing it\");\n" name;
5618       pr "\n";
5619
5620       List.iter (
5621         function
5622         | String n
5623         | FileIn n
5624         | FileOut n ->
5625             pr "  const char *%s = String_val (%sv);\n" n n
5626         | OptString n ->
5627             pr "  const char *%s =\n" n;
5628             pr "    %sv != Val_int (0) ? String_val (Field (%sv, 0)) : NULL;\n"
5629               n n
5630         | StringList n ->
5631             pr "  char **%s = ocaml_guestfs_strings_val (g, %sv);\n" n n
5632         | Bool n ->
5633             pr "  int %s = Bool_val (%sv);\n" n n
5634         | Int n ->
5635             pr "  int %s = Int_val (%sv);\n" n n
5636       ) (snd style);
5637       let error_code =
5638         match fst style with
5639         | RErr -> pr "  int r;\n"; "-1"
5640         | RInt _ -> pr "  int r;\n"; "-1"
5641         | RInt64 _ -> pr "  int64_t r;\n"; "-1"
5642         | RBool _ -> pr "  int r;\n"; "-1"
5643         | RConstString _ -> pr "  const char *r;\n"; "NULL"
5644         | RString _ -> pr "  char *r;\n"; "NULL"
5645         | RStringList _ ->
5646             pr "  int i;\n";
5647             pr "  char **r;\n";
5648             "NULL"
5649         | RStruct (_, typ) ->
5650             pr "  struct guestfs_%s *r;\n" typ; "NULL"
5651         | RStructList (_, typ) ->
5652             pr "  struct guestfs_%s_list *r;\n" typ; "NULL"
5653         | RHashtable _ ->
5654             pr "  int i;\n";
5655             pr "  char **r;\n";
5656             "NULL" in
5657       pr "\n";
5658
5659       pr "  caml_enter_blocking_section ();\n";
5660       pr "  r = guestfs_%s " name;
5661       generate_call_args ~handle:"g" (snd style);
5662       pr ";\n";
5663       pr "  caml_leave_blocking_section ();\n";
5664
5665       List.iter (
5666         function
5667         | StringList n ->
5668             pr "  ocaml_guestfs_free_strings (%s);\n" n;
5669         | String _ | OptString _ | Bool _ | Int _ | FileIn _ | FileOut _ -> ()
5670       ) (snd style);
5671
5672       pr "  if (r == %s)\n" error_code;
5673       pr "    ocaml_guestfs_raise_error (g, \"%s\");\n" name;
5674       pr "\n";
5675
5676       (match fst style with
5677        | RErr -> pr "  rv = Val_unit;\n"
5678        | RInt _ -> pr "  rv = Val_int (r);\n"
5679        | RInt64 _ ->
5680            pr "  rv = caml_copy_int64 (r);\n"
5681        | RBool _ -> pr "  rv = Val_bool (r);\n"
5682        | RConstString _ -> pr "  rv = caml_copy_string (r);\n"
5683        | RString _ ->
5684            pr "  rv = caml_copy_string (r);\n";
5685            pr "  free (r);\n"
5686        | RStringList _ ->
5687            pr "  rv = caml_copy_string_array ((const char **) r);\n";
5688            pr "  for (i = 0; r[i] != NULL; ++i) free (r[i]);\n";
5689            pr "  free (r);\n"
5690        | RStruct (_, typ) ->
5691            pr "  rv = copy_%s (r);\n" typ;
5692            pr "  guestfs_free_%s (r);\n" typ;
5693        | RStructList (_, typ) ->
5694            pr "  rv = copy_%s_list (r);\n" typ;
5695            pr "  guestfs_free_%s_list (r);\n" typ;
5696        | RHashtable _ ->
5697            pr "  rv = copy_table (r);\n";
5698            pr "  for (i = 0; r[i] != NULL; ++i) free (r[i]);\n";
5699            pr "  free (r);\n";
5700       );
5701
5702       pr "  CAMLreturn (rv);\n";
5703       pr "}\n";
5704       pr "\n";
5705
5706       if List.length params > 5 then (
5707         pr "CAMLprim value\n";
5708         pr "ocaml_guestfs_%s_byte (value *argv, int argn)\n" name;
5709         pr "{\n";
5710         pr "  return ocaml_guestfs_%s (argv[0]" name;
5711         iteri (fun i _ -> pr ", argv[%d]" i) (List.tl params);
5712         pr ");\n";
5713         pr "}\n";
5714         pr "\n"
5715       )
5716   ) all_functions
5717
5718 and generate_ocaml_structure_decls () =
5719   List.iter (
5720     fun (typ, cols) ->
5721       pr "type %s = {\n" typ;
5722       List.iter (
5723         function
5724         | name, FString -> pr "  %s : string;\n" name
5725         | name, FUUID -> pr "  %s : string;\n" name
5726         | name, (FBytes|FInt64|FUInt64) -> pr "  %s : int64;\n" name
5727         | name, (FInt32|FUInt32) -> pr "  %s : int32;\n" name
5728         | name, FChar -> pr "  %s : char;\n" name
5729         | name, FOptPercent -> pr "  %s : float option;\n" name
5730       ) cols;
5731       pr "}\n";
5732       pr "\n"
5733   ) structs
5734
5735 and generate_ocaml_prototype ?(is_external = false) name style =
5736   if is_external then pr "external " else pr "val ";
5737   pr "%s : t -> " name;
5738   List.iter (
5739     function
5740     | String _ | FileIn _ | FileOut _ -> pr "string -> "
5741     | OptString _ -> pr "string option -> "
5742     | StringList _ -> pr "string array -> "
5743     | Bool _ -> pr "bool -> "
5744     | Int _ -> pr "int -> "
5745   ) (snd style);
5746   (match fst style with
5747    | RErr -> pr "unit" (* all errors are turned into exceptions *)
5748    | RInt _ -> pr "int"
5749    | RInt64 _ -> pr "int64"
5750    | RBool _ -> pr "bool"
5751    | RConstString _ -> pr "string"
5752    | RString _ -> pr "string"
5753    | RStringList _ -> pr "string array"
5754    | RStruct (_, typ) -> pr "%s" typ
5755    | RStructList (_, typ) -> pr "%s array" typ
5756    | RHashtable _ -> pr "(string * string) list"
5757   );
5758   if is_external then (
5759     pr " = ";
5760     if List.length (snd style) + 1 > 5 then
5761       pr "\"ocaml_guestfs_%s_byte\" " name;
5762     pr "\"ocaml_guestfs_%s\"" name
5763   );
5764   pr "\n"
5765
5766 (* Generate Perl xs code, a sort of crazy variation of C with macros. *)
5767 and generate_perl_xs () =
5768   generate_header CStyle LGPLv2;
5769
5770   pr "\
5771 #include \"EXTERN.h\"
5772 #include \"perl.h\"
5773 #include \"XSUB.h\"
5774
5775 #include <guestfs.h>
5776
5777 #ifndef PRId64
5778 #define PRId64 \"lld\"
5779 #endif
5780
5781 static SV *
5782 my_newSVll(long long val) {
5783 #ifdef USE_64_BIT_ALL
5784   return newSViv(val);
5785 #else
5786   char buf[100];
5787   int len;
5788   len = snprintf(buf, 100, \"%%\" PRId64, val);
5789   return newSVpv(buf, len);
5790 #endif
5791 }
5792
5793 #ifndef PRIu64
5794 #define PRIu64 \"llu\"
5795 #endif
5796
5797 static SV *
5798 my_newSVull(unsigned long long val) {
5799 #ifdef USE_64_BIT_ALL
5800   return newSVuv(val);
5801 #else
5802   char buf[100];
5803   int len;
5804   len = snprintf(buf, 100, \"%%\" PRIu64, val);
5805   return newSVpv(buf, len);
5806 #endif
5807 }
5808
5809 /* http://www.perlmonks.org/?node_id=680842 */
5810 static char **
5811 XS_unpack_charPtrPtr (SV *arg) {
5812   char **ret;
5813   AV *av;
5814   I32 i;
5815
5816   if (!arg || !SvOK (arg) || !SvROK (arg) || SvTYPE (SvRV (arg)) != SVt_PVAV)
5817     croak (\"array reference expected\");
5818
5819   av = (AV *)SvRV (arg);
5820   ret = malloc ((av_len (av) + 1 + 1) * sizeof (char *));
5821   if (!ret)
5822     croak (\"malloc failed\");
5823
5824   for (i = 0; i <= av_len (av); i++) {
5825     SV **elem = av_fetch (av, i, 0);
5826
5827     if (!elem || !*elem)
5828       croak (\"missing element in list\");
5829
5830     ret[i] = SvPV_nolen (*elem);
5831   }
5832
5833   ret[i] = NULL;
5834
5835   return ret;
5836 }
5837
5838 MODULE = Sys::Guestfs  PACKAGE = Sys::Guestfs
5839
5840 PROTOTYPES: ENABLE
5841
5842 guestfs_h *
5843 _create ()
5844    CODE:
5845       RETVAL = guestfs_create ();
5846       if (!RETVAL)
5847         croak (\"could not create guestfs handle\");
5848       guestfs_set_error_handler (RETVAL, NULL, NULL);
5849  OUTPUT:
5850       RETVAL
5851
5852 void
5853 DESTROY (g)
5854       guestfs_h *g;
5855  PPCODE:
5856       guestfs_close (g);
5857
5858 ";
5859
5860   List.iter (
5861     fun (name, style, _, _, _, _, _) ->
5862       (match fst style with
5863        | RErr -> pr "void\n"
5864        | RInt _ -> pr "SV *\n"
5865        | RInt64 _ -> pr "SV *\n"
5866        | RBool _ -> pr "SV *\n"
5867        | RConstString _ -> pr "SV *\n"
5868        | RString _ -> pr "SV *\n"
5869        | RStringList _
5870        | RStruct _ | RStructList _
5871        | RHashtable _ ->
5872            pr "void\n" (* all lists returned implictly on the stack *)
5873       );
5874       (* Call and arguments. *)
5875       pr "%s " name;
5876       generate_call_args ~handle:"g" (snd style);
5877       pr "\n";
5878       pr "      guestfs_h *g;\n";
5879       iteri (
5880         fun i ->
5881           function
5882           | String n | FileIn n | FileOut n -> pr "      char *%s;\n" n
5883           | OptString n ->
5884               (* http://www.perlmonks.org/?node_id=554277
5885                * Note that the implicit handle argument means we have
5886                * to add 1 to the ST(x) operator.
5887                *)
5888               pr "      char *%s = SvOK(ST(%d)) ? SvPV_nolen(ST(%d)) : NULL;\n" n (i+1) (i+1)
5889           | StringList n -> pr "      char **%s;\n" n
5890           | Bool n -> pr "      int %s;\n" n
5891           | Int n -> pr "      int %s;\n" n
5892       ) (snd style);
5893
5894       let do_cleanups () =
5895         List.iter (
5896           function
5897           | String _ | OptString _ | Bool _ | Int _
5898           | FileIn _ | FileOut _ -> ()
5899           | StringList n -> pr "      free (%s);\n" n
5900         ) (snd style)
5901       in
5902
5903       (* Code. *)
5904       (match fst style with
5905        | RErr ->
5906            pr "PREINIT:\n";
5907            pr "      int r;\n";
5908            pr " PPCODE:\n";
5909            pr "      r = guestfs_%s " name;
5910            generate_call_args ~handle:"g" (snd style);
5911            pr ";\n";
5912            do_cleanups ();
5913            pr "      if (r == -1)\n";
5914            pr "        croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
5915        | RInt n
5916        | RBool n ->
5917            pr "PREINIT:\n";
5918            pr "      int %s;\n" n;
5919            pr "   CODE:\n";
5920            pr "      %s = guestfs_%s " n name;
5921            generate_call_args ~handle:"g" (snd style);
5922            pr ";\n";
5923            do_cleanups ();
5924            pr "      if (%s == -1)\n" n;
5925            pr "        croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
5926            pr "      RETVAL = newSViv (%s);\n" n;
5927            pr " OUTPUT:\n";
5928            pr "      RETVAL\n"
5929        | RInt64 n ->
5930            pr "PREINIT:\n";
5931            pr "      int64_t %s;\n" n;
5932            pr "   CODE:\n";
5933            pr "      %s = guestfs_%s " n name;
5934            generate_call_args ~handle:"g" (snd style);
5935            pr ";\n";
5936            do_cleanups ();
5937            pr "      if (%s == -1)\n" n;
5938            pr "        croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
5939            pr "      RETVAL = my_newSVll (%s);\n" n;
5940            pr " OUTPUT:\n";
5941            pr "      RETVAL\n"
5942        | RConstString n ->
5943            pr "PREINIT:\n";
5944            pr "      const char *%s;\n" n;
5945            pr "   CODE:\n";
5946            pr "      %s = guestfs_%s " n name;
5947            generate_call_args ~handle:"g" (snd style);
5948            pr ";\n";
5949            do_cleanups ();
5950            pr "      if (%s == NULL)\n" n;
5951            pr "        croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
5952            pr "      RETVAL = newSVpv (%s, 0);\n" n;
5953            pr " OUTPUT:\n";
5954            pr "      RETVAL\n"
5955        | RString n ->
5956            pr "PREINIT:\n";
5957            pr "      char *%s;\n" n;
5958            pr "   CODE:\n";
5959            pr "      %s = guestfs_%s " n name;
5960            generate_call_args ~handle:"g" (snd style);
5961            pr ";\n";
5962            do_cleanups ();
5963            pr "      if (%s == NULL)\n" n;
5964            pr "        croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
5965            pr "      RETVAL = newSVpv (%s, 0);\n" n;
5966            pr "      free (%s);\n" n;
5967            pr " OUTPUT:\n";
5968            pr "      RETVAL\n"
5969        | RStringList n | RHashtable n ->
5970            pr "PREINIT:\n";
5971            pr "      char **%s;\n" n;
5972            pr "      int i, n;\n";
5973            pr " PPCODE:\n";
5974            pr "      %s = guestfs_%s " n name;
5975            generate_call_args ~handle:"g" (snd style);
5976            pr ";\n";
5977            do_cleanups ();
5978            pr "      if (%s == NULL)\n" n;
5979            pr "        croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
5980            pr "      for (n = 0; %s[n] != NULL; ++n) /**/;\n" n;
5981            pr "      EXTEND (SP, n);\n";
5982            pr "      for (i = 0; i < n; ++i) {\n";
5983            pr "        PUSHs (sv_2mortal (newSVpv (%s[i], 0)));\n" n;
5984            pr "        free (%s[i]);\n" n;
5985            pr "      }\n";
5986            pr "      free (%s);\n" n;
5987        | RStruct (n, typ) ->
5988            let cols = cols_of_struct typ in
5989            generate_perl_struct_code typ cols name style n do_cleanups
5990        | RStructList (n, typ) ->
5991            let cols = cols_of_struct typ in
5992            generate_perl_struct_list_code typ cols name style n do_cleanups
5993       );
5994
5995       pr "\n"
5996   ) all_functions
5997
5998 and generate_perl_struct_list_code typ cols name style n do_cleanups =
5999   pr "PREINIT:\n";
6000   pr "      struct guestfs_%s_list *%s;\n" typ n;
6001   pr "      int i;\n";
6002   pr "      HV *hv;\n";
6003   pr " PPCODE:\n";
6004   pr "      %s = guestfs_%s " n name;
6005   generate_call_args ~handle:"g" (snd style);
6006   pr ";\n";
6007   do_cleanups ();
6008   pr "      if (%s == NULL)\n" n;
6009   pr "        croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
6010   pr "      EXTEND (SP, %s->len);\n" n;
6011   pr "      for (i = 0; i < %s->len; ++i) {\n" n;
6012   pr "        hv = newHV ();\n";
6013   List.iter (
6014     function
6015     | name, FString ->
6016         pr "        (void) hv_store (hv, \"%s\", %d, newSVpv (%s->val[i].%s, 0), 0);\n"
6017           name (String.length name) n name
6018     | name, FUUID ->
6019         pr "        (void) hv_store (hv, \"%s\", %d, newSVpv (%s->val[i].%s, 32), 0);\n"
6020           name (String.length name) n name
6021     | name, (FBytes|FUInt64) ->
6022         pr "        (void) hv_store (hv, \"%s\", %d, my_newSVull (%s->val[i].%s), 0);\n"
6023           name (String.length name) n name
6024     | name, FInt64 ->
6025         pr "        (void) hv_store (hv, \"%s\", %d, my_newSVll (%s->val[i].%s), 0);\n"
6026           name (String.length name) n name
6027     | name, (FInt32|FUInt32) ->
6028         pr "        (void) hv_store (hv, \"%s\", %d, newSVnv (%s->val[i].%s), 0);\n"
6029           name (String.length name) n name
6030     | name, FChar ->
6031         pr "        (void) hv_store (hv, \"%s\", %d, newSVpv (&%s->val[i].%s, 1), 0);\n"
6032           name (String.length name) n name
6033     | name, FOptPercent ->
6034         pr "        (void) hv_store (hv, \"%s\", %d, newSVnv (%s->val[i].%s), 0);\n"
6035           name (String.length name) n name
6036   ) cols;
6037   pr "        PUSHs (sv_2mortal (newRV ((SV *) hv)));\n";
6038   pr "      }\n";
6039   pr "      guestfs_free_%s_list (%s);\n" typ n
6040
6041 and generate_perl_struct_code typ cols name style n do_cleanups =
6042   pr "PREINIT:\n";
6043   pr "      struct guestfs_%s *%s;\n" typ n;
6044   pr " PPCODE:\n";
6045   pr "      %s = guestfs_%s " n name;
6046   generate_call_args ~handle:"g" (snd style);
6047   pr ";\n";
6048   do_cleanups ();
6049   pr "      if (%s == NULL)\n" n;
6050   pr "        croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
6051   pr "      EXTEND (SP, %d);\n" (List.length cols);
6052   List.iter (
6053     function
6054     | name, FString ->
6055         pr "      PUSHs (sv_2mortal (newSVpv (%s->%s, 0)));\n"
6056           n name
6057     | name, FUUID ->
6058         pr "      PUSHs (sv_2mortal (newSVpv (%s->%s, 32)));\n"
6059           n name
6060     | name, (FBytes|FUInt64) ->
6061         pr "      PUSHs (sv_2mortal (my_newSVull (%s->%s)));\n"
6062           n name
6063     | name, FInt64 ->
6064         pr "      PUSHs (sv_2mortal (my_newSVll (%s->%s)));\n"
6065           n name
6066     | name, (FInt32|FUInt32) ->
6067         pr "      PUSHs (sv_2mortal (newSVnv (%s->%s)));\n"
6068           n name
6069     | name, FChar ->
6070         pr "      PUSHs (sv_2mortal (newSVpv (&%s->%s, 1)));\n"
6071           n name
6072     | name, FOptPercent ->
6073         pr "      PUSHs (sv_2mortal (newSVnv (%s->%s)));\n"
6074           n name
6075   ) cols;
6076   pr "      free (%s);\n" n
6077
6078 (* Generate Sys/Guestfs.pm. *)
6079 and generate_perl_pm () =
6080   generate_header HashStyle LGPLv2;
6081
6082   pr "\
6083 =pod
6084
6085 =head1 NAME
6086
6087 Sys::Guestfs - Perl bindings for libguestfs
6088
6089 =head1 SYNOPSIS
6090
6091  use Sys::Guestfs;
6092
6093  my $h = Sys::Guestfs->new ();
6094  $h->add_drive ('guest.img');
6095  $h->launch ();
6096  $h->wait_ready ();
6097  $h->mount ('/dev/sda1', '/');
6098  $h->touch ('/hello');
6099  $h->sync ();
6100
6101 =head1 DESCRIPTION
6102
6103 The C<Sys::Guestfs> module provides a Perl XS binding to the
6104 libguestfs API for examining and modifying virtual machine
6105 disk images.
6106
6107 Amongst the things this is good for: making batch configuration
6108 changes to guests, getting disk used/free statistics (see also:
6109 virt-df), migrating between virtualization systems (see also:
6110 virt-p2v), performing partial backups, performing partial guest
6111 clones, cloning guests and changing registry/UUID/hostname info, and
6112 much else besides.
6113
6114 Libguestfs uses Linux kernel and qemu code, and can access any type of
6115 guest filesystem that Linux and qemu can, including but not limited
6116 to: ext2/3/4, btrfs, FAT and NTFS, LVM, many different disk partition
6117 schemes, qcow, qcow2, vmdk.
6118
6119 Libguestfs provides ways to enumerate guest storage (eg. partitions,
6120 LVs, what filesystem is in each LV, etc.).  It can also run commands
6121 in the context of the guest.  Also you can access filesystems over FTP.
6122
6123 See also L<Sys::Guestfs::Lib(3)> for a set of useful library
6124 functions for using libguestfs from Perl, including integration
6125 with libvirt.
6126
6127 =head1 ERRORS
6128
6129 All errors turn into calls to C<croak> (see L<Carp(3)>).
6130
6131 =head1 METHODS
6132
6133 =over 4
6134
6135 =cut
6136
6137 package Sys::Guestfs;
6138
6139 use strict;
6140 use warnings;
6141
6142 require XSLoader;
6143 XSLoader::load ('Sys::Guestfs');
6144
6145 =item $h = Sys::Guestfs->new ();
6146
6147 Create a new guestfs handle.
6148
6149 =cut
6150
6151 sub new {
6152   my $proto = shift;
6153   my $class = ref ($proto) || $proto;
6154
6155   my $self = Sys::Guestfs::_create ();
6156   bless $self, $class;
6157   return $self;
6158 }
6159
6160 ";
6161
6162   (* Actions.  We only need to print documentation for these as
6163    * they are pulled in from the XS code automatically.
6164    *)
6165   List.iter (
6166     fun (name, style, _, flags, _, _, longdesc) ->
6167       if not (List.mem NotInDocs flags) then (
6168         let longdesc = replace_str longdesc "C<guestfs_" "C<$h-E<gt>" in
6169         pr "=item ";
6170         generate_perl_prototype name style;
6171         pr "\n\n";
6172         pr "%s\n\n" longdesc;
6173         if List.mem ProtocolLimitWarning flags then
6174           pr "%s\n\n" protocol_limit_warning;
6175         if List.mem DangerWillRobinson flags then
6176           pr "%s\n\n" danger_will_robinson
6177       )
6178   ) all_functions_sorted;
6179
6180   (* End of file. *)
6181   pr "\
6182 =cut
6183
6184 1;
6185
6186 =back
6187
6188 =head1 COPYRIGHT
6189
6190 Copyright (C) 2009 Red Hat Inc.
6191
6192 =head1 LICENSE
6193
6194 Please see the file COPYING.LIB for the full license.
6195
6196 =head1 SEE ALSO
6197
6198 L<guestfs(3)>,
6199 L<guestfish(1)>,
6200 L<http://libguestfs.org>,
6201 L<Sys::Guestfs::Lib(3)>.
6202
6203 =cut
6204 "
6205
6206 and generate_perl_prototype name style =
6207   (match fst style with
6208    | RErr -> ()
6209    | RBool n
6210    | RInt n
6211    | RInt64 n
6212    | RConstString n
6213    | RString n -> pr "$%s = " n
6214    | RStruct (n,_)
6215    | RHashtable n -> pr "%%%s = " n
6216    | RStringList n
6217    | RStructList (n,_) -> pr "@%s = " n
6218   );
6219   pr "$h->%s (" name;
6220   let comma = ref false in
6221   List.iter (
6222     fun arg ->
6223       if !comma then pr ", ";
6224       comma := true;
6225       match arg with
6226       | String n | OptString n | Bool n | Int n | FileIn n | FileOut n ->
6227           pr "$%s" n
6228       | StringList n ->
6229           pr "\\@%s" n
6230   ) (snd style);
6231   pr ");"
6232
6233 (* Generate Python C module. *)
6234 and generate_python_c () =
6235   generate_header CStyle LGPLv2;
6236
6237   pr "\
6238 #include <stdio.h>
6239 #include <stdlib.h>
6240 #include <assert.h>
6241
6242 #include <Python.h>
6243
6244 #include \"guestfs.h\"
6245
6246 typedef struct {
6247   PyObject_HEAD
6248   guestfs_h *g;
6249 } Pyguestfs_Object;
6250
6251 static guestfs_h *
6252 get_handle (PyObject *obj)
6253 {
6254   assert (obj);
6255   assert (obj != Py_None);
6256   return ((Pyguestfs_Object *) obj)->g;
6257 }
6258
6259 static PyObject *
6260 put_handle (guestfs_h *g)
6261 {
6262   assert (g);
6263   return
6264     PyCObject_FromVoidPtrAndDesc ((void *) g, (char *) \"guestfs_h\", NULL);
6265 }
6266
6267 /* This list should be freed (but not the strings) after use. */
6268 static const char **
6269 get_string_list (PyObject *obj)
6270 {
6271   int i, len;
6272   const char **r;
6273
6274   assert (obj);
6275
6276   if (!PyList_Check (obj)) {
6277     PyErr_SetString (PyExc_RuntimeError, \"expecting a list parameter\");
6278     return NULL;
6279   }
6280
6281   len = PyList_Size (obj);
6282   r = malloc (sizeof (char *) * (len+1));
6283   if (r == NULL) {
6284     PyErr_SetString (PyExc_RuntimeError, \"get_string_list: out of memory\");
6285     return NULL;
6286   }
6287
6288   for (i = 0; i < len; ++i)
6289     r[i] = PyString_AsString (PyList_GetItem (obj, i));
6290   r[len] = NULL;
6291
6292   return r;
6293 }
6294
6295 static PyObject *
6296 put_string_list (char * const * const argv)
6297 {
6298   PyObject *list;
6299   int argc, i;
6300
6301   for (argc = 0; argv[argc] != NULL; ++argc)
6302     ;
6303
6304   list = PyList_New (argc);
6305   for (i = 0; i < argc; ++i)
6306     PyList_SetItem (list, i, PyString_FromString (argv[i]));
6307
6308   return list;
6309 }
6310
6311 static PyObject *
6312 put_table (char * const * const argv)
6313 {
6314   PyObject *list, *item;
6315   int argc, i;
6316
6317   for (argc = 0; argv[argc] != NULL; ++argc)
6318     ;
6319
6320   list = PyList_New (argc >> 1);
6321   for (i = 0; i < argc; i += 2) {
6322     item = PyTuple_New (2);
6323     PyTuple_SetItem (item, 0, PyString_FromString (argv[i]));
6324     PyTuple_SetItem (item, 1, PyString_FromString (argv[i+1]));
6325     PyList_SetItem (list, i >> 1, item);
6326   }
6327
6328   return list;
6329 }
6330
6331 static void
6332 free_strings (char **argv)
6333 {
6334   int argc;
6335
6336   for (argc = 0; argv[argc] != NULL; ++argc)
6337     free (argv[argc]);
6338   free (argv);
6339 }
6340
6341 static PyObject *
6342 py_guestfs_create (PyObject *self, PyObject *args)
6343 {
6344   guestfs_h *g;
6345
6346   g = guestfs_create ();
6347   if (g == NULL) {
6348     PyErr_SetString (PyExc_RuntimeError,
6349                      \"guestfs.create: failed to allocate handle\");
6350     return NULL;
6351   }
6352   guestfs_set_error_handler (g, NULL, NULL);
6353   return put_handle (g);
6354 }
6355
6356 static PyObject *
6357 py_guestfs_close (PyObject *self, PyObject *args)
6358 {
6359   PyObject *py_g;
6360   guestfs_h *g;
6361
6362   if (!PyArg_ParseTuple (args, (char *) \"O:guestfs_close\", &py_g))
6363     return NULL;
6364   g = get_handle (py_g);
6365
6366   guestfs_close (g);
6367
6368   Py_INCREF (Py_None);
6369   return Py_None;
6370 }
6371
6372 ";
6373
6374   (* Structures, turned into Python dictionaries. *)
6375   List.iter (
6376     fun (typ, cols) ->
6377       pr "static PyObject *\n";
6378       pr "put_%s (struct guestfs_%s *%s)\n" typ typ typ;
6379       pr "{\n";
6380       pr "  PyObject *dict;\n";
6381       pr "\n";
6382       pr "  dict = PyDict_New ();\n";
6383       List.iter (
6384         function
6385         | name, FString ->
6386             pr "  PyDict_SetItemString (dict, \"%s\",\n" name;
6387             pr "                        PyString_FromString (%s->%s));\n"
6388               typ name
6389         | name, FUUID ->
6390             pr "  PyDict_SetItemString (dict, \"%s\",\n" name;
6391             pr "                        PyString_FromStringAndSize (%s->%s, 32));\n"
6392               typ name
6393         | name, (FBytes|FUInt64) ->
6394             pr "  PyDict_SetItemString (dict, \"%s\",\n" name;
6395             pr "                        PyLong_FromUnsignedLongLong (%s->%s));\n"
6396               typ name
6397         | name, FInt64 ->
6398             pr "  PyDict_SetItemString (dict, \"%s\",\n" name;
6399             pr "                        PyLong_FromLongLong (%s->%s));\n"
6400               typ name
6401         | name, FUInt32 ->
6402             pr "  PyDict_SetItemString (dict, \"%s\",\n" name;
6403             pr "                        PyLong_FromUnsignedLong (%s->%s));\n"
6404               typ name
6405         | name, FInt32 ->
6406             pr "  PyDict_SetItemString (dict, \"%s\",\n" name;
6407             pr "                        PyLong_FromLong (%s->%s));\n"
6408               typ name
6409         | name, FOptPercent ->
6410             pr "  if (%s->%s >= 0)\n" typ name;
6411             pr "    PyDict_SetItemString (dict, \"%s\",\n" name;
6412             pr "                          PyFloat_FromDouble ((double) %s->%s));\n"
6413               typ name;
6414             pr "  else {\n";
6415             pr "    Py_INCREF (Py_None);\n";
6416             pr "    PyDict_SetItemString (dict, \"%s\", Py_None);" name;
6417             pr "  }\n"
6418         | name, FChar ->
6419             pr "  PyDict_SetItemString (dict, \"%s\",\n" name;
6420             pr "                        PyString_FromStringAndSize (&dirent->%s, 1));\n" name
6421       ) cols;
6422       pr "  return dict;\n";
6423       pr "};\n";
6424       pr "\n";
6425
6426       pr "static PyObject *\n";
6427       pr "put_%s_list (struct guestfs_%s_list *%ss)\n" typ typ typ;
6428       pr "{\n";
6429       pr "  PyObject *list;\n";
6430       pr "  int i;\n";
6431       pr "\n";
6432       pr "  list = PyList_New (%ss->len);\n" typ;
6433       pr "  for (i = 0; i < %ss->len; ++i)\n" typ;
6434       pr "    PyList_SetItem (list, i, put_%s (&%ss->val[i]));\n" typ typ;
6435       pr "  return list;\n";
6436       pr "};\n";
6437       pr "\n"
6438   ) structs;
6439
6440   (* Python wrapper functions. *)
6441   List.iter (
6442     fun (name, style, _, _, _, _, _) ->
6443       pr "static PyObject *\n";
6444       pr "py_guestfs_%s (PyObject *self, PyObject *args)\n" name;
6445       pr "{\n";
6446
6447       pr "  PyObject *py_g;\n";
6448       pr "  guestfs_h *g;\n";
6449       pr "  PyObject *py_r;\n";
6450
6451       let error_code =
6452         match fst style with
6453         | RErr | RInt _ | RBool _ -> pr "  int r;\n"; "-1"
6454         | RInt64 _ -> pr "  int64_t r;\n"; "-1"
6455         | RConstString _ -> pr "  const char *r;\n"; "NULL"
6456         | RString _ -> pr "  char *r;\n"; "NULL"
6457         | RStringList _ | RHashtable _ -> pr "  char **r;\n"; "NULL"
6458         | RStruct (_, typ) -> pr "  struct guestfs_%s *r;\n" typ; "NULL"
6459         | RStructList (_, typ) ->
6460             pr "  struct guestfs_%s_list *r;\n" typ; "NULL" in
6461
6462       List.iter (
6463         function
6464         | String n | FileIn n | FileOut n -> pr "  const char *%s;\n" n
6465         | OptString n -> pr "  const char *%s;\n" n
6466         | StringList n ->
6467             pr "  PyObject *py_%s;\n" n;
6468             pr "  const char **%s;\n" n
6469         | Bool n -> pr "  int %s;\n" n
6470         | Int n -> pr "  int %s;\n" n
6471       ) (snd style);
6472
6473       pr "\n";
6474
6475       (* Convert the parameters. *)
6476       pr "  if (!PyArg_ParseTuple (args, (char *) \"O";
6477       List.iter (
6478         function
6479         | String _ | FileIn _ | FileOut _ -> pr "s"
6480         | OptString _ -> pr "z"
6481         | StringList _ -> pr "O"
6482         | Bool _ -> pr "i" (* XXX Python has booleans? *)
6483         | Int _ -> pr "i"
6484       ) (snd style);
6485       pr ":guestfs_%s\",\n" name;
6486       pr "                         &py_g";
6487       List.iter (
6488         function
6489         | String n | FileIn n | FileOut n -> pr ", &%s" n
6490         | OptString n -> pr ", &%s" n
6491         | StringList n -> pr ", &py_%s" n
6492         | Bool n -> pr ", &%s" n
6493         | Int n -> pr ", &%s" n
6494       ) (snd style);
6495
6496       pr "))\n";
6497       pr "    return NULL;\n";
6498
6499       pr "  g = get_handle (py_g);\n";
6500       List.iter (
6501         function
6502         | String _ | FileIn _ | FileOut _ | OptString _ | Bool _ | Int _ -> ()
6503         | StringList n ->
6504             pr "  %s = get_string_list (py_%s);\n" n n;
6505             pr "  if (!%s) return NULL;\n" n
6506       ) (snd style);
6507
6508       pr "\n";
6509
6510       pr "  r = guestfs_%s " name;
6511       generate_call_args ~handle:"g" (snd style);
6512       pr ";\n";
6513
6514       List.iter (
6515         function
6516         | String _ | FileIn _ | FileOut _ | OptString _ | Bool _ | Int _ -> ()
6517         | StringList n ->
6518             pr "  free (%s);\n" n
6519       ) (snd style);
6520
6521       pr "  if (r == %s) {\n" error_code;
6522       pr "    PyErr_SetString (PyExc_RuntimeError, guestfs_last_error (g));\n";
6523       pr "    return NULL;\n";
6524       pr "  }\n";
6525       pr "\n";
6526
6527       (match fst style with
6528        | RErr ->
6529            pr "  Py_INCREF (Py_None);\n";
6530            pr "  py_r = Py_None;\n"
6531        | RInt _
6532        | RBool _ -> pr "  py_r = PyInt_FromLong ((long) r);\n"
6533        | RInt64 _ -> pr "  py_r = PyLong_FromLongLong (r);\n"
6534        | RConstString _ -> pr "  py_r = PyString_FromString (r);\n"
6535        | RString _ ->
6536            pr "  py_r = PyString_FromString (r);\n";
6537            pr "  free (r);\n"
6538        | RStringList _ ->
6539            pr "  py_r = put_string_list (r);\n";
6540            pr "  free_strings (r);\n"
6541        | RStruct (_, typ) ->
6542            pr "  py_r = put_%s (r);\n" typ;
6543            pr "  guestfs_free_%s (r);\n" typ
6544        | RStructList (_, typ) ->
6545            pr "  py_r = put_%s_list (r);\n" typ;
6546            pr "  guestfs_free_%s_list (r);\n" typ
6547        | RHashtable n ->
6548            pr "  py_r = put_table (r);\n";
6549            pr "  free_strings (r);\n"
6550       );
6551
6552       pr "  return py_r;\n";
6553       pr "}\n";
6554       pr "\n"
6555   ) all_functions;
6556
6557   (* Table of functions. *)
6558   pr "static PyMethodDef methods[] = {\n";
6559   pr "  { (char *) \"create\", py_guestfs_create, METH_VARARGS, NULL },\n";
6560   pr "  { (char *) \"close\", py_guestfs_close, METH_VARARGS, NULL },\n";
6561   List.iter (
6562     fun (name, _, _, _, _, _, _) ->
6563       pr "  { (char *) \"%s\", py_guestfs_%s, METH_VARARGS, NULL },\n"
6564         name name
6565   ) all_functions;
6566   pr "  { NULL, NULL, 0, NULL }\n";
6567   pr "};\n";
6568   pr "\n";
6569
6570   (* Init function. *)
6571   pr "\
6572 void
6573 initlibguestfsmod (void)
6574 {
6575   static int initialized = 0;
6576
6577   if (initialized) return;
6578   Py_InitModule ((char *) \"libguestfsmod\", methods);
6579   initialized = 1;
6580 }
6581 "
6582
6583 (* Generate Python module. *)
6584 and generate_python_py () =
6585   generate_header HashStyle LGPLv2;
6586
6587   pr "\
6588 u\"\"\"Python bindings for libguestfs
6589
6590 import guestfs
6591 g = guestfs.GuestFS ()
6592 g.add_drive (\"guest.img\")
6593 g.launch ()
6594 g.wait_ready ()
6595 parts = g.list_partitions ()
6596
6597 The guestfs module provides a Python binding to the libguestfs API
6598 for examining and modifying virtual machine disk images.
6599
6600 Amongst the things this is good for: making batch configuration
6601 changes to guests, getting disk used/free statistics (see also:
6602 virt-df), migrating between virtualization systems (see also:
6603 virt-p2v), performing partial backups, performing partial guest
6604 clones, cloning guests and changing registry/UUID/hostname info, and
6605 much else besides.
6606
6607 Libguestfs uses Linux kernel and qemu code, and can access any type of
6608 guest filesystem that Linux and qemu can, including but not limited
6609 to: ext2/3/4, btrfs, FAT and NTFS, LVM, many different disk partition
6610 schemes, qcow, qcow2, vmdk.
6611
6612 Libguestfs provides ways to enumerate guest storage (eg. partitions,
6613 LVs, what filesystem is in each LV, etc.).  It can also run commands
6614 in the context of the guest.  Also you can access filesystems over FTP.
6615
6616 Errors which happen while using the API are turned into Python
6617 RuntimeError exceptions.
6618
6619 To create a guestfs handle you usually have to perform the following
6620 sequence of calls:
6621
6622 # Create the handle, call add_drive at least once, and possibly
6623 # several times if the guest has multiple block devices:
6624 g = guestfs.GuestFS ()
6625 g.add_drive (\"guest.img\")
6626
6627 # Launch the qemu subprocess and wait for it to become ready:
6628 g.launch ()
6629 g.wait_ready ()
6630
6631 # Now you can issue commands, for example:
6632 logvols = g.lvs ()
6633
6634 \"\"\"
6635
6636 import libguestfsmod
6637
6638 class GuestFS:
6639     \"\"\"Instances of this class are libguestfs API handles.\"\"\"
6640
6641     def __init__ (self):
6642         \"\"\"Create a new libguestfs handle.\"\"\"
6643         self._o = libguestfsmod.create ()
6644
6645     def __del__ (self):
6646         libguestfsmod.close (self._o)
6647
6648 ";
6649
6650   List.iter (
6651     fun (name, style, _, flags, _, _, longdesc) ->
6652       pr "    def %s " name;
6653       generate_call_args ~handle:"self" (snd style);
6654       pr ":\n";
6655
6656       if not (List.mem NotInDocs flags) then (
6657         let doc = replace_str longdesc "C<guestfs_" "C<g." in
6658         let doc =
6659           match fst style with
6660           | RErr | RInt _ | RInt64 _ | RBool _ | RConstString _
6661           | RString _ -> doc
6662           | RStringList _ ->
6663               doc ^ "\n\nThis function returns a list of strings."
6664           | RStruct (_, typ) ->
6665               doc ^ sprintf "\n\nThis function returns a dictionary, with keys matching the various fields in the guestfs_%s structure." typ
6666           | RStructList (_, typ) ->
6667               doc ^ sprintf "\n\nThis function returns a list of %ss.  Each %s is represented as a dictionary." typ typ
6668           | RHashtable _ ->
6669               doc ^ "\n\nThis function returns a dictionary." in
6670         let doc =
6671           if List.mem ProtocolLimitWarning flags then
6672             doc ^ "\n\n" ^ protocol_limit_warning
6673           else doc in
6674         let doc =
6675           if List.mem DangerWillRobinson flags then
6676             doc ^ "\n\n" ^ danger_will_robinson
6677           else doc in
6678         let doc = pod2text ~width:60 name doc in
6679         let doc = List.map (fun line -> replace_str line "\\" "\\\\") doc in
6680         let doc = String.concat "\n        " doc in
6681         pr "        u\"\"\"%s\"\"\"\n" doc;
6682       );
6683       pr "        return libguestfsmod.%s " name;
6684       generate_call_args ~handle:"self._o" (snd style);
6685       pr "\n";
6686       pr "\n";
6687   ) all_functions
6688
6689 (* Useful if you need the longdesc POD text as plain text.  Returns a
6690  * list of lines.
6691  *
6692  * Because this is very slow (the slowest part of autogeneration),
6693  * we memoize the results.
6694  *)
6695 and pod2text ~width name longdesc =
6696   let key = width, name, longdesc in
6697   try Hashtbl.find pod2text_memo key
6698   with Not_found ->
6699     let filename, chan = Filename.open_temp_file "gen" ".tmp" in
6700     fprintf chan "=head1 %s\n\n%s\n" name longdesc;
6701     close_out chan;
6702     let cmd = sprintf "pod2text -w %d %s" width (Filename.quote filename) in
6703     let chan = Unix.open_process_in cmd in
6704     let lines = ref [] in
6705     let rec loop i =
6706       let line = input_line chan in
6707       if i = 1 then             (* discard the first line of output *)
6708         loop (i+1)
6709       else (
6710         let line = triml line in
6711         lines := line :: !lines;
6712         loop (i+1)
6713       ) in
6714     let lines = try loop 1 with End_of_file -> List.rev !lines in
6715     Unix.unlink filename;
6716     (match Unix.close_process_in chan with
6717      | Unix.WEXITED 0 -> ()
6718      | Unix.WEXITED i ->
6719          failwithf "pod2text: process exited with non-zero status (%d)" i
6720      | Unix.WSIGNALED i | Unix.WSTOPPED i ->
6721          failwithf "pod2text: process signalled or stopped by signal %d" i
6722     );
6723     Hashtbl.add pod2text_memo key lines;
6724     let chan = open_out pod2text_memo_filename in
6725     output_value chan pod2text_memo;
6726     close_out chan;
6727     lines
6728
6729 (* Generate ruby bindings. *)
6730 and generate_ruby_c () =
6731   generate_header CStyle LGPLv2;
6732
6733   pr "\
6734 #include <stdio.h>
6735 #include <stdlib.h>
6736
6737 #include <ruby.h>
6738
6739 #include \"guestfs.h\"
6740
6741 #include \"extconf.h\"
6742
6743 /* For Ruby < 1.9 */
6744 #ifndef RARRAY_LEN
6745 #define RARRAY_LEN(r) (RARRAY((r))->len)
6746 #endif
6747
6748 static VALUE m_guestfs;                 /* guestfs module */
6749 static VALUE c_guestfs;                 /* guestfs_h handle */
6750 static VALUE e_Error;                   /* used for all errors */
6751
6752 static void ruby_guestfs_free (void *p)
6753 {
6754   if (!p) return;
6755   guestfs_close ((guestfs_h *) p);
6756 }
6757
6758 static VALUE ruby_guestfs_create (VALUE m)
6759 {
6760   guestfs_h *g;
6761
6762   g = guestfs_create ();
6763   if (!g)
6764     rb_raise (e_Error, \"failed to create guestfs handle\");
6765
6766   /* Don't print error messages to stderr by default. */
6767   guestfs_set_error_handler (g, NULL, NULL);
6768
6769   /* Wrap it, and make sure the close function is called when the
6770    * handle goes away.
6771    */
6772   return Data_Wrap_Struct (c_guestfs, NULL, ruby_guestfs_free, g);
6773 }
6774
6775 static VALUE ruby_guestfs_close (VALUE gv)
6776 {
6777   guestfs_h *g;
6778   Data_Get_Struct (gv, guestfs_h, g);
6779
6780   ruby_guestfs_free (g);
6781   DATA_PTR (gv) = NULL;
6782
6783   return Qnil;
6784 }
6785
6786 ";
6787
6788   List.iter (
6789     fun (name, style, _, _, _, _, _) ->
6790       pr "static VALUE ruby_guestfs_%s (VALUE gv" name;
6791       List.iter (fun arg -> pr ", VALUE %sv" (name_of_argt arg)) (snd style);
6792       pr ")\n";
6793       pr "{\n";
6794       pr "  guestfs_h *g;\n";
6795       pr "  Data_Get_Struct (gv, guestfs_h, g);\n";
6796       pr "  if (!g)\n";
6797       pr "    rb_raise (rb_eArgError, \"%%s: used handle after closing it\", \"%s\");\n"
6798         name;
6799       pr "\n";
6800
6801       List.iter (
6802         function
6803         | String n | FileIn n | FileOut n ->
6804             pr "  Check_Type (%sv, T_STRING);\n" n;
6805             pr "  const char *%s = StringValueCStr (%sv);\n" n n;
6806             pr "  if (!%s)\n" n;
6807             pr "    rb_raise (rb_eTypeError, \"expected string for parameter %%s of %%s\",\n";
6808             pr "              \"%s\", \"%s\");\n" n name
6809         | OptString n ->
6810             pr "  const char *%s = !NIL_P (%sv) ? StringValueCStr (%sv) : NULL;\n" n n n
6811         | StringList n ->
6812             pr "  char **%s;\n" n;
6813             pr "  Check_Type (%sv, T_ARRAY);\n" n;
6814             pr "  {\n";
6815             pr "    int i, len;\n";
6816             pr "    len = RARRAY_LEN (%sv);\n" n;
6817             pr "    %s = guestfs_safe_malloc (g, sizeof (char *) * (len+1));\n"
6818               n;
6819             pr "    for (i = 0; i < len; ++i) {\n";
6820             pr "      VALUE v = rb_ary_entry (%sv, i);\n" n;
6821             pr "      %s[i] = StringValueCStr (v);\n" n;
6822             pr "    }\n";
6823             pr "    %s[len] = NULL;\n" n;
6824             pr "  }\n";
6825         | Bool n ->
6826             pr "  int %s = RTEST (%sv);\n" n n
6827         | Int n ->
6828             pr "  int %s = NUM2INT (%sv);\n" n n
6829       ) (snd style);
6830       pr "\n";
6831
6832       let error_code =
6833         match fst style with
6834         | RErr | RInt _ | RBool _ -> pr "  int r;\n"; "-1"
6835         | RInt64 _ -> pr "  int64_t r;\n"; "-1"
6836         | RConstString _ -> pr "  const char *r;\n"; "NULL"
6837         | RString _ -> pr "  char *r;\n"; "NULL"
6838         | RStringList _ | RHashtable _ -> pr "  char **r;\n"; "NULL"
6839         | RStruct (_, typ) -> pr "  struct guestfs_%s *r;\n" typ; "NULL"
6840         | RStructList (_, typ) ->
6841             pr "  struct guestfs_%s_list *r;\n" typ; "NULL" in
6842       pr "\n";
6843
6844       pr "  r = guestfs_%s " name;
6845       generate_call_args ~handle:"g" (snd style);
6846       pr ";\n";
6847
6848       List.iter (
6849         function
6850         | String _ | FileIn _ | FileOut _ | OptString _ | Bool _ | Int _ -> ()
6851         | StringList n ->
6852             pr "  free (%s);\n" n
6853       ) (snd style);
6854
6855       pr "  if (r == %s)\n" error_code;
6856       pr "    rb_raise (e_Error, \"%%s\", guestfs_last_error (g));\n";
6857       pr "\n";
6858
6859       (match fst style with
6860        | RErr ->
6861            pr "  return Qnil;\n"
6862        | RInt _ | RBool _ ->
6863            pr "  return INT2NUM (r);\n"
6864        | RInt64 _ ->
6865            pr "  return ULL2NUM (r);\n"
6866        | RConstString _ ->
6867            pr "  return rb_str_new2 (r);\n";
6868        | RString _ ->
6869            pr "  VALUE rv = rb_str_new2 (r);\n";
6870            pr "  free (r);\n";
6871            pr "  return rv;\n";
6872        | RStringList _ ->
6873            pr "  int i, len = 0;\n";
6874            pr "  for (i = 0; r[i] != NULL; ++i) len++;\n";
6875            pr "  VALUE rv = rb_ary_new2 (len);\n";
6876            pr "  for (i = 0; r[i] != NULL; ++i) {\n";
6877            pr "    rb_ary_push (rv, rb_str_new2 (r[i]));\n";
6878            pr "    free (r[i]);\n";
6879            pr "  }\n";
6880            pr "  free (r);\n";
6881            pr "  return rv;\n"
6882        | RStruct (_, typ) ->
6883            let cols = cols_of_struct typ in
6884            generate_ruby_struct_code typ cols
6885        | RStructList (_, typ) ->
6886            let cols = cols_of_struct typ in
6887            generate_ruby_struct_list_code typ cols
6888        | RHashtable _ ->
6889            pr "  VALUE rv = rb_hash_new ();\n";
6890            pr "  int i;\n";
6891            pr "  for (i = 0; r[i] != NULL; i+=2) {\n";
6892            pr "    rb_hash_aset (rv, rb_str_new2 (r[i]), rb_str_new2 (r[i+1]));\n";
6893            pr "    free (r[i]);\n";
6894            pr "    free (r[i+1]);\n";
6895            pr "  }\n";
6896            pr "  free (r);\n";
6897            pr "  return rv;\n"
6898       );
6899
6900       pr "}\n";
6901       pr "\n"
6902   ) all_functions;
6903
6904   pr "\
6905 /* Initialize the module. */
6906 void Init__guestfs ()
6907 {
6908   m_guestfs = rb_define_module (\"Guestfs\");
6909   c_guestfs = rb_define_class_under (m_guestfs, \"Guestfs\", rb_cObject);
6910   e_Error = rb_define_class_under (m_guestfs, \"Error\", rb_eStandardError);
6911
6912   rb_define_module_function (m_guestfs, \"create\", ruby_guestfs_create, 0);
6913   rb_define_method (c_guestfs, \"close\", ruby_guestfs_close, 0);
6914
6915 ";
6916   (* Define the rest of the methods. *)
6917   List.iter (
6918     fun (name, style, _, _, _, _, _) ->
6919       pr "  rb_define_method (c_guestfs, \"%s\",\n" name;
6920       pr "        ruby_guestfs_%s, %d);\n" name (List.length (snd style))
6921   ) all_functions;
6922
6923   pr "}\n"
6924
6925 (* Ruby code to return a struct. *)
6926 and generate_ruby_struct_code typ cols =
6927   pr "  VALUE rv = rb_hash_new ();\n";
6928   List.iter (
6929     function
6930     | name, FString ->
6931         pr "  rb_hash_aset (rv, rb_str_new2 (\"%s\"), rb_str_new2 (r->%s));\n" name name
6932     | name, FUUID ->
6933         pr "  rb_hash_aset (rv, rb_str_new2 (\"%s\"), rb_str_new (r->%s, 32));\n" name name
6934     | name, (FBytes|FUInt64) ->
6935         pr "  rb_hash_aset (rv, rb_str_new2 (\"%s\"), ULL2NUM (r->%s));\n" name name
6936     | name, FInt64 ->
6937         pr "  rb_hash_aset (rv, rb_str_new2 (\"%s\"), LL2NUM (r->%s));\n" name name
6938     | name, FUInt32 ->
6939         pr "  rb_hash_aset (rv, rb_str_new2 (\"%s\"), UINT2NUM (r->%s));\n" name name
6940     | name, FInt32 ->
6941         pr "  rb_hash_aset (rv, rb_str_new2 (\"%s\"), INT2NUM (r->%s));\n" name name
6942     | name, FOptPercent ->
6943         pr "  rb_hash_aset (rv, rb_str_new2 (\"%s\"), rb_dbl2big (r->%s));\n" name name
6944     | name, FChar -> (* XXX wrong? *)
6945         pr "  rb_hash_aset (rv, rb_str_new2 (\"%s\"), ULL2NUM (r->%s));\n" name name
6946   ) cols;
6947   pr "  guestfs_free_%s (r);\n" typ;
6948   pr "  return rv;\n"
6949
6950 (* Ruby code to return a struct list. *)
6951 and generate_ruby_struct_list_code typ cols =
6952   pr "  VALUE rv = rb_ary_new2 (r->len);\n";
6953   pr "  int i;\n";
6954   pr "  for (i = 0; i < r->len; ++i) {\n";
6955   pr "    VALUE hv = rb_hash_new ();\n";
6956   List.iter (
6957     function
6958     | name, FString ->
6959         pr "    rb_hash_aset (hv, rb_str_new2 (\"%s\"), rb_str_new2 (r->val[i].%s));\n" name name
6960     | name, FUUID ->
6961         pr "    rb_hash_aset (hv, rb_str_new2 (\"%s\"), rb_str_new (r->val[i].%s, 32));\n" name name
6962     | name, (FBytes|FUInt64) ->
6963         pr "    rb_hash_aset (hv, rb_str_new2 (\"%s\"), ULL2NUM (r->val[i].%s));\n" name name
6964     | name, FInt64 ->
6965         pr "    rb_hash_aset (hv, rb_str_new2 (\"%s\"), LL2NUM (r->val[i].%s));\n" name name
6966     | name, FUInt32 ->
6967         pr "    rb_hash_aset (hv, rb_str_new2 (\"%s\"), UINT2NUM (r->val[i].%s));\n" name name
6968     | name, FInt32 ->
6969         pr "    rb_hash_aset (hv, rb_str_new2 (\"%s\"), INT2NUM (r->val[i].%s));\n" name name
6970     | name, FOptPercent ->
6971         pr "    rb_hash_aset (hv, rb_str_new2 (\"%s\"), rb_dbl2big (r->val[i].%s));\n" name name
6972     | name, FChar -> (* XXX wrong? *)
6973         pr "    rb_hash_aset (hv, rb_str_new2 (\"%s\"), ULL2NUM (r->val[i].%s));\n" name name
6974   ) cols;
6975   pr "    rb_ary_push (rv, hv);\n";
6976   pr "  }\n";
6977   pr "  guestfs_free_%s_list (r);\n" typ;
6978   pr "  return rv;\n"
6979
6980 (* Generate Java bindings GuestFS.java file. *)
6981 and generate_java_java () =
6982   generate_header CStyle LGPLv2;
6983
6984   pr "\
6985 package com.redhat.et.libguestfs;
6986
6987 import java.util.HashMap;
6988 import com.redhat.et.libguestfs.LibGuestFSException;
6989 import com.redhat.et.libguestfs.PV;
6990 import com.redhat.et.libguestfs.VG;
6991 import com.redhat.et.libguestfs.LV;
6992 import com.redhat.et.libguestfs.Stat;
6993 import com.redhat.et.libguestfs.StatVFS;
6994 import com.redhat.et.libguestfs.IntBool;
6995 import com.redhat.et.libguestfs.Dirent;
6996
6997 /**
6998  * The GuestFS object is a libguestfs handle.
6999  *
7000  * @author rjones
7001  */
7002 public class GuestFS {
7003   // Load the native code.
7004   static {
7005     System.loadLibrary (\"guestfs_jni\");
7006   }
7007
7008   /**
7009    * The native guestfs_h pointer.
7010    */
7011   long g;
7012
7013   /**
7014    * Create a libguestfs handle.
7015    *
7016    * @throws LibGuestFSException
7017    */
7018   public GuestFS () throws LibGuestFSException
7019   {
7020     g = _create ();
7021   }
7022   private native long _create () throws LibGuestFSException;
7023
7024   /**
7025    * Close a libguestfs handle.
7026    *
7027    * You can also leave handles to be collected by the garbage
7028    * collector, but this method ensures that the resources used
7029    * by the handle are freed up immediately.  If you call any
7030    * other methods after closing the handle, you will get an
7031    * exception.
7032    *
7033    * @throws LibGuestFSException
7034    */
7035   public void close () throws LibGuestFSException
7036   {
7037     if (g != 0)
7038       _close (g);
7039     g = 0;
7040   }
7041   private native void _close (long g) throws LibGuestFSException;
7042
7043   public void finalize () throws LibGuestFSException
7044   {
7045     close ();
7046   }
7047
7048 ";
7049
7050   List.iter (
7051     fun (name, style, _, flags, _, shortdesc, longdesc) ->
7052       if not (List.mem NotInDocs flags); then (
7053         let doc = replace_str longdesc "C<guestfs_" "C<g." in
7054         let doc =
7055           if List.mem ProtocolLimitWarning flags then
7056             doc ^ "\n\n" ^ protocol_limit_warning
7057           else doc in
7058         let doc =
7059           if List.mem DangerWillRobinson flags then
7060             doc ^ "\n\n" ^ danger_will_robinson
7061           else doc in
7062         let doc = pod2text ~width:60 name doc in
7063         let doc = List.map (            (* RHBZ#501883 *)
7064           function
7065           | "" -> "<p>"
7066           | nonempty -> nonempty
7067         ) doc in
7068         let doc = String.concat "\n   * " doc in
7069
7070         pr "  /**\n";
7071         pr "   * %s\n" shortdesc;
7072         pr "   * <p>\n";
7073         pr "   * %s\n" doc;
7074         pr "   * @throws LibGuestFSException\n";
7075         pr "   */\n";
7076         pr "  ";
7077       );
7078       generate_java_prototype ~public:true ~semicolon:false name style;
7079       pr "\n";
7080       pr "  {\n";
7081       pr "    if (g == 0)\n";
7082       pr "      throw new LibGuestFSException (\"%s: handle is closed\");\n"
7083         name;
7084       pr "    ";
7085       if fst style <> RErr then pr "return ";
7086       pr "_%s " name;
7087       generate_call_args ~handle:"g" (snd style);
7088       pr ";\n";
7089       pr "  }\n";
7090       pr "  ";
7091       generate_java_prototype ~privat:true ~native:true name style;
7092       pr "\n";
7093       pr "\n";
7094   ) all_functions;
7095
7096   pr "}\n"
7097
7098 and generate_java_prototype ?(public=false) ?(privat=false) ?(native=false)
7099     ?(semicolon=true) name style =
7100   if privat then pr "private ";
7101   if public then pr "public ";
7102   if native then pr "native ";
7103
7104   (* return type *)
7105   (match fst style with
7106    | RErr -> pr "void ";
7107    | RInt _ -> pr "int ";
7108    | RInt64 _ -> pr "long ";
7109    | RBool _ -> pr "boolean ";
7110    | RConstString _ | RString _ -> pr "String ";
7111    | RStringList _ -> pr "String[] ";
7112    | RStruct (_, typ) ->
7113        let name = java_name_of_struct typ in
7114        pr "%s " name;
7115    | RStructList (_, typ) ->
7116        let name = java_name_of_struct typ in
7117        pr "%s[] " name;
7118    | RHashtable _ -> pr "HashMap<String,String> ";
7119   );
7120
7121   if native then pr "_%s " name else pr "%s " name;
7122   pr "(";
7123   let needs_comma = ref false in
7124   if native then (
7125     pr "long g";
7126     needs_comma := true
7127   );
7128
7129   (* args *)
7130   List.iter (
7131     fun arg ->
7132       if !needs_comma then pr ", ";
7133       needs_comma := true;
7134
7135       match arg with
7136       | String n
7137       | OptString n
7138       | FileIn n
7139       | FileOut n ->
7140           pr "String %s" n
7141       | StringList n ->
7142           pr "String[] %s" n
7143       | Bool n ->
7144           pr "boolean %s" n
7145       | Int n ->
7146           pr "int %s" n
7147   ) (snd style);
7148
7149   pr ")\n";
7150   pr "    throws LibGuestFSException";
7151   if semicolon then pr ";"
7152
7153 and generate_java_struct jtyp cols =
7154   generate_header CStyle LGPLv2;
7155
7156   pr "\
7157 package com.redhat.et.libguestfs;
7158
7159 /**
7160  * Libguestfs %s structure.
7161  *
7162  * @author rjones
7163  * @see GuestFS
7164  */
7165 public class %s {
7166 " jtyp jtyp;
7167
7168   List.iter (
7169     function
7170     | name, FString
7171     | name, FUUID -> pr "  public String %s;\n" name
7172     | name, (FBytes|FUInt64|FInt64) -> pr "  public long %s;\n" name
7173     | name, (FUInt32|FInt32) -> pr "  public int %s;\n" name
7174     | name, FChar -> pr "  public char %s;\n" name
7175     | name, FOptPercent ->
7176         pr "  /* The next field is [0..100] or -1 meaning 'not present': */\n";
7177         pr "  public float %s;\n" name
7178   ) cols;
7179
7180   pr "}\n"
7181
7182 and generate_java_c () =
7183   generate_header CStyle LGPLv2;
7184
7185   pr "\
7186 #include <stdio.h>
7187 #include <stdlib.h>
7188 #include <string.h>
7189
7190 #include \"com_redhat_et_libguestfs_GuestFS.h\"
7191 #include \"guestfs.h\"
7192
7193 /* Note that this function returns.  The exception is not thrown
7194  * until after the wrapper function returns.
7195  */
7196 static void
7197 throw_exception (JNIEnv *env, const char *msg)
7198 {
7199   jclass cl;
7200   cl = (*env)->FindClass (env,
7201                           \"com/redhat/et/libguestfs/LibGuestFSException\");
7202   (*env)->ThrowNew (env, cl, msg);
7203 }
7204
7205 JNIEXPORT jlong JNICALL
7206 Java_com_redhat_et_libguestfs_GuestFS__1create
7207   (JNIEnv *env, jobject obj)
7208 {
7209   guestfs_h *g;
7210
7211   g = guestfs_create ();
7212   if (g == NULL) {
7213     throw_exception (env, \"GuestFS.create: failed to allocate handle\");
7214     return 0;
7215   }
7216   guestfs_set_error_handler (g, NULL, NULL);
7217   return (jlong) (long) g;
7218 }
7219
7220 JNIEXPORT void JNICALL
7221 Java_com_redhat_et_libguestfs_GuestFS__1close
7222   (JNIEnv *env, jobject obj, jlong jg)
7223 {
7224   guestfs_h *g = (guestfs_h *) (long) jg;
7225   guestfs_close (g);
7226 }
7227
7228 ";
7229
7230   List.iter (
7231     fun (name, style, _, _, _, _, _) ->
7232       pr "JNIEXPORT ";
7233       (match fst style with
7234        | RErr -> pr "void ";
7235        | RInt _ -> pr "jint ";
7236        | RInt64 _ -> pr "jlong ";
7237        | RBool _ -> pr "jboolean ";
7238        | RConstString _ | RString _ -> pr "jstring ";
7239        | RStruct _ | RHashtable _ ->
7240            pr "jobject ";
7241        | RStringList _ | RStructList _ ->
7242            pr "jobjectArray ";
7243       );
7244       pr "JNICALL\n";
7245       pr "Java_com_redhat_et_libguestfs_GuestFS_";
7246       pr "%s" (replace_str ("_" ^ name) "_" "_1");
7247       pr "\n";
7248       pr "  (JNIEnv *env, jobject obj, jlong jg";
7249       List.iter (
7250         function
7251         | String n
7252         | OptString n
7253         | FileIn n
7254         | FileOut n ->
7255             pr ", jstring j%s" n
7256         | StringList n ->
7257             pr ", jobjectArray j%s" n
7258         | Bool n ->
7259             pr ", jboolean j%s" n
7260         | Int n ->
7261             pr ", jint j%s" n
7262       ) (snd style);
7263       pr ")\n";
7264       pr "{\n";
7265       pr "  guestfs_h *g = (guestfs_h *) (long) jg;\n";
7266       let error_code, no_ret =
7267         match fst style with
7268         | RErr -> pr "  int r;\n"; "-1", ""
7269         | RBool _
7270         | RInt _ -> pr "  int r;\n"; "-1", "0"
7271         | RInt64 _ -> pr "  int64_t r;\n"; "-1", "0"
7272         | RConstString _ -> pr "  const char *r;\n"; "NULL", "NULL"
7273         | RString _ ->
7274             pr "  jstring jr;\n";
7275             pr "  char *r;\n"; "NULL", "NULL"
7276         | RStringList _ ->
7277             pr "  jobjectArray jr;\n";
7278             pr "  int r_len;\n";
7279             pr "  jclass cl;\n";
7280             pr "  jstring jstr;\n";
7281             pr "  char **r;\n"; "NULL", "NULL"
7282         | RStruct (_, typ) ->
7283             pr "  jobject jr;\n";
7284             pr "  jclass cl;\n";
7285             pr "  jfieldID fl;\n";
7286             pr "  struct guestfs_%s *r;\n" typ; "NULL", "NULL"
7287         | RStructList (_, typ) ->
7288             pr "  jobjectArray jr;\n";
7289             pr "  jclass cl;\n";
7290             pr "  jfieldID fl;\n";
7291             pr "  jobject jfl;\n";
7292             pr "  struct guestfs_%s_list *r;\n" typ; "NULL", "NULL"
7293         | RHashtable _ -> pr "  char **r;\n"; "NULL", "NULL" in
7294       List.iter (
7295         function
7296         | String n
7297         | OptString n
7298         | FileIn n
7299         | FileOut n ->
7300             pr "  const char *%s;\n" n
7301         | StringList n ->
7302             pr "  int %s_len;\n" n;
7303             pr "  const char **%s;\n" n
7304         | Bool n
7305         | Int n ->
7306             pr "  int %s;\n" n
7307       ) (snd style);
7308
7309       let needs_i =
7310         (match fst style with
7311          | RStringList _ | RStructList _ -> true
7312          | RErr | RBool _ | RInt _ | RInt64 _ | RConstString _
7313          | RString _ | RStruct _ | RHashtable _ -> false) ||
7314           List.exists (function StringList _ -> true | _ -> false) (snd style) in
7315       if needs_i then
7316         pr "  int i;\n";
7317
7318       pr "\n";
7319
7320       (* Get the parameters. *)
7321       List.iter (
7322         function
7323         | String n
7324         | FileIn n
7325         | FileOut n ->
7326             pr "  %s = (*env)->GetStringUTFChars (env, j%s, NULL);\n" n n
7327         | OptString n ->
7328             (* This is completely undocumented, but Java null becomes
7329              * a NULL parameter.
7330              *)
7331             pr "  %s = j%s ? (*env)->GetStringUTFChars (env, j%s, NULL) : NULL;\n" n n n
7332         | StringList n ->
7333             pr "  %s_len = (*env)->GetArrayLength (env, j%s);\n" n n;
7334             pr "  %s = guestfs_safe_malloc (g, sizeof (char *) * (%s_len+1));\n" n n;
7335             pr "  for (i = 0; i < %s_len; ++i) {\n" n;
7336             pr "    jobject o = (*env)->GetObjectArrayElement (env, j%s, i);\n"
7337               n;
7338             pr "    %s[i] = (*env)->GetStringUTFChars (env, o, NULL);\n" n;
7339             pr "  }\n";
7340             pr "  %s[%s_len] = NULL;\n" n n;
7341         | Bool n
7342         | Int n ->
7343             pr "  %s = j%s;\n" n n
7344       ) (snd style);
7345
7346       (* Make the call. *)
7347       pr "  r = guestfs_%s " name;
7348       generate_call_args ~handle:"g" (snd style);
7349       pr ";\n";
7350
7351       (* Release the parameters. *)
7352       List.iter (
7353         function
7354         | String n
7355         | FileIn n
7356         | FileOut n ->
7357             pr "  (*env)->ReleaseStringUTFChars (env, j%s, %s);\n" n n
7358         | OptString n ->
7359             pr "  if (j%s)\n" n;
7360             pr "    (*env)->ReleaseStringUTFChars (env, j%s, %s);\n" n n
7361         | StringList n ->
7362             pr "  for (i = 0; i < %s_len; ++i) {\n" n;
7363             pr "    jobject o = (*env)->GetObjectArrayElement (env, j%s, i);\n"
7364               n;
7365             pr "    (*env)->ReleaseStringUTFChars (env, o, %s[i]);\n" n;
7366             pr "  }\n";
7367             pr "  free (%s);\n" n
7368         | Bool n
7369         | Int n -> ()
7370       ) (snd style);
7371
7372       (* Check for errors. *)
7373       pr "  if (r == %s) {\n" error_code;
7374       pr "    throw_exception (env, guestfs_last_error (g));\n";
7375       pr "    return %s;\n" no_ret;
7376       pr "  }\n";
7377
7378       (* Return value. *)
7379       (match fst style with
7380        | RErr -> ()
7381        | RInt _ -> pr "  return (jint) r;\n"
7382        | RBool _ -> pr "  return (jboolean) r;\n"
7383        | RInt64 _ -> pr "  return (jlong) r;\n"
7384        | RConstString _ -> pr "  return (*env)->NewStringUTF (env, r);\n"
7385        | RString _ ->
7386            pr "  jr = (*env)->NewStringUTF (env, r);\n";
7387            pr "  free (r);\n";
7388            pr "  return jr;\n"
7389        | RStringList _ ->
7390            pr "  for (r_len = 0; r[r_len] != NULL; ++r_len) ;\n";
7391            pr "  cl = (*env)->FindClass (env, \"java/lang/String\");\n";
7392            pr "  jstr = (*env)->NewStringUTF (env, \"\");\n";
7393            pr "  jr = (*env)->NewObjectArray (env, r_len, cl, jstr);\n";
7394            pr "  for (i = 0; i < r_len; ++i) {\n";
7395            pr "    jstr = (*env)->NewStringUTF (env, r[i]);\n";
7396            pr "    (*env)->SetObjectArrayElement (env, jr, i, jstr);\n";
7397            pr "    free (r[i]);\n";
7398            pr "  }\n";
7399            pr "  free (r);\n";
7400            pr "  return jr;\n"
7401        | RStruct (_, typ) ->
7402            let jtyp = java_name_of_struct typ in
7403            let cols = cols_of_struct typ in
7404            generate_java_struct_return typ jtyp cols
7405        | RStructList (_, typ) ->
7406            let jtyp = java_name_of_struct typ in
7407            let cols = cols_of_struct typ in
7408            generate_java_struct_list_return typ jtyp cols
7409        | RHashtable _ ->
7410            (* XXX *)
7411            pr "  throw_exception (env, \"%s: internal error: please let us know how to make a Java HashMap from JNI bindings!\");\n" name;
7412            pr "  return NULL;\n"
7413       );
7414
7415       pr "}\n";
7416       pr "\n"
7417   ) all_functions
7418
7419 and generate_java_struct_return typ jtyp cols =
7420   pr "  cl = (*env)->FindClass (env, \"com/redhat/et/libguestfs/%s\");\n" jtyp;
7421   pr "  jr = (*env)->AllocObject (env, cl);\n";
7422   List.iter (
7423     function
7424     | name, FString ->
7425         pr "  fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
7426         pr "  (*env)->SetObjectField (env, jr, fl, (*env)->NewStringUTF (env, r->%s));\n" name;
7427     | name, FUUID ->
7428         pr "  {\n";
7429         pr "    char s[33];\n";
7430         pr "    memcpy (s, r->%s, 32);\n" name;
7431         pr "    s[32] = 0;\n";
7432         pr "    fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
7433         pr "    (*env)->SetObjectField (env, jr, fl, (*env)->NewStringUTF (env, s));\n";
7434         pr "  }\n";
7435     | name, (FBytes|FUInt64|FInt64) ->
7436         pr "  fl = (*env)->GetFieldID (env, cl, \"%s\", \"J\");\n" name;
7437         pr "  (*env)->SetLongField (env, jr, fl, r->%s);\n" name;
7438     | name, (FUInt32|FInt32) ->
7439         pr "  fl = (*env)->GetFieldID (env, cl, \"%s\", \"I\");\n" name;
7440         pr "  (*env)->SetLongField (env, jr, fl, r->%s);\n" name;
7441     | name, FOptPercent ->
7442         pr "  fl = (*env)->GetFieldID (env, cl, \"%s\", \"F\");\n" name;
7443         pr "  (*env)->SetFloatField (env, jr, fl, r->%s);\n" name;
7444     | name, FChar ->
7445         pr "  fl = (*env)->GetFieldID (env, cl, \"%s\", \"C\");\n" name;
7446         pr "  (*env)->SetLongField (env, jr, fl, r->%s);\n" name;
7447   ) cols;
7448   pr "  free (r);\n";
7449   pr "  return jr;\n"
7450
7451 and generate_java_struct_list_return typ jtyp cols =
7452   pr "  cl = (*env)->FindClass (env, \"com/redhat/et/libguestfs/%s\");\n" jtyp;
7453   pr "  jr = (*env)->NewObjectArray (env, r->len, cl, NULL);\n";
7454   pr "  for (i = 0; i < r->len; ++i) {\n";
7455   pr "    jfl = (*env)->AllocObject (env, cl);\n";
7456   List.iter (
7457     function
7458     | name, FString ->
7459         pr "    fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
7460         pr "    (*env)->SetObjectField (env, jfl, fl, (*env)->NewStringUTF (env, r->val[i].%s));\n" name;
7461     | name, FUUID ->
7462         pr "    {\n";
7463         pr "      char s[33];\n";
7464         pr "      memcpy (s, r->val[i].%s, 32);\n" name;
7465         pr "      s[32] = 0;\n";
7466         pr "      fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
7467         pr "      (*env)->SetObjectField (env, jfl, fl, (*env)->NewStringUTF (env, s));\n";
7468         pr "    }\n";
7469     | name, (FBytes|FUInt64|FInt64) ->
7470         pr "    fl = (*env)->GetFieldID (env, cl, \"%s\", \"J\");\n" name;
7471         pr "    (*env)->SetLongField (env, jfl, fl, r->val[i].%s);\n" name;
7472     | name, (FUInt32|FInt32) ->
7473         pr "    fl = (*env)->GetFieldID (env, cl, \"%s\", \"I\");\n" name;
7474         pr "    (*env)->SetLongField (env, jfl, fl, r->val[i].%s);\n" name;
7475     | name, FOptPercent ->
7476         pr "    fl = (*env)->GetFieldID (env, cl, \"%s\", \"F\");\n" name;
7477         pr "    (*env)->SetFloatField (env, jfl, fl, r->val[i].%s);\n" name;
7478     | name, FChar ->
7479         pr "    fl = (*env)->GetFieldID (env, cl, \"%s\", \"C\");\n" name;
7480         pr "    (*env)->SetLongField (env, jfl, fl, r->val[i].%s);\n" name;
7481   ) cols;
7482   pr "    (*env)->SetObjectArrayElement (env, jfl, i, jfl);\n";
7483   pr "  }\n";
7484   pr "  guestfs_free_%s_list (r);\n" typ;
7485   pr "  return jr;\n"
7486
7487 and generate_haskell_hs () =
7488   generate_header HaskellStyle LGPLv2;
7489
7490   (* XXX We only know how to generate partial FFI for Haskell
7491    * at the moment.  Please help out!
7492    *)
7493   let can_generate style =
7494     match style with
7495     | RErr, _
7496     | RInt _, _
7497     | RInt64 _, _ -> true
7498     | RBool _, _
7499     | RConstString _, _
7500     | RString _, _
7501     | RStringList _, _
7502     | RStruct _, _
7503     | RStructList _, _
7504     | RHashtable _, _ -> false in
7505
7506   pr "\
7507 {-# INCLUDE <guestfs.h> #-}
7508 {-# LANGUAGE ForeignFunctionInterface #-}
7509
7510 module Guestfs (
7511   create";
7512
7513   (* List out the names of the actions we want to export. *)
7514   List.iter (
7515     fun (name, style, _, _, _, _, _) ->
7516       if can_generate style then pr ",\n  %s" name
7517   ) all_functions;
7518
7519   pr "
7520   ) where
7521 import Foreign
7522 import Foreign.C
7523 import Foreign.C.Types
7524 import IO
7525 import Control.Exception
7526 import Data.Typeable
7527
7528 data GuestfsS = GuestfsS            -- represents the opaque C struct
7529 type GuestfsP = Ptr GuestfsS        -- guestfs_h *
7530 type GuestfsH = ForeignPtr GuestfsS -- guestfs_h * with attached finalizer
7531
7532 -- XXX define properly later XXX
7533 data PV = PV
7534 data VG = VG
7535 data LV = LV
7536 data IntBool = IntBool
7537 data Stat = Stat
7538 data StatVFS = StatVFS
7539 data Hashtable = Hashtable
7540
7541 foreign import ccall unsafe \"guestfs_create\" c_create
7542   :: IO GuestfsP
7543 foreign import ccall unsafe \"&guestfs_close\" c_close
7544   :: FunPtr (GuestfsP -> IO ())
7545 foreign import ccall unsafe \"guestfs_set_error_handler\" c_set_error_handler
7546   :: GuestfsP -> Ptr CInt -> Ptr CInt -> IO ()
7547
7548 create :: IO GuestfsH
7549 create = do
7550   p <- c_create
7551   c_set_error_handler p nullPtr nullPtr
7552   h <- newForeignPtr c_close p
7553   return h
7554
7555 foreign import ccall unsafe \"guestfs_last_error\" c_last_error
7556   :: GuestfsP -> IO CString
7557
7558 -- last_error :: GuestfsH -> IO (Maybe String)
7559 -- last_error h = do
7560 --   str <- withForeignPtr h (\\p -> c_last_error p)
7561 --   maybePeek peekCString str
7562
7563 last_error :: GuestfsH -> IO (String)
7564 last_error h = do
7565   str <- withForeignPtr h (\\p -> c_last_error p)
7566   if (str == nullPtr)
7567     then return \"no error\"
7568     else peekCString str
7569
7570 ";
7571
7572   (* Generate wrappers for each foreign function. *)
7573   List.iter (
7574     fun (name, style, _, _, _, _, _) ->
7575       if can_generate style then (
7576         pr "foreign import ccall unsafe \"guestfs_%s\" c_%s\n" name name;
7577         pr "  :: ";
7578         generate_haskell_prototype ~handle:"GuestfsP" style;
7579         pr "\n";
7580         pr "\n";
7581         pr "%s :: " name;
7582         generate_haskell_prototype ~handle:"GuestfsH" ~hs:true style;
7583         pr "\n";
7584         pr "%s %s = do\n" name
7585           (String.concat " " ("h" :: List.map name_of_argt (snd style)));
7586         pr "  r <- ";
7587         (* Convert pointer arguments using with* functions. *)
7588         List.iter (
7589           function
7590           | FileIn n
7591           | FileOut n
7592           | String n -> pr "withCString %s $ \\%s -> " n n
7593           | OptString n -> pr "maybeWith withCString %s $ \\%s -> " n n
7594           | StringList n -> pr "withMany withCString %s $ \\%s -> withArray0 nullPtr %s $ \\%s -> " n n n n
7595           | Bool _ | Int _ -> ()
7596         ) (snd style);
7597         (* Convert integer arguments. *)
7598         let args =
7599           List.map (
7600             function
7601             | Bool n -> sprintf "(fromBool %s)" n
7602             | Int n -> sprintf "(fromIntegral %s)" n
7603             | FileIn n | FileOut n | String n | OptString n | StringList n -> n
7604           ) (snd style) in
7605         pr "withForeignPtr h (\\p -> c_%s %s)\n" name
7606           (String.concat " " ("p" :: args));
7607         (match fst style with
7608          | RErr | RInt _ | RInt64 _ | RBool _ ->
7609              pr "  if (r == -1)\n";
7610              pr "    then do\n";
7611              pr "      err <- last_error h\n";
7612              pr "      fail err\n";
7613          | RConstString _ | RString _ | RStringList _ | RStruct _
7614          | RStructList _ | RHashtable _ ->
7615              pr "  if (r == nullPtr)\n";
7616              pr "    then do\n";
7617              pr "      err <- last_error h\n";
7618              pr "      fail err\n";
7619         );
7620         (match fst style with
7621          | RErr ->
7622              pr "    else return ()\n"
7623          | RInt _ ->
7624              pr "    else return (fromIntegral r)\n"
7625          | RInt64 _ ->
7626              pr "    else return (fromIntegral r)\n"
7627          | RBool _ ->
7628              pr "    else return (toBool r)\n"
7629          | RConstString _
7630          | RString _
7631          | RStringList _
7632          | RStruct _
7633          | RStructList _
7634          | RHashtable _ ->
7635              pr "    else return ()\n" (* XXXXXXXXXXXXXXXXXXXX *)
7636         );
7637         pr "\n";
7638       )
7639   ) all_functions
7640
7641 and generate_haskell_prototype ~handle ?(hs = false) style =
7642   pr "%s -> " handle;
7643   let string = if hs then "String" else "CString" in
7644   let int = if hs then "Int" else "CInt" in
7645   let bool = if hs then "Bool" else "CInt" in
7646   let int64 = if hs then "Integer" else "Int64" in
7647   List.iter (
7648     fun arg ->
7649       (match arg with
7650        | String _ -> pr "%s" string
7651        | OptString _ -> if hs then pr "Maybe String" else pr "CString"
7652        | StringList _ -> if hs then pr "[String]" else pr "Ptr CString"
7653        | Bool _ -> pr "%s" bool
7654        | Int _ -> pr "%s" int
7655        | FileIn _ -> pr "%s" string
7656        | FileOut _ -> pr "%s" string
7657       );
7658       pr " -> ";
7659   ) (snd style);
7660   pr "IO (";
7661   (match fst style with
7662    | RErr -> if not hs then pr "CInt"
7663    | RInt _ -> pr "%s" int
7664    | RInt64 _ -> pr "%s" int64
7665    | RBool _ -> pr "%s" bool
7666    | RConstString _ -> pr "%s" string
7667    | RString _ -> pr "%s" string
7668    | RStringList _ -> pr "[%s]" string
7669    | RStruct (_, typ) ->
7670        let name = java_name_of_struct typ in
7671        pr "%s" name
7672    | RStructList (_, typ) ->
7673        let name = java_name_of_struct typ in
7674        pr "[%s]" name
7675    | RHashtable _ -> pr "Hashtable"
7676   );
7677   pr ")"
7678
7679 and generate_bindtests () =
7680   generate_header CStyle LGPLv2;
7681
7682   pr "\
7683 #include <stdio.h>
7684 #include <stdlib.h>
7685 #include <inttypes.h>
7686 #include <string.h>
7687
7688 #include \"guestfs.h\"
7689 #include \"guestfs_protocol.h\"
7690
7691 #define error guestfs_error
7692 #define safe_calloc guestfs_safe_calloc
7693 #define safe_malloc guestfs_safe_malloc
7694
7695 static void
7696 print_strings (char * const* const argv)
7697 {
7698   int argc;
7699
7700   printf (\"[\");
7701   for (argc = 0; argv[argc] != NULL; ++argc) {
7702     if (argc > 0) printf (\", \");
7703     printf (\"\\\"%%s\\\"\", argv[argc]);
7704   }
7705   printf (\"]\\n\");
7706 }
7707
7708 /* The test0 function prints its parameters to stdout. */
7709 ";
7710
7711   let test0, tests =
7712     match test_functions with
7713     | [] -> assert false
7714     | test0 :: tests -> test0, tests in
7715
7716   let () =
7717     let (name, style, _, _, _, _, _) = test0 in
7718     generate_prototype ~extern:false ~semicolon:false ~newline:true
7719       ~handle:"g" ~prefix:"guestfs_" name style;
7720     pr "{\n";
7721     List.iter (
7722       function
7723       | String n
7724       | FileIn n
7725       | FileOut n -> pr "  printf (\"%%s\\n\", %s);\n" n
7726       | OptString n -> pr "  printf (\"%%s\\n\", %s ? %s : \"null\");\n" n n
7727       | StringList n -> pr "  print_strings (%s);\n" n
7728       | Bool n -> pr "  printf (\"%%s\\n\", %s ? \"true\" : \"false\");\n" n
7729       | Int n -> pr "  printf (\"%%d\\n\", %s);\n" n
7730     ) (snd style);
7731     pr "  /* Java changes stdout line buffering so we need this: */\n";
7732     pr "  fflush (stdout);\n";
7733     pr "  return 0;\n";
7734     pr "}\n";
7735     pr "\n" in
7736
7737   List.iter (
7738     fun (name, style, _, _, _, _, _) ->
7739       if String.sub name (String.length name - 3) 3 <> "err" then (
7740         pr "/* Test normal return. */\n";
7741         generate_prototype ~extern:false ~semicolon:false ~newline:true
7742           ~handle:"g" ~prefix:"guestfs_" name style;
7743         pr "{\n";
7744         (match fst style with
7745          | RErr ->
7746              pr "  return 0;\n"
7747          | RInt _ ->
7748              pr "  int r;\n";
7749              pr "  sscanf (val, \"%%d\", &r);\n";
7750              pr "  return r;\n"
7751          | RInt64 _ ->
7752              pr "  int64_t r;\n";
7753              pr "  sscanf (val, \"%%\" SCNi64, &r);\n";
7754              pr "  return r;\n"
7755          | RBool _ ->
7756              pr "  return strcmp (val, \"true\") == 0;\n"
7757          | RConstString _ ->
7758              (* Can't return the input string here.  Return a static
7759               * string so we ensure we get a segfault if the caller
7760               * tries to free it.
7761               *)
7762              pr "  return \"static string\";\n"
7763          | RString _ ->
7764              pr "  return strdup (val);\n"
7765          | RStringList _ ->
7766              pr "  char **strs;\n";
7767              pr "  int n, i;\n";
7768              pr "  sscanf (val, \"%%d\", &n);\n";
7769              pr "  strs = safe_malloc (g, (n+1) * sizeof (char *));\n";
7770              pr "  for (i = 0; i < n; ++i) {\n";
7771              pr "    strs[i] = safe_malloc (g, 16);\n";
7772              pr "    snprintf (strs[i], 16, \"%%d\", i);\n";
7773              pr "  }\n";
7774              pr "  strs[n] = NULL;\n";
7775              pr "  return strs;\n"
7776          | RStruct (_, typ) ->
7777              pr "  struct guestfs_%s *r;\n" typ;
7778              pr "  r = safe_calloc (g, sizeof *r, 1);\n";
7779              pr "  return r;\n"
7780          | RStructList (_, typ) ->
7781              pr "  struct guestfs_%s_list *r;\n" typ;
7782              pr "  r = safe_calloc (g, sizeof *r, 1);\n";
7783              pr "  sscanf (val, \"%%d\", &r->len);\n";
7784              pr "  r->val = safe_calloc (g, r->len, sizeof *r->val);\n";
7785              pr "  return r;\n"
7786          | RHashtable _ ->
7787              pr "  char **strs;\n";
7788              pr "  int n, i;\n";
7789              pr "  sscanf (val, \"%%d\", &n);\n";
7790              pr "  strs = safe_malloc (g, (n*2+1) * sizeof (*strs));\n";
7791              pr "  for (i = 0; i < n; ++i) {\n";
7792              pr "    strs[i*2] = safe_malloc (g, 16);\n";
7793              pr "    strs[i*2+1] = safe_malloc (g, 16);\n";
7794              pr "    snprintf (strs[i*2], 16, \"%%d\", i);\n";
7795              pr "    snprintf (strs[i*2+1], 16, \"%%d\", i);\n";
7796              pr "  }\n";
7797              pr "  strs[n*2] = NULL;\n";
7798              pr "  return strs;\n"
7799         );
7800         pr "}\n";
7801         pr "\n"
7802       ) else (
7803         pr "/* Test error return. */\n";
7804         generate_prototype ~extern:false ~semicolon:false ~newline:true
7805           ~handle:"g" ~prefix:"guestfs_" name style;
7806         pr "{\n";
7807         pr "  error (g, \"error\");\n";
7808         (match fst style with
7809          | RErr | RInt _ | RInt64 _ | RBool _ ->
7810              pr "  return -1;\n"
7811          | RConstString _
7812          | RString _ | RStringList _ | RStruct _
7813          | RStructList _
7814          | RHashtable _ ->
7815              pr "  return NULL;\n"
7816         );
7817         pr "}\n";
7818         pr "\n"
7819       )
7820   ) tests
7821
7822 and generate_ocaml_bindtests () =
7823   generate_header OCamlStyle GPLv2;
7824
7825   pr "\
7826 let () =
7827   let g = Guestfs.create () in
7828 ";
7829
7830   let mkargs args =
7831     String.concat " " (
7832       List.map (
7833         function
7834         | CallString s -> "\"" ^ s ^ "\""
7835         | CallOptString None -> "None"
7836         | CallOptString (Some s) -> sprintf "(Some \"%s\")" s
7837         | CallStringList xs ->
7838             "[|" ^ String.concat ";" (List.map (sprintf "\"%s\"") xs) ^ "|]"
7839         | CallInt i when i >= 0 -> string_of_int i
7840         | CallInt i (* when i < 0 *) -> "(" ^ string_of_int i ^ ")"
7841         | CallBool b -> string_of_bool b
7842       ) args
7843     )
7844   in
7845
7846   generate_lang_bindtests (
7847     fun f args -> pr "  Guestfs.%s g %s;\n" f (mkargs args)
7848   );
7849
7850   pr "print_endline \"EOF\"\n"
7851
7852 and generate_perl_bindtests () =
7853   pr "#!/usr/bin/perl -w\n";
7854   generate_header HashStyle GPLv2;
7855
7856   pr "\
7857 use strict;
7858
7859 use Sys::Guestfs;
7860
7861 my $g = Sys::Guestfs->new ();
7862 ";
7863
7864   let mkargs args =
7865     String.concat ", " (
7866       List.map (
7867         function
7868         | CallString s -> "\"" ^ s ^ "\""
7869         | CallOptString None -> "undef"
7870         | CallOptString (Some s) -> sprintf "\"%s\"" s
7871         | CallStringList xs ->
7872             "[" ^ String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "]"
7873         | CallInt i -> string_of_int i
7874         | CallBool b -> if b then "1" else "0"
7875       ) args
7876     )
7877   in
7878
7879   generate_lang_bindtests (
7880     fun f args -> pr "$g->%s (%s);\n" f (mkargs args)
7881   );
7882
7883   pr "print \"EOF\\n\"\n"
7884
7885 and generate_python_bindtests () =
7886   generate_header HashStyle GPLv2;
7887
7888   pr "\
7889 import guestfs
7890
7891 g = guestfs.GuestFS ()
7892 ";
7893
7894   let mkargs args =
7895     String.concat ", " (
7896       List.map (
7897         function
7898         | CallString s -> "\"" ^ s ^ "\""
7899         | CallOptString None -> "None"
7900         | CallOptString (Some s) -> sprintf "\"%s\"" s
7901         | CallStringList xs ->
7902             "[" ^ String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "]"
7903         | CallInt i -> string_of_int i
7904         | CallBool b -> if b then "1" else "0"
7905       ) args
7906     )
7907   in
7908
7909   generate_lang_bindtests (
7910     fun f args -> pr "g.%s (%s)\n" f (mkargs args)
7911   );
7912
7913   pr "print \"EOF\"\n"
7914
7915 and generate_ruby_bindtests () =
7916   generate_header HashStyle GPLv2;
7917
7918   pr "\
7919 require 'guestfs'
7920
7921 g = Guestfs::create()
7922 ";
7923
7924   let mkargs args =
7925     String.concat ", " (
7926       List.map (
7927         function
7928         | CallString s -> "\"" ^ s ^ "\""
7929         | CallOptString None -> "nil"
7930         | CallOptString (Some s) -> sprintf "\"%s\"" s
7931         | CallStringList xs ->
7932             "[" ^ String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "]"
7933         | CallInt i -> string_of_int i
7934         | CallBool b -> string_of_bool b
7935       ) args
7936     )
7937   in
7938
7939   generate_lang_bindtests (
7940     fun f args -> pr "g.%s(%s)\n" f (mkargs args)
7941   );
7942
7943   pr "print \"EOF\\n\"\n"
7944
7945 and generate_java_bindtests () =
7946   generate_header CStyle GPLv2;
7947
7948   pr "\
7949 import com.redhat.et.libguestfs.*;
7950
7951 public class Bindtests {
7952     public static void main (String[] argv)
7953     {
7954         try {
7955             GuestFS g = new GuestFS ();
7956 ";
7957
7958   let mkargs args =
7959     String.concat ", " (
7960       List.map (
7961         function
7962         | CallString s -> "\"" ^ s ^ "\""
7963         | CallOptString None -> "null"
7964         | CallOptString (Some s) -> sprintf "\"%s\"" s
7965         | CallStringList xs ->
7966             "new String[]{" ^
7967               String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "}"
7968         | CallInt i -> string_of_int i
7969         | CallBool b -> string_of_bool b
7970       ) args
7971     )
7972   in
7973
7974   generate_lang_bindtests (
7975     fun f args -> pr "            g.%s (%s);\n" f (mkargs args)
7976   );
7977
7978   pr "
7979             System.out.println (\"EOF\");
7980         }
7981         catch (Exception exn) {
7982             System.err.println (exn);
7983             System.exit (1);
7984         }
7985     }
7986 }
7987 "
7988
7989 and generate_haskell_bindtests () =
7990   generate_header HaskellStyle GPLv2;
7991
7992   pr "\
7993 module Bindtests where
7994 import qualified Guestfs
7995
7996 main = do
7997   g <- Guestfs.create
7998 ";
7999
8000   let mkargs args =
8001     String.concat " " (
8002       List.map (
8003         function
8004         | CallString s -> "\"" ^ s ^ "\""
8005         | CallOptString None -> "Nothing"
8006         | CallOptString (Some s) -> sprintf "(Just \"%s\")" s
8007         | CallStringList xs ->
8008             "[" ^ String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "]"
8009         | CallInt i when i < 0 -> "(" ^ string_of_int i ^ ")"
8010         | CallInt i -> string_of_int i
8011         | CallBool true -> "True"
8012         | CallBool false -> "False"
8013       ) args
8014     )
8015   in
8016
8017   generate_lang_bindtests (
8018     fun f args -> pr "  Guestfs.%s g %s\n" f (mkargs args)
8019   );
8020
8021   pr "  putStrLn \"EOF\"\n"
8022
8023 (* Language-independent bindings tests - we do it this way to
8024  * ensure there is parity in testing bindings across all languages.
8025  *)
8026 and generate_lang_bindtests call =
8027   call "test0" [CallString "abc"; CallOptString (Some "def");
8028                 CallStringList []; CallBool false;
8029                 CallInt 0; CallString "123"; CallString "456"];
8030   call "test0" [CallString "abc"; CallOptString None;
8031                 CallStringList []; CallBool false;
8032                 CallInt 0; CallString "123"; CallString "456"];
8033   call "test0" [CallString ""; CallOptString (Some "def");
8034                 CallStringList []; CallBool false;
8035                 CallInt 0; CallString "123"; CallString "456"];
8036   call "test0" [CallString ""; CallOptString (Some "");
8037                 CallStringList []; CallBool false;
8038                 CallInt 0; CallString "123"; CallString "456"];
8039   call "test0" [CallString "abc"; CallOptString (Some "def");
8040                 CallStringList ["1"]; CallBool false;
8041                 CallInt 0; CallString "123"; CallString "456"];
8042   call "test0" [CallString "abc"; CallOptString (Some "def");
8043                 CallStringList ["1"; "2"]; CallBool false;
8044                 CallInt 0; CallString "123"; CallString "456"];
8045   call "test0" [CallString "abc"; CallOptString (Some "def");
8046                 CallStringList ["1"]; CallBool true;
8047                 CallInt 0; CallString "123"; CallString "456"];
8048   call "test0" [CallString "abc"; CallOptString (Some "def");
8049                 CallStringList ["1"]; CallBool false;
8050                 CallInt (-1); CallString "123"; CallString "456"];
8051   call "test0" [CallString "abc"; CallOptString (Some "def");
8052                 CallStringList ["1"]; CallBool false;
8053                 CallInt (-2); CallString "123"; CallString "456"];
8054   call "test0" [CallString "abc"; CallOptString (Some "def");
8055                 CallStringList ["1"]; CallBool false;
8056                 CallInt 1; CallString "123"; CallString "456"];
8057   call "test0" [CallString "abc"; CallOptString (Some "def");
8058                 CallStringList ["1"]; CallBool false;
8059                 CallInt 2; CallString "123"; CallString "456"];
8060   call "test0" [CallString "abc"; CallOptString (Some "def");
8061                 CallStringList ["1"]; CallBool false;
8062                 CallInt 4095; CallString "123"; CallString "456"];
8063   call "test0" [CallString "abc"; CallOptString (Some "def");
8064                 CallStringList ["1"]; CallBool false;
8065                 CallInt 0; CallString ""; CallString ""]
8066
8067 (* XXX Add here tests of the return and error functions. *)
8068
8069 (* This is used to generate the src/MAX_PROC_NR file which
8070  * contains the maximum procedure number, a surrogate for the
8071  * ABI version number.  See src/Makefile.am for the details.
8072  *)
8073 and generate_max_proc_nr () =
8074   let proc_nrs = List.map (
8075     fun (_, _, proc_nr, _, _, _, _) -> proc_nr
8076   ) daemon_functions in
8077
8078   let max_proc_nr = List.fold_left max 0 proc_nrs in
8079
8080   pr "%d\n" max_proc_nr
8081
8082 let output_to filename =
8083   let filename_new = filename ^ ".new" in
8084   chan := open_out filename_new;
8085   let close () =
8086     close_out !chan;
8087     chan := stdout;
8088
8089     (* Is the new file different from the current file? *)
8090     if Sys.file_exists filename && files_equal filename filename_new then
8091       Unix.unlink filename_new          (* same, so skip it *)
8092     else (
8093       (* different, overwrite old one *)
8094       (try Unix.chmod filename 0o644 with Unix.Unix_error _ -> ());
8095       Unix.rename filename_new filename;
8096       Unix.chmod filename 0o444;
8097       printf "written %s\n%!" filename;
8098     )
8099   in
8100   close
8101
8102 (* Main program. *)
8103 let () =
8104   check_functions ();
8105
8106   if not (Sys.file_exists "HACKING") then (
8107     eprintf "\
8108 You are probably running this from the wrong directory.
8109 Run it from the top source directory using the command
8110   src/generator.ml
8111 ";
8112     exit 1
8113   );
8114
8115   let close = output_to "src/guestfs_protocol.x" in
8116   generate_xdr ();
8117   close ();
8118
8119   let close = output_to "src/guestfs-structs.h" in
8120   generate_structs_h ();
8121   close ();
8122
8123   let close = output_to "src/guestfs-actions.h" in
8124   generate_actions_h ();
8125   close ();
8126
8127   let close = output_to "src/guestfs-actions.c" in
8128   generate_client_actions ();
8129   close ();
8130
8131   let close = output_to "daemon/actions.h" in
8132   generate_daemon_actions_h ();
8133   close ();
8134
8135   let close = output_to "daemon/stubs.c" in
8136   generate_daemon_actions ();
8137   close ();
8138
8139   let close = output_to "daemon/names.c" in
8140   generate_daemon_names ();
8141   close ();
8142
8143   let close = output_to "capitests/tests.c" in
8144   generate_tests ();
8145   close ();
8146
8147   let close = output_to "src/guestfs-bindtests.c" in
8148   generate_bindtests ();
8149   close ();
8150
8151   let close = output_to "fish/cmds.c" in
8152   generate_fish_cmds ();
8153   close ();
8154
8155   let close = output_to "fish/completion.c" in
8156   generate_fish_completion ();
8157   close ();
8158
8159   let close = output_to "guestfs-structs.pod" in
8160   generate_structs_pod ();
8161   close ();
8162
8163   let close = output_to "guestfs-actions.pod" in
8164   generate_actions_pod ();
8165   close ();
8166
8167   let close = output_to "guestfish-actions.pod" in
8168   generate_fish_actions_pod ();
8169   close ();
8170
8171   let close = output_to "ocaml/guestfs.mli" in
8172   generate_ocaml_mli ();
8173   close ();
8174
8175   let close = output_to "ocaml/guestfs.ml" in
8176   generate_ocaml_ml ();
8177   close ();
8178
8179   let close = output_to "ocaml/guestfs_c_actions.c" in
8180   generate_ocaml_c ();
8181   close ();
8182
8183   let close = output_to "ocaml/bindtests.ml" in
8184   generate_ocaml_bindtests ();
8185   close ();
8186
8187   let close = output_to "perl/Guestfs.xs" in
8188   generate_perl_xs ();
8189   close ();
8190
8191   let close = output_to "perl/lib/Sys/Guestfs.pm" in
8192   generate_perl_pm ();
8193   close ();
8194
8195   let close = output_to "perl/bindtests.pl" in
8196   generate_perl_bindtests ();
8197   close ();
8198
8199   let close = output_to "python/guestfs-py.c" in
8200   generate_python_c ();
8201   close ();
8202
8203   let close = output_to "python/guestfs.py" in
8204   generate_python_py ();
8205   close ();
8206
8207   let close = output_to "python/bindtests.py" in
8208   generate_python_bindtests ();
8209   close ();
8210
8211   let close = output_to "ruby/ext/guestfs/_guestfs.c" in
8212   generate_ruby_c ();
8213   close ();
8214
8215   let close = output_to "ruby/bindtests.rb" in
8216   generate_ruby_bindtests ();
8217   close ();
8218
8219   let close = output_to "java/com/redhat/et/libguestfs/GuestFS.java" in
8220   generate_java_java ();
8221   close ();
8222
8223   List.iter (
8224     fun (typ, jtyp) ->
8225       let cols = cols_of_struct typ in
8226       let filename = sprintf "java/com/redhat/et/libguestfs/%s.java" jtyp in
8227       let close = output_to filename in
8228       generate_java_struct jtyp cols;
8229       close ();
8230   ) java_structs;
8231
8232   let close = output_to "java/com_redhat_et_libguestfs_GuestFS.c" in
8233   generate_java_c ();
8234   close ();
8235
8236   let close = output_to "java/Bindtests.java" in
8237   generate_java_bindtests ();
8238   close ();
8239
8240   let close = output_to "haskell/Guestfs.hs" in
8241   generate_haskell_hs ();
8242   close ();
8243
8244   let close = output_to "haskell/Bindtests.hs" in
8245   generate_haskell_bindtests ();
8246   close ();
8247
8248   let close = output_to "src/MAX_PROC_NR" in
8249   generate_max_proc_nr ();
8250   close ();
8251
8252   (* Always generate this file last, and unconditionally.  It's used
8253    * by the Makefile to know when we must re-run the generator.
8254    *)
8255   let chan = open_out "src/stamp-generator" in
8256   fprintf chan "1\n";
8257   close_out chan