3 * Copyright (C) 2009 Red Hat Inc.
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.
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.
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
20 (* This script generates a large amount of code and documentation for
21 * all the daemon actions.
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.
27 * After editing this file, run it (./src/generator.ml) to regenerate
28 * all the output files.
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]
40 type style = ret * args
42 (* "RErr" as a return value means an int used as a simple error
43 * indication, ie. 0 or -1.
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).
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).
56 (* "RBool" is a bool return value which can be true/false or
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.
65 | RConstString of string
66 (* "RString" and "RStringList" are caller-frees. *)
68 | RStringList of string
69 (* Some limited tuples are possible: *)
70 | RIntBool of string * string
71 (* LVM PVs, VGs and LVs. *)
78 (* Key-value pairs of untyped strings. Turns into a hashtable or
79 * dictionary in languages which support it. DON'T use this as a
80 * general "bucket" for results. Prefer a stronger typed return
81 * value if one is available, or write a custom struct. Don't use
82 * this if the list could potentially be very long, since it is
83 * inefficient. Keys should be unique. NULLs are not permitted.
85 | RHashtable of string
87 and args = argt list (* Function parameters, guestfs handle is implicit. *)
89 (* Note in future we should allow a "variable args" parameter as
90 * the final parameter, to allow commands like
91 * chmod mode file [file(s)...]
92 * This is not implemented yet, but many commands (such as chmod)
93 * are currently defined with the argument order keeping this future
94 * possibility in mind.
97 | String of string (* const char *name, cannot be NULL *)
98 | OptString of string (* const char *name, may be NULL *)
99 | StringList of string(* list of strings (each string cannot be NULL) *)
100 | Bool of string (* boolean *)
101 | Int of string (* int (smallish ints, signed, <= 31 bits) *)
102 (* These are treated as filenames (simple string parameters) in
103 * the C API and bindings. But in the RPC protocol, we transfer
104 * the actual file content up to or down from the daemon.
105 * FileIn: local machine -> daemon (in request)
106 * FileOut: daemon -> local machine (in reply)
107 * In guestfish (only), the special name "-" means read from
108 * stdin or write to stdout.
114 | ProtocolLimitWarning (* display warning about protocol size limits *)
115 | DangerWillRobinson (* flags particularly dangerous commands *)
116 | FishAlias of string (* provide an alias for this cmd in guestfish *)
117 | FishAction of string (* call this function in guestfish *)
118 | NotInFish (* do not export via guestfish *)
119 | NotInDocs (* do not add this function to documentation *)
121 let protocol_limit_warning =
122 "Because of the message protocol, there is a transfer limit
123 of somewhere between 2MB and 4MB. To transfer large files you should use
126 let danger_will_robinson =
127 "B<This command is dangerous. Without careful use you
128 can easily destroy all your data>."
130 (* You can supply zero or as many tests as you want per API call.
132 * Note that the test environment has 3 block devices, of size 500MB,
133 * 50MB and 10MB (respectively /dev/sda, /dev/sdb, /dev/sdc).
134 * Note for partitioning purposes, the 500MB device has 63 cylinders.
136 * To be able to run the tests in a reasonable amount of time,
137 * the virtual machine and block devices are reused between tests.
138 * So don't try testing kill_subprocess :-x
140 * Between each test we blockdev-setrw, umount-all, lvm-remove-all.
142 * If the appliance is running an older Linux kernel (eg. RHEL 5) then
143 * devices are named /dev/hda etc. To cope with this, the test suite
144 * adds some hairly logic to detect this case, and then automagically
145 * replaces all strings which match "/dev/sd.*" with "/dev/hd.*".
146 * When writing test cases you shouldn't have to worry about this
149 * Don't assume anything about the previous contents of the block
150 * devices. Use 'Init*' to create some initial scenarios.
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.
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)
163 type tests = (test_init * test_prereq * test) list
165 (* Run the command sequence and just expect nothing to fail. *)
167 (* Run the command sequence and expect the output of the final
168 * command to be the string.
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.
174 | TestOutputList of seq * string list
175 (* Run the command sequence and expect the output of the final
176 * command to be the integer.
178 | TestOutputInt of seq * int
179 (* Run the command sequence and expect the output of the final
180 * command to be a true value (!= 0 or != NULL).
182 | TestOutputTrue of seq
183 (* Run the command sequence and expect the output of the final
184 * command to be a false value (== 0 or == NULL, but not an error).
186 | TestOutputFalse of seq
187 (* Run the command sequence and expect the output of the final
188 * command to be a list of the given length (but don't care about
191 | TestOutputLength of seq * int
192 (* Run the command sequence and expect the output of the final
193 * command to be a structure.
195 | TestOutputStruct of seq * test_field_compare list
196 (* Run the command sequence and expect the final command (only)
199 | TestLastFail of seq
201 and test_field_compare =
202 | CompareWithInt of string * int
203 | CompareWithString of string * string
204 | CompareFieldsIntEq of string * string
205 | CompareFieldsStrEq of string * string
207 (* Test prerequisites. *)
209 (* Test always runs. *)
211 (* Test is currently disabled - eg. it fails, or it tests some
212 * unimplemented feature.
215 (* 'string' is some C code (a function body) that should return
216 * true or false. The test will run if the code returns true.
219 (* As for 'If' but the test runs _unless_ the code returns true. *)
222 (* Some initial scenarios for testing. *)
224 (* Do nothing, block devices could contain random stuff including
225 * LVM PVs, and some filesystems might be mounted. This is usually
229 (* Block devices are empty and no filesystems are mounted. *)
231 (* /dev/sda contains a single partition /dev/sda1, which is formatted
232 * as ext2, empty [except for lost+found] and mounted on /.
233 * /dev/sdb and /dev/sdc may have random content.
238 * /dev/sda1 (is a PV):
239 * /dev/VG/LV (size 8MB):
240 * formatted as ext2, empty [except for lost+found], mounted on /
241 * /dev/sdb and /dev/sdc may have random content.
245 (* Sequence of commands for testing. *)
247 and cmd = string list
249 (* Note about long descriptions: When referring to another
250 * action, use the format C<guestfs_other> (ie. the full name of
251 * the C function). This will be replaced as appropriate in other
254 * Apart from that, long descriptions are just perldoc paragraphs.
257 (* These test functions are used in the language binding tests. *)
259 let test_all_args = [
262 StringList "strlist";
269 let test_all_rets = [
270 (* except for RErr, which is tested thoroughly elsewhere *)
271 "test0rint", RInt "valout";
272 "test0rint64", RInt64 "valout";
273 "test0rbool", RBool "valout";
274 "test0rconststring", RConstString "valout";
275 "test0rstring", RString "valout";
276 "test0rstringlist", RStringList "valout";
277 "test0rintbool", RIntBool ("valout", "valout");
278 "test0rpvlist", RPVList "valout";
279 "test0rvglist", RVGList "valout";
280 "test0rlvlist", RLVList "valout";
281 "test0rstat", RStat "valout";
282 "test0rstatvfs", RStatVFS "valout";
283 "test0rhashtable", RHashtable "valout";
286 let test_functions = [
287 ("test0", (RErr, test_all_args), -1, [NotInFish; NotInDocs],
289 "internal test function - do not use",
291 This is an internal test function which is used to test whether
292 the automatically generated bindings can handle every possible
293 parameter type correctly.
295 It echos the contents of each parameter to stdout.
297 You probably don't want to call this function.");
301 [(name, (ret, [String "val"]), -1, [NotInFish; NotInDocs],
303 "internal test function - do not use",
305 This is an internal test function which is used to test whether
306 the automatically generated bindings can handle every possible
307 return type correctly.
309 It converts string C<val> to the return type.
311 You probably don't want to call this function.");
312 (name ^ "err", (ret, []), -1, [NotInFish; NotInDocs],
314 "internal test function - do not use",
316 This is an internal test function which is used to test whether
317 the automatically generated bindings can handle every possible
318 return type correctly.
320 This function always returns an error.
322 You probably don't want to call this function.")]
326 (* non_daemon_functions are any functions which don't get processed
327 * in the daemon, eg. functions for setting and getting local
328 * configuration values.
331 let non_daemon_functions = test_functions @ [
332 ("launch", (RErr, []), -1, [FishAlias "run"; FishAction "launch"],
334 "launch the qemu subprocess",
336 Internally libguestfs is implemented by running a virtual machine
339 You should call this after configuring the handle
340 (eg. adding drives) but before performing any actions.");
342 ("wait_ready", (RErr, []), -1, [NotInFish],
344 "wait until the qemu subprocess launches",
346 Internally libguestfs is implemented by running a virtual machine
349 You should call this after C<guestfs_launch> to wait for the launch
352 ("kill_subprocess", (RErr, []), -1, [],
354 "kill the qemu subprocess",
356 This kills the qemu subprocess. You should never need to call this.");
358 ("add_drive", (RErr, [String "filename"]), -1, [FishAlias "add"],
360 "add an image to examine or modify",
362 This function adds a virtual machine disk image C<filename> to the
363 guest. The first time you call this function, the disk appears as IDE
364 disk 0 (C</dev/sda>) in the guest, the second time as C</dev/sdb>, and
367 You don't necessarily need to be root when using libguestfs. However
368 you obviously do need sufficient permissions to access the filename
369 for whatever operations you want to perform (ie. read access if you
370 just want to read the image or write access if you want to modify the
373 This is equivalent to the qemu parameter C<-drive file=filename>.");
375 ("add_cdrom", (RErr, [String "filename"]), -1, [FishAlias "cdrom"],
377 "add a CD-ROM disk image to examine",
379 This function adds a virtual CD-ROM disk image to the guest.
381 This is equivalent to the qemu parameter C<-cdrom filename>.");
383 ("config", (RErr, [String "qemuparam"; OptString "qemuvalue"]), -1, [],
385 "add qemu parameters",
387 This can be used to add arbitrary qemu command line parameters
388 of the form C<-param value>. Actually it's not quite arbitrary - we
389 prevent you from setting some parameters which would interfere with
390 parameters that we use.
392 The first character of C<param> string must be a C<-> (dash).
394 C<value> can be NULL.");
396 ("set_qemu", (RErr, [String "qemu"]), -1, [FishAlias "qemu"],
398 "set the qemu binary",
400 Set the qemu binary that we will use.
402 The default is chosen when the library was compiled by the
405 You can also override this by setting the C<LIBGUESTFS_QEMU>
406 environment variable.
408 Setting C<qemu> to C<NULL> restores the default qemu binary.");
410 ("get_qemu", (RConstString "qemu", []), -1, [],
412 "get the qemu binary",
414 Return the current qemu binary.
416 This is always non-NULL. If it wasn't set already, then this will
417 return the default qemu binary name.");
419 ("set_path", (RErr, [String "path"]), -1, [FishAlias "path"],
421 "set the search path",
423 Set the path that libguestfs searches for kernel and initrd.img.
425 The default is C<$libdir/guestfs> unless overridden by setting
426 C<LIBGUESTFS_PATH> environment variable.
428 Setting C<path> to C<NULL> restores the default path.");
430 ("get_path", (RConstString "path", []), -1, [],
432 "get the search path",
434 Return the current search path.
436 This is always non-NULL. If it wasn't set already, then this will
437 return the default path.");
439 ("set_append", (RErr, [String "append"]), -1, [FishAlias "append"],
441 "add options to kernel command line",
443 This function is used to add additional options to the
444 guest kernel command line.
446 The default is C<NULL> unless overridden by setting
447 C<LIBGUESTFS_APPEND> environment variable.
449 Setting C<append> to C<NULL> means I<no> additional options
450 are passed (libguestfs always adds a few of its own).");
452 ("get_append", (RConstString "append", []), -1, [],
454 "get the additional kernel options",
456 Return the additional kernel options which are added to the
457 guest kernel command line.
459 If C<NULL> then no options are added.");
461 ("set_autosync", (RErr, [Bool "autosync"]), -1, [FishAlias "autosync"],
465 If C<autosync> is true, this enables autosync. Libguestfs will make a
466 best effort attempt to run C<guestfs_umount_all> followed by
467 C<guestfs_sync> when the handle is closed
468 (also if the program exits without closing handles).
470 This is disabled by default (except in guestfish where it is
471 enabled by default).");
473 ("get_autosync", (RBool "autosync", []), -1, [],
477 Get the autosync flag.");
479 ("set_verbose", (RErr, [Bool "verbose"]), -1, [FishAlias "verbose"],
483 If C<verbose> is true, this turns on verbose messages (to C<stderr>).
485 Verbose messages are disabled unless the environment variable
486 C<LIBGUESTFS_DEBUG> is defined and set to C<1>.");
488 ("get_verbose", (RBool "verbose", []), -1, [],
492 This returns the verbose messages flag.");
494 ("is_ready", (RBool "ready", []), -1, [],
496 "is ready to accept commands",
498 This returns true iff this handle is ready to accept commands
499 (in the C<READY> state).
501 For more information on states, see L<guestfs(3)>.");
503 ("is_config", (RBool "config", []), -1, [],
505 "is in configuration state",
507 This returns true iff this handle is being configured
508 (in the C<CONFIG> state).
510 For more information on states, see L<guestfs(3)>.");
512 ("is_launching", (RBool "launching", []), -1, [],
514 "is launching subprocess",
516 This returns true iff this handle is launching the subprocess
517 (in the C<LAUNCHING> state).
519 For more information on states, see L<guestfs(3)>.");
521 ("is_busy", (RBool "busy", []), -1, [],
523 "is busy processing a command",
525 This returns true iff this handle is busy processing a command
526 (in the C<BUSY> state).
528 For more information on states, see L<guestfs(3)>.");
530 ("get_state", (RInt "state", []), -1, [],
532 "get the current state",
534 This returns the current state as an opaque integer. This is
535 only useful for printing debug and internal error messages.
537 For more information on states, see L<guestfs(3)>.");
539 ("set_busy", (RErr, []), -1, [NotInFish],
543 This sets the state to C<BUSY>. This is only used when implementing
544 actions using the low-level API.
546 For more information on states, see L<guestfs(3)>.");
548 ("set_ready", (RErr, []), -1, [NotInFish],
550 "set state to ready",
552 This sets the state to C<READY>. This is only used when implementing
553 actions using the low-level API.
555 For more information on states, see L<guestfs(3)>.");
557 ("end_busy", (RErr, []), -1, [NotInFish],
559 "leave the busy state",
561 This sets the state to C<READY>, or if in C<CONFIG> then it leaves the
562 state as is. This is only used when implementing
563 actions using the low-level API.
565 For more information on states, see L<guestfs(3)>.");
569 (* daemon_functions are any functions which cause some action
570 * to take place in the daemon.
573 let daemon_functions = [
574 ("mount", (RErr, [String "device"; String "mountpoint"]), 1, [],
575 [InitEmpty, Always, TestOutput (
576 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
577 ["mkfs"; "ext2"; "/dev/sda1"];
578 ["mount"; "/dev/sda1"; "/"];
579 ["write_file"; "/new"; "new file contents"; "0"];
580 ["cat"; "/new"]], "new file contents")],
581 "mount a guest disk at a position in the filesystem",
583 Mount a guest disk at a position in the filesystem. Block devices
584 are named C</dev/sda>, C</dev/sdb> and so on, as they were added to
585 the guest. If those block devices contain partitions, they will have
586 the usual names (eg. C</dev/sda1>). Also LVM C</dev/VG/LV>-style
589 The rules are the same as for L<mount(2)>: A filesystem must
590 first be mounted on C</> before others can be mounted. Other
591 filesystems can only be mounted on directories which already
594 The mounted filesystem is writable, if we have sufficient permissions
595 on the underlying device.
597 The filesystem options C<sync> and C<noatime> are set with this
598 call, in order to improve reliability.");
600 ("sync", (RErr, []), 2, [],
601 [ InitEmpty, Always, TestRun [["sync"]]],
602 "sync disks, writes are flushed through to the disk image",
604 This syncs the disk, so that any writes are flushed through to the
605 underlying disk image.
607 You should always call this if you have modified a disk image, before
608 closing the handle.");
610 ("touch", (RErr, [String "path"]), 3, [],
611 [InitBasicFS, Always, TestOutputTrue (
613 ["exists"; "/new"]])],
614 "update file timestamps or create a new file",
616 Touch acts like the L<touch(1)> command. It can be used to
617 update the timestamps on a file, or, if the file does not exist,
618 to create a new zero-length file.");
620 ("cat", (RString "content", [String "path"]), 4, [ProtocolLimitWarning],
621 [InitBasicFS, Always, TestOutput (
622 [["write_file"; "/new"; "new file contents"; "0"];
623 ["cat"; "/new"]], "new file contents")],
624 "list the contents of a file",
626 Return the contents of the file named C<path>.
628 Note that this function cannot correctly handle binary files
629 (specifically, files containing C<\\0> character which is treated
630 as end of string). For those you need to use the C<guestfs_download>
631 function which has a more complex interface.");
633 ("ll", (RString "listing", [String "directory"]), 5, [],
634 [], (* XXX Tricky to test because it depends on the exact format
635 * of the 'ls -l' command, which changes between F10 and F11.
637 "list the files in a directory (long format)",
639 List the files in C<directory> (relative to the root directory,
640 there is no cwd) in the format of 'ls -la'.
642 This command is mostly useful for interactive sessions. It
643 is I<not> intended that you try to parse the output string.");
645 ("ls", (RStringList "listing", [String "directory"]), 6, [],
646 [InitBasicFS, Always, TestOutputList (
649 ["touch"; "/newest"];
650 ["ls"; "/"]], ["lost+found"; "new"; "newer"; "newest"])],
651 "list the files in a directory",
653 List the files in C<directory> (relative to the root directory,
654 there is no cwd). The '.' and '..' entries are not returned, but
655 hidden files are shown.
657 This command is mostly useful for interactive sessions. Programs
658 should probably use C<guestfs_readdir> instead.");
660 ("list_devices", (RStringList "devices", []), 7, [],
661 [InitEmpty, Always, TestOutputList (
662 [["list_devices"]], ["/dev/sda"; "/dev/sdb"; "/dev/sdc"])],
663 "list the block devices",
665 List all the block devices.
667 The full block device names are returned, eg. C</dev/sda>");
669 ("list_partitions", (RStringList "partitions", []), 8, [],
670 [InitBasicFS, Always, TestOutputList (
671 [["list_partitions"]], ["/dev/sda1"]);
672 InitEmpty, Always, TestOutputList (
673 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",10 ,20 ,"];
674 ["list_partitions"]], ["/dev/sda1"; "/dev/sda2"; "/dev/sda3"])],
675 "list the partitions",
677 List all the partitions detected on all block devices.
679 The full partition device names are returned, eg. C</dev/sda1>
681 This does not return logical volumes. For that you will need to
682 call C<guestfs_lvs>.");
684 ("pvs", (RStringList "physvols", []), 9, [],
685 [InitBasicFSonLVM, Always, TestOutputList (
686 [["pvs"]], ["/dev/sda1"]);
687 InitEmpty, Always, TestOutputList (
688 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",10 ,20 ,"];
689 ["pvcreate"; "/dev/sda1"];
690 ["pvcreate"; "/dev/sda2"];
691 ["pvcreate"; "/dev/sda3"];
692 ["pvs"]], ["/dev/sda1"; "/dev/sda2"; "/dev/sda3"])],
693 "list the LVM physical volumes (PVs)",
695 List all the physical volumes detected. This is the equivalent
696 of the L<pvs(8)> command.
698 This returns a list of just the device names that contain
699 PVs (eg. C</dev/sda2>).
701 See also C<guestfs_pvs_full>.");
703 ("vgs", (RStringList "volgroups", []), 10, [],
704 [InitBasicFSonLVM, Always, TestOutputList (
706 InitEmpty, Always, TestOutputList (
707 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",10 ,20 ,"];
708 ["pvcreate"; "/dev/sda1"];
709 ["pvcreate"; "/dev/sda2"];
710 ["pvcreate"; "/dev/sda3"];
711 ["vgcreate"; "VG1"; "/dev/sda1 /dev/sda2"];
712 ["vgcreate"; "VG2"; "/dev/sda3"];
713 ["vgs"]], ["VG1"; "VG2"])],
714 "list the LVM volume groups (VGs)",
716 List all the volumes groups detected. This is the equivalent
717 of the L<vgs(8)> command.
719 This returns a list of just the volume group names that were
720 detected (eg. C<VolGroup00>).
722 See also C<guestfs_vgs_full>.");
724 ("lvs", (RStringList "logvols", []), 11, [],
725 [InitBasicFSonLVM, Always, TestOutputList (
726 [["lvs"]], ["/dev/VG/LV"]);
727 InitEmpty, Always, TestOutputList (
728 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",10 ,20 ,"];
729 ["pvcreate"; "/dev/sda1"];
730 ["pvcreate"; "/dev/sda2"];
731 ["pvcreate"; "/dev/sda3"];
732 ["vgcreate"; "VG1"; "/dev/sda1 /dev/sda2"];
733 ["vgcreate"; "VG2"; "/dev/sda3"];
734 ["lvcreate"; "LV1"; "VG1"; "50"];
735 ["lvcreate"; "LV2"; "VG1"; "50"];
736 ["lvcreate"; "LV3"; "VG2"; "50"];
737 ["lvs"]], ["/dev/VG1/LV1"; "/dev/VG1/LV2"; "/dev/VG2/LV3"])],
738 "list the LVM logical volumes (LVs)",
740 List all the logical volumes detected. This is the equivalent
741 of the L<lvs(8)> command.
743 This returns a list of the logical volume device names
744 (eg. C</dev/VolGroup00/LogVol00>).
746 See also C<guestfs_lvs_full>.");
748 ("pvs_full", (RPVList "physvols", []), 12, [],
749 [], (* XXX how to test? *)
750 "list the LVM physical volumes (PVs)",
752 List all the physical volumes detected. This is the equivalent
753 of the L<pvs(8)> command. The \"full\" version includes all fields.");
755 ("vgs_full", (RVGList "volgroups", []), 13, [],
756 [], (* XXX how to test? *)
757 "list the LVM volume groups (VGs)",
759 List all the volumes groups detected. This is the equivalent
760 of the L<vgs(8)> command. The \"full\" version includes all fields.");
762 ("lvs_full", (RLVList "logvols", []), 14, [],
763 [], (* XXX how to test? *)
764 "list the LVM logical volumes (LVs)",
766 List all the logical volumes detected. This is the equivalent
767 of the L<lvs(8)> command. The \"full\" version includes all fields.");
769 ("read_lines", (RStringList "lines", [String "path"]), 15, [],
770 [InitBasicFS, Always, TestOutputList (
771 [["write_file"; "/new"; "line1\r\nline2\nline3"; "0"];
772 ["read_lines"; "/new"]], ["line1"; "line2"; "line3"]);
773 InitBasicFS, Always, TestOutputList (
774 [["write_file"; "/new"; ""; "0"];
775 ["read_lines"; "/new"]], [])],
776 "read file as lines",
778 Return the contents of the file named C<path>.
780 The file contents are returned as a list of lines. Trailing
781 C<LF> and C<CRLF> character sequences are I<not> returned.
783 Note that this function cannot correctly handle binary files
784 (specifically, files containing C<\\0> character which is treated
785 as end of line). For those you need to use the C<guestfs_read_file>
786 function which has a more complex interface.");
788 ("aug_init", (RErr, [String "root"; Int "flags"]), 16, [],
789 [], (* XXX Augeas code needs tests. *)
790 "create a new Augeas handle",
792 Create a new Augeas handle for editing configuration files.
793 If there was any previous Augeas handle associated with this
794 guestfs session, then it is closed.
796 You must call this before using any other C<guestfs_aug_*>
799 C<root> is the filesystem root. C<root> must not be NULL,
802 The flags are the same as the flags defined in
803 E<lt>augeas.hE<gt>, the logical I<or> of the following
808 =item C<AUG_SAVE_BACKUP> = 1
810 Keep the original file with a C<.augsave> extension.
812 =item C<AUG_SAVE_NEWFILE> = 2
814 Save changes into a file with extension C<.augnew>, and
815 do not overwrite original. Overrides C<AUG_SAVE_BACKUP>.
817 =item C<AUG_TYPE_CHECK> = 4
819 Typecheck lenses (can be expensive).
821 =item C<AUG_NO_STDINC> = 8
823 Do not use standard load path for modules.
825 =item C<AUG_SAVE_NOOP> = 16
827 Make save a no-op, just record what would have been changed.
829 =item C<AUG_NO_LOAD> = 32
831 Do not load the tree in C<guestfs_aug_init>.
835 To close the handle, you can call C<guestfs_aug_close>.
837 To find out more about Augeas, see L<http://augeas.net/>.");
839 ("aug_close", (RErr, []), 26, [],
840 [], (* XXX Augeas code needs tests. *)
841 "close the current Augeas handle",
843 Close the current Augeas handle and free up any resources
844 used by it. After calling this, you have to call
845 C<guestfs_aug_init> again before you can use any other
848 ("aug_defvar", (RInt "nrnodes", [String "name"; OptString "expr"]), 17, [],
849 [], (* XXX Augeas code needs tests. *)
850 "define an Augeas variable",
852 Defines an Augeas variable C<name> whose value is the result
853 of evaluating C<expr>. If C<expr> is NULL, then C<name> is
856 On success this returns the number of nodes in C<expr>, or
857 C<0> if C<expr> evaluates to something which is not a nodeset.");
859 ("aug_defnode", (RIntBool ("nrnodes", "created"), [String "name"; String "expr"; String "val"]), 18, [],
860 [], (* XXX Augeas code needs tests. *)
861 "define an Augeas node",
863 Defines a variable C<name> whose value is the result of
866 If C<expr> evaluates to an empty nodeset, a node is created,
867 equivalent to calling C<guestfs_aug_set> C<expr>, C<value>.
868 C<name> will be the nodeset containing that single node.
870 On success this returns a pair containing the
871 number of nodes in the nodeset, and a boolean flag
872 if a node was created.");
874 ("aug_get", (RString "val", [String "path"]), 19, [],
875 [], (* XXX Augeas code needs tests. *)
876 "look up the value of an Augeas path",
878 Look up the value associated with C<path>. If C<path>
879 matches exactly one node, the C<value> is returned.");
881 ("aug_set", (RErr, [String "path"; String "val"]), 20, [],
882 [], (* XXX Augeas code needs tests. *)
883 "set Augeas path to value",
885 Set the value associated with C<path> to C<value>.");
887 ("aug_insert", (RErr, [String "path"; String "label"; Bool "before"]), 21, [],
888 [], (* XXX Augeas code needs tests. *)
889 "insert a sibling Augeas node",
891 Create a new sibling C<label> for C<path>, inserting it into
892 the tree before or after C<path> (depending on the boolean
895 C<path> must match exactly one existing node in the tree, and
896 C<label> must be a label, ie. not contain C</>, C<*> or end
897 with a bracketed index C<[N]>.");
899 ("aug_rm", (RInt "nrnodes", [String "path"]), 22, [],
900 [], (* XXX Augeas code needs tests. *)
901 "remove an Augeas path",
903 Remove C<path> and all of its children.
905 On success this returns the number of entries which were removed.");
907 ("aug_mv", (RErr, [String "src"; String "dest"]), 23, [],
908 [], (* XXX Augeas code needs tests. *)
911 Move the node C<src> to C<dest>. C<src> must match exactly
912 one node. C<dest> is overwritten if it exists.");
914 ("aug_match", (RStringList "matches", [String "path"]), 24, [],
915 [], (* XXX Augeas code needs tests. *)
916 "return Augeas nodes which match path",
918 Returns a list of paths which match the path expression C<path>.
919 The returned paths are sufficiently qualified so that they match
920 exactly one node in the current tree.");
922 ("aug_save", (RErr, []), 25, [],
923 [], (* XXX Augeas code needs tests. *)
924 "write all pending Augeas changes to disk",
926 This writes all pending changes to disk.
928 The flags which were passed to C<guestfs_aug_init> affect exactly
929 how files are saved.");
931 ("aug_load", (RErr, []), 27, [],
932 [], (* XXX Augeas code needs tests. *)
933 "load files into the tree",
935 Load files into the tree.
937 See C<aug_load> in the Augeas documentation for the full gory
940 ("aug_ls", (RStringList "matches", [String "path"]), 28, [],
941 [], (* XXX Augeas code needs tests. *)
942 "list Augeas nodes under a path",
944 This is just a shortcut for listing C<guestfs_aug_match>
945 C<path/*> and sorting the resulting nodes into alphabetical order.");
947 ("rm", (RErr, [String "path"]), 29, [],
948 [InitBasicFS, Always, TestRun
951 InitBasicFS, Always, TestLastFail
953 InitBasicFS, Always, TestLastFail
958 Remove the single file C<path>.");
960 ("rmdir", (RErr, [String "path"]), 30, [],
961 [InitBasicFS, Always, TestRun
964 InitBasicFS, Always, TestLastFail
966 InitBasicFS, Always, TestLastFail
969 "remove a directory",
971 Remove the single directory C<path>.");
973 ("rm_rf", (RErr, [String "path"]), 31, [],
974 [InitBasicFS, Always, TestOutputFalse
976 ["mkdir"; "/new/foo"];
977 ["touch"; "/new/foo/bar"];
979 ["exists"; "/new"]]],
980 "remove a file or directory recursively",
982 Remove the file or directory C<path>, recursively removing the
983 contents if its a directory. This is like the C<rm -rf> shell
986 ("mkdir", (RErr, [String "path"]), 32, [],
987 [InitBasicFS, Always, TestOutputTrue
990 InitBasicFS, Always, TestLastFail
991 [["mkdir"; "/new/foo/bar"]]],
992 "create a directory",
994 Create a directory named C<path>.");
996 ("mkdir_p", (RErr, [String "path"]), 33, [],
997 [InitBasicFS, Always, TestOutputTrue
998 [["mkdir_p"; "/new/foo/bar"];
999 ["is_dir"; "/new/foo/bar"]];
1000 InitBasicFS, Always, TestOutputTrue
1001 [["mkdir_p"; "/new/foo/bar"];
1002 ["is_dir"; "/new/foo"]];
1003 InitBasicFS, Always, TestOutputTrue
1004 [["mkdir_p"; "/new/foo/bar"];
1005 ["is_dir"; "/new"]];
1006 (* Regression tests for RHBZ#503133: *)
1007 InitBasicFS, Always, TestRun
1009 ["mkdir_p"; "/new"]];
1010 InitBasicFS, Always, TestLastFail
1012 ["mkdir_p"; "/new"]]],
1013 "create a directory and parents",
1015 Create a directory named C<path>, creating any parent directories
1016 as necessary. This is like the C<mkdir -p> shell command.");
1018 ("chmod", (RErr, [Int "mode"; String "path"]), 34, [],
1019 [], (* XXX Need stat command to test *)
1022 Change the mode (permissions) of C<path> to C<mode>. Only
1023 numeric modes are supported.");
1025 ("chown", (RErr, [Int "owner"; Int "group"; String "path"]), 35, [],
1026 [], (* XXX Need stat command to test *)
1027 "change file owner and group",
1029 Change the file owner to C<owner> and group to C<group>.
1031 Only numeric uid and gid are supported. If you want to use
1032 names, you will need to locate and parse the password file
1033 yourself (Augeas support makes this relatively easy).");
1035 ("exists", (RBool "existsflag", [String "path"]), 36, [],
1036 [InitBasicFS, Always, TestOutputTrue (
1038 ["exists"; "/new"]]);
1039 InitBasicFS, Always, TestOutputTrue (
1041 ["exists"; "/new"]])],
1042 "test if file or directory exists",
1044 This returns C<true> if and only if there is a file, directory
1045 (or anything) with the given C<path> name.
1047 See also C<guestfs_is_file>, C<guestfs_is_dir>, C<guestfs_stat>.");
1049 ("is_file", (RBool "fileflag", [String "path"]), 37, [],
1050 [InitBasicFS, Always, TestOutputTrue (
1052 ["is_file"; "/new"]]);
1053 InitBasicFS, Always, TestOutputFalse (
1055 ["is_file"; "/new"]])],
1056 "test if file exists",
1058 This returns C<true> if and only if there is a file
1059 with the given C<path> name. Note that it returns false for
1060 other objects like directories.
1062 See also C<guestfs_stat>.");
1064 ("is_dir", (RBool "dirflag", [String "path"]), 38, [],
1065 [InitBasicFS, Always, TestOutputFalse (
1067 ["is_dir"; "/new"]]);
1068 InitBasicFS, Always, TestOutputTrue (
1070 ["is_dir"; "/new"]])],
1071 "test if file exists",
1073 This returns C<true> if and only if there is a directory
1074 with the given C<path> name. Note that it returns false for
1075 other objects like files.
1077 See also C<guestfs_stat>.");
1079 ("pvcreate", (RErr, [String "device"]), 39, [],
1080 [InitEmpty, Always, TestOutputList (
1081 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",10 ,20 ,"];
1082 ["pvcreate"; "/dev/sda1"];
1083 ["pvcreate"; "/dev/sda2"];
1084 ["pvcreate"; "/dev/sda3"];
1085 ["pvs"]], ["/dev/sda1"; "/dev/sda2"; "/dev/sda3"])],
1086 "create an LVM physical volume",
1088 This creates an LVM physical volume on the named C<device>,
1089 where C<device> should usually be a partition name such
1092 ("vgcreate", (RErr, [String "volgroup"; StringList "physvols"]), 40, [],
1093 [InitEmpty, Always, TestOutputList (
1094 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",10 ,20 ,"];
1095 ["pvcreate"; "/dev/sda1"];
1096 ["pvcreate"; "/dev/sda2"];
1097 ["pvcreate"; "/dev/sda3"];
1098 ["vgcreate"; "VG1"; "/dev/sda1 /dev/sda2"];
1099 ["vgcreate"; "VG2"; "/dev/sda3"];
1100 ["vgs"]], ["VG1"; "VG2"])],
1101 "create an LVM volume group",
1103 This creates an LVM volume group called C<volgroup>
1104 from the non-empty list of physical volumes C<physvols>.");
1106 ("lvcreate", (RErr, [String "logvol"; String "volgroup"; Int "mbytes"]), 41, [],
1107 [InitEmpty, Always, TestOutputList (
1108 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",10 ,20 ,"];
1109 ["pvcreate"; "/dev/sda1"];
1110 ["pvcreate"; "/dev/sda2"];
1111 ["pvcreate"; "/dev/sda3"];
1112 ["vgcreate"; "VG1"; "/dev/sda1 /dev/sda2"];
1113 ["vgcreate"; "VG2"; "/dev/sda3"];
1114 ["lvcreate"; "LV1"; "VG1"; "50"];
1115 ["lvcreate"; "LV2"; "VG1"; "50"];
1116 ["lvcreate"; "LV3"; "VG2"; "50"];
1117 ["lvcreate"; "LV4"; "VG2"; "50"];
1118 ["lvcreate"; "LV5"; "VG2"; "50"];
1120 ["/dev/VG1/LV1"; "/dev/VG1/LV2";
1121 "/dev/VG2/LV3"; "/dev/VG2/LV4"; "/dev/VG2/LV5"])],
1122 "create an LVM volume group",
1124 This creates an LVM volume group called C<logvol>
1125 on the volume group C<volgroup>, with C<size> megabytes.");
1127 ("mkfs", (RErr, [String "fstype"; String "device"]), 42, [],
1128 [InitEmpty, Always, TestOutput (
1129 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1130 ["mkfs"; "ext2"; "/dev/sda1"];
1131 ["mount"; "/dev/sda1"; "/"];
1132 ["write_file"; "/new"; "new file contents"; "0"];
1133 ["cat"; "/new"]], "new file contents")],
1134 "make a filesystem",
1136 This creates a filesystem on C<device> (usually a partition
1137 or LVM logical volume). The filesystem type is C<fstype>, for
1140 ("sfdisk", (RErr, [String "device";
1141 Int "cyls"; Int "heads"; Int "sectors";
1142 StringList "lines"]), 43, [DangerWillRobinson],
1144 "create partitions on a block device",
1146 This is a direct interface to the L<sfdisk(8)> program for creating
1147 partitions on block devices.
1149 C<device> should be a block device, for example C</dev/sda>.
1151 C<cyls>, C<heads> and C<sectors> are the number of cylinders, heads
1152 and sectors on the device, which are passed directly to sfdisk as
1153 the I<-C>, I<-H> and I<-S> parameters. If you pass C<0> for any
1154 of these, then the corresponding parameter is omitted. Usually for
1155 'large' disks, you can just pass C<0> for these, but for small
1156 (floppy-sized) disks, sfdisk (or rather, the kernel) cannot work
1157 out the right geometry and you will need to tell it.
1159 C<lines> is a list of lines that we feed to C<sfdisk>. For more
1160 information refer to the L<sfdisk(8)> manpage.
1162 To create a single partition occupying the whole disk, you would
1163 pass C<lines> as a single element list, when the single element being
1164 the string C<,> (comma).
1166 See also: C<guestfs_sfdisk_l>, C<guestfs_sfdisk_N>");
1168 ("write_file", (RErr, [String "path"; String "content"; Int "size"]), 44, [ProtocolLimitWarning],
1169 [InitBasicFS, Always, TestOutput (
1170 [["write_file"; "/new"; "new file contents"; "0"];
1171 ["cat"; "/new"]], "new file contents");
1172 InitBasicFS, Always, TestOutput (
1173 [["write_file"; "/new"; "\nnew file contents\n"; "0"];
1174 ["cat"; "/new"]], "\nnew file contents\n");
1175 InitBasicFS, Always, TestOutput (
1176 [["write_file"; "/new"; "\n\n"; "0"];
1177 ["cat"; "/new"]], "\n\n");
1178 InitBasicFS, Always, TestOutput (
1179 [["write_file"; "/new"; ""; "0"];
1180 ["cat"; "/new"]], "");
1181 InitBasicFS, Always, TestOutput (
1182 [["write_file"; "/new"; "\n\n\n"; "0"];
1183 ["cat"; "/new"]], "\n\n\n");
1184 InitBasicFS, Always, TestOutput (
1185 [["write_file"; "/new"; "\n"; "0"];
1186 ["cat"; "/new"]], "\n")],
1189 This call creates a file called C<path>. The contents of the
1190 file is the string C<content> (which can contain any 8 bit data),
1191 with length C<size>.
1193 As a special case, if C<size> is C<0>
1194 then the length is calculated using C<strlen> (so in this case
1195 the content cannot contain embedded ASCII NULs).
1197 I<NB.> Owing to a bug, writing content containing ASCII NUL
1198 characters does I<not> work, even if the length is specified.
1199 We hope to resolve this bug in a future version. In the meantime
1200 use C<guestfs_upload>.");
1202 ("umount", (RErr, [String "pathordevice"]), 45, [FishAlias "unmount"],
1203 [InitEmpty, Always, TestOutputList (
1204 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1205 ["mkfs"; "ext2"; "/dev/sda1"];
1206 ["mount"; "/dev/sda1"; "/"];
1207 ["mounts"]], ["/dev/sda1"]);
1208 InitEmpty, Always, TestOutputList (
1209 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1210 ["mkfs"; "ext2"; "/dev/sda1"];
1211 ["mount"; "/dev/sda1"; "/"];
1214 "unmount a filesystem",
1216 This unmounts the given filesystem. The filesystem may be
1217 specified either by its mountpoint (path) or the device which
1218 contains the filesystem.");
1220 ("mounts", (RStringList "devices", []), 46, [],
1221 [InitBasicFS, Always, TestOutputList (
1222 [["mounts"]], ["/dev/sda1"])],
1223 "show mounted filesystems",
1225 This returns the list of currently mounted filesystems. It returns
1226 the list of devices (eg. C</dev/sda1>, C</dev/VG/LV>).
1228 Some internal mounts are not shown.");
1230 ("umount_all", (RErr, []), 47, [FishAlias "unmount-all"],
1231 [InitBasicFS, Always, TestOutputList (
1234 (* check that umount_all can unmount nested mounts correctly: *)
1235 InitEmpty, Always, TestOutputList (
1236 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",10 ,20 ,"];
1237 ["mkfs"; "ext2"; "/dev/sda1"];
1238 ["mkfs"; "ext2"; "/dev/sda2"];
1239 ["mkfs"; "ext2"; "/dev/sda3"];
1240 ["mount"; "/dev/sda1"; "/"];
1242 ["mount"; "/dev/sda2"; "/mp1"];
1243 ["mkdir"; "/mp1/mp2"];
1244 ["mount"; "/dev/sda3"; "/mp1/mp2"];
1245 ["mkdir"; "/mp1/mp2/mp3"];
1248 "unmount all filesystems",
1250 This unmounts all mounted filesystems.
1252 Some internal mounts are not unmounted by this call.");
1254 ("lvm_remove_all", (RErr, []), 48, [DangerWillRobinson],
1256 "remove all LVM LVs, VGs and PVs",
1258 This command removes all LVM logical volumes, volume groups
1259 and physical volumes.");
1261 ("file", (RString "description", [String "path"]), 49, [],
1262 [InitBasicFS, Always, TestOutput (
1264 ["file"; "/new"]], "empty");
1265 InitBasicFS, Always, TestOutput (
1266 [["write_file"; "/new"; "some content\n"; "0"];
1267 ["file"; "/new"]], "ASCII text");
1268 InitBasicFS, Always, TestLastFail (
1269 [["file"; "/nofile"]])],
1270 "determine file type",
1272 This call uses the standard L<file(1)> command to determine
1273 the type or contents of the file. This also works on devices,
1274 for example to find out whether a partition contains a filesystem.
1276 The exact command which runs is C<file -bsL path>. Note in
1277 particular that the filename is not prepended to the output
1278 (the C<-b> option).");
1280 ("command", (RString "output", [StringList "arguments"]), 50, [ProtocolLimitWarning],
1281 [InitBasicFS, Always, TestOutput (
1282 [["upload"; "test-command"; "/test-command"];
1283 ["chmod"; "493"; "/test-command"];
1284 ["command"; "/test-command 1"]], "Result1");
1285 InitBasicFS, Always, TestOutput (
1286 [["upload"; "test-command"; "/test-command"];
1287 ["chmod"; "493"; "/test-command"];
1288 ["command"; "/test-command 2"]], "Result2\n");
1289 InitBasicFS, Always, TestOutput (
1290 [["upload"; "test-command"; "/test-command"];
1291 ["chmod"; "493"; "/test-command"];
1292 ["command"; "/test-command 3"]], "\nResult3");
1293 InitBasicFS, Always, TestOutput (
1294 [["upload"; "test-command"; "/test-command"];
1295 ["chmod"; "493"; "/test-command"];
1296 ["command"; "/test-command 4"]], "\nResult4\n");
1297 InitBasicFS, Always, TestOutput (
1298 [["upload"; "test-command"; "/test-command"];
1299 ["chmod"; "493"; "/test-command"];
1300 ["command"; "/test-command 5"]], "\nResult5\n\n");
1301 InitBasicFS, Always, TestOutput (
1302 [["upload"; "test-command"; "/test-command"];
1303 ["chmod"; "493"; "/test-command"];
1304 ["command"; "/test-command 6"]], "\n\nResult6\n\n");
1305 InitBasicFS, Always, TestOutput (
1306 [["upload"; "test-command"; "/test-command"];
1307 ["chmod"; "493"; "/test-command"];
1308 ["command"; "/test-command 7"]], "");
1309 InitBasicFS, Always, TestOutput (
1310 [["upload"; "test-command"; "/test-command"];
1311 ["chmod"; "493"; "/test-command"];
1312 ["command"; "/test-command 8"]], "\n");
1313 InitBasicFS, Always, TestOutput (
1314 [["upload"; "test-command"; "/test-command"];
1315 ["chmod"; "493"; "/test-command"];
1316 ["command"; "/test-command 9"]], "\n\n");
1317 InitBasicFS, Always, TestOutput (
1318 [["upload"; "test-command"; "/test-command"];
1319 ["chmod"; "493"; "/test-command"];
1320 ["command"; "/test-command 10"]], "Result10-1\nResult10-2\n");
1321 InitBasicFS, Always, TestOutput (
1322 [["upload"; "test-command"; "/test-command"];
1323 ["chmod"; "493"; "/test-command"];
1324 ["command"; "/test-command 11"]], "Result11-1\nResult11-2");
1325 InitBasicFS, Always, TestLastFail (
1326 [["upload"; "test-command"; "/test-command"];
1327 ["chmod"; "493"; "/test-command"];
1328 ["command"; "/test-command"]])],
1329 "run a command from the guest filesystem",
1331 This call runs a command from the guest filesystem. The
1332 filesystem must be mounted, and must contain a compatible
1333 operating system (ie. something Linux, with the same
1334 or compatible processor architecture).
1336 The single parameter is an argv-style list of arguments.
1337 The first element is the name of the program to run.
1338 Subsequent elements are parameters. The list must be
1339 non-empty (ie. must contain a program name).
1341 The return value is anything printed to I<stdout> by
1344 If the command returns a non-zero exit status, then
1345 this function returns an error message. The error message
1346 string is the content of I<stderr> from the command.
1348 The C<$PATH> environment variable will contain at least
1349 C</usr/bin> and C</bin>. If you require a program from
1350 another location, you should provide the full path in the
1353 Shared libraries and data files required by the program
1354 must be available on filesystems which are mounted in the
1355 correct places. It is the caller's responsibility to ensure
1356 all filesystems that are needed are mounted at the right
1359 ("command_lines", (RStringList "lines", [StringList "arguments"]), 51, [ProtocolLimitWarning],
1360 [InitBasicFS, Always, TestOutputList (
1361 [["upload"; "test-command"; "/test-command"];
1362 ["chmod"; "493"; "/test-command"];
1363 ["command_lines"; "/test-command 1"]], ["Result1"]);
1364 InitBasicFS, Always, TestOutputList (
1365 [["upload"; "test-command"; "/test-command"];
1366 ["chmod"; "493"; "/test-command"];
1367 ["command_lines"; "/test-command 2"]], ["Result2"]);
1368 InitBasicFS, Always, TestOutputList (
1369 [["upload"; "test-command"; "/test-command"];
1370 ["chmod"; "493"; "/test-command"];
1371 ["command_lines"; "/test-command 3"]], ["";"Result3"]);
1372 InitBasicFS, Always, TestOutputList (
1373 [["upload"; "test-command"; "/test-command"];
1374 ["chmod"; "493"; "/test-command"];
1375 ["command_lines"; "/test-command 4"]], ["";"Result4"]);
1376 InitBasicFS, Always, TestOutputList (
1377 [["upload"; "test-command"; "/test-command"];
1378 ["chmod"; "493"; "/test-command"];
1379 ["command_lines"; "/test-command 5"]], ["";"Result5";""]);
1380 InitBasicFS, Always, TestOutputList (
1381 [["upload"; "test-command"; "/test-command"];
1382 ["chmod"; "493"; "/test-command"];
1383 ["command_lines"; "/test-command 6"]], ["";"";"Result6";""]);
1384 InitBasicFS, Always, TestOutputList (
1385 [["upload"; "test-command"; "/test-command"];
1386 ["chmod"; "493"; "/test-command"];
1387 ["command_lines"; "/test-command 7"]], []);
1388 InitBasicFS, Always, TestOutputList (
1389 [["upload"; "test-command"; "/test-command"];
1390 ["chmod"; "493"; "/test-command"];
1391 ["command_lines"; "/test-command 8"]], [""]);
1392 InitBasicFS, Always, TestOutputList (
1393 [["upload"; "test-command"; "/test-command"];
1394 ["chmod"; "493"; "/test-command"];
1395 ["command_lines"; "/test-command 9"]], ["";""]);
1396 InitBasicFS, Always, TestOutputList (
1397 [["upload"; "test-command"; "/test-command"];
1398 ["chmod"; "493"; "/test-command"];
1399 ["command_lines"; "/test-command 10"]], ["Result10-1";"Result10-2"]);
1400 InitBasicFS, Always, TestOutputList (
1401 [["upload"; "test-command"; "/test-command"];
1402 ["chmod"; "493"; "/test-command"];
1403 ["command_lines"; "/test-command 11"]], ["Result11-1";"Result11-2"])],
1404 "run a command, returning lines",
1406 This is the same as C<guestfs_command>, but splits the
1407 result into a list of lines.");
1409 ("stat", (RStat "statbuf", [String "path"]), 52, [],
1410 [InitBasicFS, Always, TestOutputStruct (
1412 ["stat"; "/new"]], [CompareWithInt ("size", 0)])],
1413 "get file information",
1415 Returns file information for the given C<path>.
1417 This is the same as the C<stat(2)> system call.");
1419 ("lstat", (RStat "statbuf", [String "path"]), 53, [],
1420 [InitBasicFS, Always, TestOutputStruct (
1422 ["lstat"; "/new"]], [CompareWithInt ("size", 0)])],
1423 "get file information for a symbolic link",
1425 Returns file information for the given C<path>.
1427 This is the same as C<guestfs_stat> except that if C<path>
1428 is a symbolic link, then the link is stat-ed, not the file it
1431 This is the same as the C<lstat(2)> system call.");
1433 ("statvfs", (RStatVFS "statbuf", [String "path"]), 54, [],
1434 [InitBasicFS, Always, TestOutputStruct (
1435 [["statvfs"; "/"]], [CompareWithInt ("bfree", 487702);
1436 CompareWithInt ("blocks", 490020);
1437 CompareWithInt ("bsize", 1024)])],
1438 "get file system statistics",
1440 Returns file system statistics for any mounted file system.
1441 C<path> should be a file or directory in the mounted file system
1442 (typically it is the mount point itself, but it doesn't need to be).
1444 This is the same as the C<statvfs(2)> system call.");
1446 ("tune2fs_l", (RHashtable "superblock", [String "device"]), 55, [],
1448 "get ext2/ext3/ext4 superblock details",
1450 This returns the contents of the ext2, ext3 or ext4 filesystem
1451 superblock on C<device>.
1453 It is the same as running C<tune2fs -l device>. See L<tune2fs(8)>
1454 manpage for more details. The list of fields returned isn't
1455 clearly defined, and depends on both the version of C<tune2fs>
1456 that libguestfs was built against, and the filesystem itself.");
1458 ("blockdev_setro", (RErr, [String "device"]), 56, [],
1459 [InitEmpty, Always, TestOutputTrue (
1460 [["blockdev_setro"; "/dev/sda"];
1461 ["blockdev_getro"; "/dev/sda"]])],
1462 "set block device to read-only",
1464 Sets the block device named C<device> to read-only.
1466 This uses the L<blockdev(8)> command.");
1468 ("blockdev_setrw", (RErr, [String "device"]), 57, [],
1469 [InitEmpty, Always, TestOutputFalse (
1470 [["blockdev_setrw"; "/dev/sda"];
1471 ["blockdev_getro"; "/dev/sda"]])],
1472 "set block device to read-write",
1474 Sets the block device named C<device> to read-write.
1476 This uses the L<blockdev(8)> command.");
1478 ("blockdev_getro", (RBool "ro", [String "device"]), 58, [],
1479 [InitEmpty, Always, TestOutputTrue (
1480 [["blockdev_setro"; "/dev/sda"];
1481 ["blockdev_getro"; "/dev/sda"]])],
1482 "is block device set to read-only",
1484 Returns a boolean indicating if the block device is read-only
1485 (true if read-only, false if not).
1487 This uses the L<blockdev(8)> command.");
1489 ("blockdev_getss", (RInt "sectorsize", [String "device"]), 59, [],
1490 [InitEmpty, Always, TestOutputInt (
1491 [["blockdev_getss"; "/dev/sda"]], 512)],
1492 "get sectorsize of block device",
1494 This returns the size of sectors on a block device.
1495 Usually 512, but can be larger for modern devices.
1497 (Note, this is not the size in sectors, use C<guestfs_blockdev_getsz>
1500 This uses the L<blockdev(8)> command.");
1502 ("blockdev_getbsz", (RInt "blocksize", [String "device"]), 60, [],
1503 [InitEmpty, Always, TestOutputInt (
1504 [["blockdev_getbsz"; "/dev/sda"]], 4096)],
1505 "get blocksize of block device",
1507 This returns the block size of a device.
1509 (Note this is different from both I<size in blocks> and
1510 I<filesystem block size>).
1512 This uses the L<blockdev(8)> command.");
1514 ("blockdev_setbsz", (RErr, [String "device"; Int "blocksize"]), 61, [],
1516 "set blocksize of block device",
1518 This sets the block size of a device.
1520 (Note this is different from both I<size in blocks> and
1521 I<filesystem block size>).
1523 This uses the L<blockdev(8)> command.");
1525 ("blockdev_getsz", (RInt64 "sizeinsectors", [String "device"]), 62, [],
1526 [InitEmpty, Always, TestOutputInt (
1527 [["blockdev_getsz"; "/dev/sda"]], 1024000)],
1528 "get total size of device in 512-byte sectors",
1530 This returns the size of the device in units of 512-byte sectors
1531 (even if the sectorsize isn't 512 bytes ... weird).
1533 See also C<guestfs_blockdev_getss> for the real sector size of
1534 the device, and C<guestfs_blockdev_getsize64> for the more
1535 useful I<size in bytes>.
1537 This uses the L<blockdev(8)> command.");
1539 ("blockdev_getsize64", (RInt64 "sizeinbytes", [String "device"]), 63, [],
1540 [InitEmpty, Always, TestOutputInt (
1541 [["blockdev_getsize64"; "/dev/sda"]], 524288000)],
1542 "get total size of device in bytes",
1544 This returns the size of the device in bytes.
1546 See also C<guestfs_blockdev_getsz>.
1548 This uses the L<blockdev(8)> command.");
1550 ("blockdev_flushbufs", (RErr, [String "device"]), 64, [],
1551 [InitEmpty, Always, TestRun
1552 [["blockdev_flushbufs"; "/dev/sda"]]],
1553 "flush device buffers",
1555 This tells the kernel to flush internal buffers associated
1558 This uses the L<blockdev(8)> command.");
1560 ("blockdev_rereadpt", (RErr, [String "device"]), 65, [],
1561 [InitEmpty, Always, TestRun
1562 [["blockdev_rereadpt"; "/dev/sda"]]],
1563 "reread partition table",
1565 Reread the partition table on C<device>.
1567 This uses the L<blockdev(8)> command.");
1569 ("upload", (RErr, [FileIn "filename"; String "remotefilename"]), 66, [],
1570 [InitBasicFS, Always, TestOutput (
1571 (* Pick a file from cwd which isn't likely to change. *)
1572 [["upload"; "../COPYING.LIB"; "/COPYING.LIB"];
1573 ["checksum"; "md5"; "/COPYING.LIB"]], "e3eda01d9815f8d24aae2dbd89b68b06")],
1574 "upload a file from the local machine",
1576 Upload local file C<filename> to C<remotefilename> on the
1579 C<filename> can also be a named pipe.
1581 See also C<guestfs_download>.");
1583 ("download", (RErr, [String "remotefilename"; FileOut "filename"]), 67, [],
1584 [InitBasicFS, Always, TestOutput (
1585 (* Pick a file from cwd which isn't likely to change. *)
1586 [["upload"; "../COPYING.LIB"; "/COPYING.LIB"];
1587 ["download"; "/COPYING.LIB"; "testdownload.tmp"];
1588 ["upload"; "testdownload.tmp"; "/upload"];
1589 ["checksum"; "md5"; "/upload"]], "e3eda01d9815f8d24aae2dbd89b68b06")],
1590 "download a file to the local machine",
1592 Download file C<remotefilename> and save it as C<filename>
1593 on the local machine.
1595 C<filename> can also be a named pipe.
1597 See also C<guestfs_upload>, C<guestfs_cat>.");
1599 ("checksum", (RString "checksum", [String "csumtype"; String "path"]), 68, [],
1600 [InitBasicFS, Always, TestOutput (
1601 [["write_file"; "/new"; "test\n"; "0"];
1602 ["checksum"; "crc"; "/new"]], "935282863");
1603 InitBasicFS, Always, TestLastFail (
1604 [["checksum"; "crc"; "/new"]]);
1605 InitBasicFS, Always, TestOutput (
1606 [["write_file"; "/new"; "test\n"; "0"];
1607 ["checksum"; "md5"; "/new"]], "d8e8fca2dc0f896fd7cb4cb0031ba249");
1608 InitBasicFS, Always, TestOutput (
1609 [["write_file"; "/new"; "test\n"; "0"];
1610 ["checksum"; "sha1"; "/new"]], "4e1243bd22c66e76c2ba9eddc1f91394e57f9f83");
1611 InitBasicFS, Always, TestOutput (
1612 [["write_file"; "/new"; "test\n"; "0"];
1613 ["checksum"; "sha224"; "/new"]], "52f1bf093f4b7588726035c176c0cdb4376cfea53819f1395ac9e6ec");
1614 InitBasicFS, Always, TestOutput (
1615 [["write_file"; "/new"; "test\n"; "0"];
1616 ["checksum"; "sha256"; "/new"]], "f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2");
1617 InitBasicFS, Always, TestOutput (
1618 [["write_file"; "/new"; "test\n"; "0"];
1619 ["checksum"; "sha384"; "/new"]], "109bb6b5b6d5547c1ce03c7a8bd7d8f80c1cb0957f50c4f7fda04692079917e4f9cad52b878f3d8234e1a170b154b72d");
1620 InitBasicFS, Always, TestOutput (
1621 [["write_file"; "/new"; "test\n"; "0"];
1622 ["checksum"; "sha512"; "/new"]], "0e3e75234abc68f4378a86b3f4b32a198ba301845b0cd6e50106e874345700cc6663a86c1ea125dc5e92be17c98f9a0f85ca9d5f595db2012f7cc3571945c123")],
1623 "compute MD5, SHAx or CRC checksum of file",
1625 This call computes the MD5, SHAx or CRC checksum of the
1628 The type of checksum to compute is given by the C<csumtype>
1629 parameter which must have one of the following values:
1635 Compute the cyclic redundancy check (CRC) specified by POSIX
1636 for the C<cksum> command.
1640 Compute the MD5 hash (using the C<md5sum> program).
1644 Compute the SHA1 hash (using the C<sha1sum> program).
1648 Compute the SHA224 hash (using the C<sha224sum> program).
1652 Compute the SHA256 hash (using the C<sha256sum> program).
1656 Compute the SHA384 hash (using the C<sha384sum> program).
1660 Compute the SHA512 hash (using the C<sha512sum> program).
1664 The checksum is returned as a printable string.");
1666 ("tar_in", (RErr, [FileIn "tarfile"; String "directory"]), 69, [],
1667 [InitBasicFS, Always, TestOutput (
1668 [["tar_in"; "../images/helloworld.tar"; "/"];
1669 ["cat"; "/hello"]], "hello\n")],
1670 "unpack tarfile to directory",
1672 This command uploads and unpacks local file C<tarfile> (an
1673 I<uncompressed> tar file) into C<directory>.
1675 To upload a compressed tarball, use C<guestfs_tgz_in>.");
1677 ("tar_out", (RErr, [String "directory"; FileOut "tarfile"]), 70, [],
1679 "pack directory into tarfile",
1681 This command packs the contents of C<directory> and downloads
1682 it to local file C<tarfile>.
1684 To download a compressed tarball, use C<guestfs_tgz_out>.");
1686 ("tgz_in", (RErr, [FileIn "tarball"; String "directory"]), 71, [],
1687 [InitBasicFS, Always, TestOutput (
1688 [["tgz_in"; "../images/helloworld.tar.gz"; "/"];
1689 ["cat"; "/hello"]], "hello\n")],
1690 "unpack compressed tarball to directory",
1692 This command uploads and unpacks local file C<tarball> (a
1693 I<gzip compressed> tar file) into C<directory>.
1695 To upload an uncompressed tarball, use C<guestfs_tar_in>.");
1697 ("tgz_out", (RErr, [String "directory"; FileOut "tarball"]), 72, [],
1699 "pack directory into compressed tarball",
1701 This command packs the contents of C<directory> and downloads
1702 it to local file C<tarball>.
1704 To download an uncompressed tarball, use C<guestfs_tar_out>.");
1706 ("mount_ro", (RErr, [String "device"; String "mountpoint"]), 73, [],
1707 [InitBasicFS, Always, TestLastFail (
1709 ["mount_ro"; "/dev/sda1"; "/"];
1710 ["touch"; "/new"]]);
1711 InitBasicFS, Always, TestOutput (
1712 [["write_file"; "/new"; "data"; "0"];
1714 ["mount_ro"; "/dev/sda1"; "/"];
1715 ["cat"; "/new"]], "data")],
1716 "mount a guest disk, read-only",
1718 This is the same as the C<guestfs_mount> command, but it
1719 mounts the filesystem with the read-only (I<-o ro>) flag.");
1721 ("mount_options", (RErr, [String "options"; String "device"; String "mountpoint"]), 74, [],
1723 "mount a guest disk with mount options",
1725 This is the same as the C<guestfs_mount> command, but it
1726 allows you to set the mount options as for the
1727 L<mount(8)> I<-o> flag.");
1729 ("mount_vfs", (RErr, [String "options"; String "vfstype"; String "device"; String "mountpoint"]), 75, [],
1731 "mount a guest disk with mount options and vfstype",
1733 This is the same as the C<guestfs_mount> command, but it
1734 allows you to set both the mount options and the vfstype
1735 as for the L<mount(8)> I<-o> and I<-t> flags.");
1737 ("debug", (RString "result", [String "subcmd"; StringList "extraargs"]), 76, [],
1739 "debugging and internals",
1741 The C<guestfs_debug> command exposes some internals of
1742 C<guestfsd> (the guestfs daemon) that runs inside the
1745 There is no comprehensive help for this command. You have
1746 to look at the file C<daemon/debug.c> in the libguestfs source
1747 to find out what you can do.");
1749 ("lvremove", (RErr, [String "device"]), 77, [],
1750 [InitEmpty, Always, TestOutputList (
1751 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1752 ["pvcreate"; "/dev/sda1"];
1753 ["vgcreate"; "VG"; "/dev/sda1"];
1754 ["lvcreate"; "LV1"; "VG"; "50"];
1755 ["lvcreate"; "LV2"; "VG"; "50"];
1756 ["lvremove"; "/dev/VG/LV1"];
1757 ["lvs"]], ["/dev/VG/LV2"]);
1758 InitEmpty, Always, TestOutputList (
1759 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1760 ["pvcreate"; "/dev/sda1"];
1761 ["vgcreate"; "VG"; "/dev/sda1"];
1762 ["lvcreate"; "LV1"; "VG"; "50"];
1763 ["lvcreate"; "LV2"; "VG"; "50"];
1764 ["lvremove"; "/dev/VG"];
1766 InitEmpty, Always, TestOutputList (
1767 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1768 ["pvcreate"; "/dev/sda1"];
1769 ["vgcreate"; "VG"; "/dev/sda1"];
1770 ["lvcreate"; "LV1"; "VG"; "50"];
1771 ["lvcreate"; "LV2"; "VG"; "50"];
1772 ["lvremove"; "/dev/VG"];
1774 "remove an LVM logical volume",
1776 Remove an LVM logical volume C<device>, where C<device> is
1777 the path to the LV, such as C</dev/VG/LV>.
1779 You can also remove all LVs in a volume group by specifying
1780 the VG name, C</dev/VG>.");
1782 ("vgremove", (RErr, [String "vgname"]), 78, [],
1783 [InitEmpty, Always, TestOutputList (
1784 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1785 ["pvcreate"; "/dev/sda1"];
1786 ["vgcreate"; "VG"; "/dev/sda1"];
1787 ["lvcreate"; "LV1"; "VG"; "50"];
1788 ["lvcreate"; "LV2"; "VG"; "50"];
1791 InitEmpty, Always, TestOutputList (
1792 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1793 ["pvcreate"; "/dev/sda1"];
1794 ["vgcreate"; "VG"; "/dev/sda1"];
1795 ["lvcreate"; "LV1"; "VG"; "50"];
1796 ["lvcreate"; "LV2"; "VG"; "50"];
1799 "remove an LVM volume group",
1801 Remove an LVM volume group C<vgname>, (for example C<VG>).
1803 This also forcibly removes all logical volumes in the volume
1806 ("pvremove", (RErr, [String "device"]), 79, [],
1807 [InitEmpty, Always, TestOutputList (
1808 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1809 ["pvcreate"; "/dev/sda1"];
1810 ["vgcreate"; "VG"; "/dev/sda1"];
1811 ["lvcreate"; "LV1"; "VG"; "50"];
1812 ["lvcreate"; "LV2"; "VG"; "50"];
1814 ["pvremove"; "/dev/sda1"];
1816 InitEmpty, Always, TestOutputList (
1817 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1818 ["pvcreate"; "/dev/sda1"];
1819 ["vgcreate"; "VG"; "/dev/sda1"];
1820 ["lvcreate"; "LV1"; "VG"; "50"];
1821 ["lvcreate"; "LV2"; "VG"; "50"];
1823 ["pvremove"; "/dev/sda1"];
1825 InitEmpty, Always, TestOutputList (
1826 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1827 ["pvcreate"; "/dev/sda1"];
1828 ["vgcreate"; "VG"; "/dev/sda1"];
1829 ["lvcreate"; "LV1"; "VG"; "50"];
1830 ["lvcreate"; "LV2"; "VG"; "50"];
1832 ["pvremove"; "/dev/sda1"];
1834 "remove an LVM physical volume",
1836 This wipes a physical volume C<device> so that LVM will no longer
1839 The implementation uses the C<pvremove> command which refuses to
1840 wipe physical volumes that contain any volume groups, so you have
1841 to remove those first.");
1843 ("set_e2label", (RErr, [String "device"; String "label"]), 80, [],
1844 [InitBasicFS, Always, TestOutput (
1845 [["set_e2label"; "/dev/sda1"; "testlabel"];
1846 ["get_e2label"; "/dev/sda1"]], "testlabel")],
1847 "set the ext2/3/4 filesystem label",
1849 This sets the ext2/3/4 filesystem label of the filesystem on
1850 C<device> to C<label>. Filesystem labels are limited to
1853 You can use either C<guestfs_tune2fs_l> or C<guestfs_get_e2label>
1854 to return the existing label on a filesystem.");
1856 ("get_e2label", (RString "label", [String "device"]), 81, [],
1858 "get the ext2/3/4 filesystem label",
1860 This returns the ext2/3/4 filesystem label of the filesystem on
1863 ("set_e2uuid", (RErr, [String "device"; String "uuid"]), 82, [],
1864 [InitBasicFS, Always, TestOutput (
1865 [["set_e2uuid"; "/dev/sda1"; "a3a61220-882b-4f61-89f4-cf24dcc7297d"];
1866 ["get_e2uuid"; "/dev/sda1"]], "a3a61220-882b-4f61-89f4-cf24dcc7297d");
1867 InitBasicFS, Always, TestOutput (
1868 [["set_e2uuid"; "/dev/sda1"; "clear"];
1869 ["get_e2uuid"; "/dev/sda1"]], "");
1870 (* We can't predict what UUIDs will be, so just check the commands run. *)
1871 InitBasicFS, Always, TestRun (
1872 [["set_e2uuid"; "/dev/sda1"; "random"]]);
1873 InitBasicFS, Always, TestRun (
1874 [["set_e2uuid"; "/dev/sda1"; "time"]])],
1875 "set the ext2/3/4 filesystem UUID",
1877 This sets the ext2/3/4 filesystem UUID of the filesystem on
1878 C<device> to C<uuid>. The format of the UUID and alternatives
1879 such as C<clear>, C<random> and C<time> are described in the
1880 L<tune2fs(8)> manpage.
1882 You can use either C<guestfs_tune2fs_l> or C<guestfs_get_e2uuid>
1883 to return the existing UUID of a filesystem.");
1885 ("get_e2uuid", (RString "uuid", [String "device"]), 83, [],
1887 "get the ext2/3/4 filesystem UUID",
1889 This returns the ext2/3/4 filesystem UUID of the filesystem on
1892 ("fsck", (RInt "status", [String "fstype"; String "device"]), 84, [],
1893 [InitBasicFS, Always, TestOutputInt (
1894 [["umount"; "/dev/sda1"];
1895 ["fsck"; "ext2"; "/dev/sda1"]], 0);
1896 InitBasicFS, Always, TestOutputInt (
1897 [["umount"; "/dev/sda1"];
1898 ["zero"; "/dev/sda1"];
1899 ["fsck"; "ext2"; "/dev/sda1"]], 8)],
1900 "run the filesystem checker",
1902 This runs the filesystem checker (fsck) on C<device> which
1903 should have filesystem type C<fstype>.
1905 The returned integer is the status. See L<fsck(8)> for the
1906 list of status codes from C<fsck>.
1914 Multiple status codes can be summed together.
1918 A non-zero return code can mean \"success\", for example if
1919 errors have been corrected on the filesystem.
1923 Checking or repairing NTFS volumes is not supported
1928 This command is entirely equivalent to running C<fsck -a -t fstype device>.");
1930 ("zero", (RErr, [String "device"]), 85, [],
1931 [InitBasicFS, Always, TestOutput (
1932 [["umount"; "/dev/sda1"];
1933 ["zero"; "/dev/sda1"];
1934 ["file"; "/dev/sda1"]], "data")],
1935 "write zeroes to the device",
1937 This command writes zeroes over the first few blocks of C<device>.
1939 How many blocks are zeroed isn't specified (but it's I<not> enough
1940 to securely wipe the device). It should be sufficient to remove
1941 any partition tables, filesystem superblocks and so on.");
1943 ("grub_install", (RErr, [String "root"; String "device"]), 86, [],
1944 [InitBasicFS, Always, TestOutputTrue (
1945 [["grub_install"; "/"; "/dev/sda1"];
1946 ["is_dir"; "/boot"]])],
1949 This command installs GRUB (the Grand Unified Bootloader) on
1950 C<device>, with the root directory being C<root>.");
1952 ("cp", (RErr, [String "src"; String "dest"]), 87, [],
1953 [InitBasicFS, Always, TestOutput (
1954 [["write_file"; "/old"; "file content"; "0"];
1955 ["cp"; "/old"; "/new"];
1956 ["cat"; "/new"]], "file content");
1957 InitBasicFS, Always, TestOutputTrue (
1958 [["write_file"; "/old"; "file content"; "0"];
1959 ["cp"; "/old"; "/new"];
1960 ["is_file"; "/old"]]);
1961 InitBasicFS, Always, TestOutput (
1962 [["write_file"; "/old"; "file content"; "0"];
1964 ["cp"; "/old"; "/dir/new"];
1965 ["cat"; "/dir/new"]], "file content")],
1968 This copies a file from C<src> to C<dest> where C<dest> is
1969 either a destination filename or destination directory.");
1971 ("cp_a", (RErr, [String "src"; String "dest"]), 88, [],
1972 [InitBasicFS, Always, TestOutput (
1973 [["mkdir"; "/olddir"];
1974 ["mkdir"; "/newdir"];
1975 ["write_file"; "/olddir/file"; "file content"; "0"];
1976 ["cp_a"; "/olddir"; "/newdir"];
1977 ["cat"; "/newdir/olddir/file"]], "file content")],
1978 "copy a file or directory recursively",
1980 This copies a file or directory from C<src> to C<dest>
1981 recursively using the C<cp -a> command.");
1983 ("mv", (RErr, [String "src"; String "dest"]), 89, [],
1984 [InitBasicFS, Always, TestOutput (
1985 [["write_file"; "/old"; "file content"; "0"];
1986 ["mv"; "/old"; "/new"];
1987 ["cat"; "/new"]], "file content");
1988 InitBasicFS, Always, TestOutputFalse (
1989 [["write_file"; "/old"; "file content"; "0"];
1990 ["mv"; "/old"; "/new"];
1991 ["is_file"; "/old"]])],
1994 This moves a file from C<src> to C<dest> where C<dest> is
1995 either a destination filename or destination directory.");
1997 ("drop_caches", (RErr, [Int "whattodrop"]), 90, [],
1998 [InitEmpty, Always, TestRun (
1999 [["drop_caches"; "3"]])],
2000 "drop kernel page cache, dentries and inodes",
2002 This instructs the guest kernel to drop its page cache,
2003 and/or dentries and inode caches. The parameter C<whattodrop>
2004 tells the kernel what precisely to drop, see
2005 L<http://linux-mm.org/Drop_Caches>
2007 Setting C<whattodrop> to 3 should drop everything.
2009 This automatically calls L<sync(2)> before the operation,
2010 so that the maximum guest memory is freed.");
2012 ("dmesg", (RString "kmsgs", []), 91, [],
2013 [InitEmpty, Always, TestRun (
2015 "return kernel messages",
2017 This returns the kernel messages (C<dmesg> output) from
2018 the guest kernel. This is sometimes useful for extended
2019 debugging of problems.
2021 Another way to get the same information is to enable
2022 verbose messages with C<guestfs_set_verbose> or by setting
2023 the environment variable C<LIBGUESTFS_DEBUG=1> before
2024 running the program.");
2026 ("ping_daemon", (RErr, []), 92, [],
2027 [InitEmpty, Always, TestRun (
2028 [["ping_daemon"]])],
2029 "ping the guest daemon",
2031 This is a test probe into the guestfs daemon running inside
2032 the qemu subprocess. Calling this function checks that the
2033 daemon responds to the ping message, without affecting the daemon
2034 or attached block device(s) in any other way.");
2036 ("equal", (RBool "equality", [String "file1"; String "file2"]), 93, [],
2037 [InitBasicFS, Always, TestOutputTrue (
2038 [["write_file"; "/file1"; "contents of a file"; "0"];
2039 ["cp"; "/file1"; "/file2"];
2040 ["equal"; "/file1"; "/file2"]]);
2041 InitBasicFS, Always, TestOutputFalse (
2042 [["write_file"; "/file1"; "contents of a file"; "0"];
2043 ["write_file"; "/file2"; "contents of another file"; "0"];
2044 ["equal"; "/file1"; "/file2"]]);
2045 InitBasicFS, Always, TestLastFail (
2046 [["equal"; "/file1"; "/file2"]])],
2047 "test if two files have equal contents",
2049 This compares the two files C<file1> and C<file2> and returns
2050 true if their content is exactly equal, or false otherwise.
2052 The external L<cmp(1)> program is used for the comparison.");
2054 ("strings", (RStringList "stringsout", [String "path"]), 94, [ProtocolLimitWarning],
2055 [InitBasicFS, Always, TestOutputList (
2056 [["write_file"; "/new"; "hello\nworld\n"; "0"];
2057 ["strings"; "/new"]], ["hello"; "world"]);
2058 InitBasicFS, Always, TestOutputList (
2060 ["strings"; "/new"]], [])],
2061 "print the printable strings in a file",
2063 This runs the L<strings(1)> command on a file and returns
2064 the list of printable strings found.");
2066 ("strings_e", (RStringList "stringsout", [String "encoding"; String "path"]), 95, [ProtocolLimitWarning],
2067 [InitBasicFS, Always, TestOutputList (
2068 [["write_file"; "/new"; "hello\nworld\n"; "0"];
2069 ["strings_e"; "b"; "/new"]], []);
2070 InitBasicFS, Disabled, TestOutputList (
2071 [["write_file"; "/new"; "\000h\000e\000l\000l\000o\000\n\000w\000o\000r\000l\000d\000\n"; "24"];
2072 ["strings_e"; "b"; "/new"]], ["hello"; "world"])],
2073 "print the printable strings in a file",
2075 This is like the C<guestfs_strings> command, but allows you to
2076 specify the encoding.
2078 See the L<strings(1)> manpage for the full list of encodings.
2080 Commonly useful encodings are C<l> (lower case L) which will
2081 show strings inside Windows/x86 files.
2083 The returned strings are transcoded to UTF-8.");
2085 ("hexdump", (RString "dump", [String "path"]), 96, [ProtocolLimitWarning],
2086 [InitBasicFS, Always, TestOutput (
2087 [["write_file"; "/new"; "hello\nworld\n"; "12"];
2088 ["hexdump"; "/new"]], "00000000 68 65 6c 6c 6f 0a 77 6f 72 6c 64 0a |hello.world.|\n0000000c\n")],
2089 "dump a file in hexadecimal",
2091 This runs C<hexdump -C> on the given C<path>. The result is
2092 the human-readable, canonical hex dump of the file.");
2094 ("zerofree", (RErr, [String "device"]), 97, [],
2095 [InitNone, Always, TestOutput (
2096 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
2097 ["mkfs"; "ext3"; "/dev/sda1"];
2098 ["mount"; "/dev/sda1"; "/"];
2099 ["write_file"; "/new"; "test file"; "0"];
2100 ["umount"; "/dev/sda1"];
2101 ["zerofree"; "/dev/sda1"];
2102 ["mount"; "/dev/sda1"; "/"];
2103 ["cat"; "/new"]], "test file")],
2104 "zero unused inodes and disk blocks on ext2/3 filesystem",
2106 This runs the I<zerofree> program on C<device>. This program
2107 claims to zero unused inodes and disk blocks on an ext2/3
2108 filesystem, thus making it possible to compress the filesystem
2111 You should B<not> run this program if the filesystem is
2114 It is possible that using this program can damage the filesystem
2115 or data on the filesystem.");
2117 ("pvresize", (RErr, [String "device"]), 98, [],
2119 "resize an LVM physical volume",
2121 This resizes (expands or shrinks) an existing LVM physical
2122 volume to match the new size of the underlying device.");
2124 ("sfdisk_N", (RErr, [String "device"; Int "n";
2125 Int "cyls"; Int "heads"; Int "sectors";
2126 String "line"]), 99, [DangerWillRobinson],
2128 "modify a single partition on a block device",
2130 This runs L<sfdisk(8)> option to modify just the single
2131 partition C<n> (note: C<n> counts from 1).
2133 For other parameters, see C<guestfs_sfdisk>. You should usually
2134 pass C<0> for the cyls/heads/sectors parameters.");
2136 ("sfdisk_l", (RString "partitions", [String "device"]), 100, [],
2138 "display the partition table",
2140 This displays the partition table on C<device>, in the
2141 human-readable output of the L<sfdisk(8)> command. It is
2142 not intended to be parsed.");
2144 ("sfdisk_kernel_geometry", (RString "partitions", [String "device"]), 101, [],
2146 "display the kernel geometry",
2148 This displays the kernel's idea of the geometry of C<device>.
2150 The result is in human-readable format, and not designed to
2153 ("sfdisk_disk_geometry", (RString "partitions", [String "device"]), 102, [],
2155 "display the disk geometry from the partition table",
2157 This displays the disk geometry of C<device> read from the
2158 partition table. Especially in the case where the underlying
2159 block device has been resized, this can be different from the
2160 kernel's idea of the geometry (see C<guestfs_sfdisk_kernel_geometry>).
2162 The result is in human-readable format, and not designed to
2165 ("vg_activate_all", (RErr, [Bool "activate"]), 103, [],
2167 "activate or deactivate all volume groups",
2169 This command activates or (if C<activate> is false) deactivates
2170 all logical volumes in all volume groups.
2171 If activated, then they are made known to the
2172 kernel, ie. they appear as C</dev/mapper> devices. If deactivated,
2173 then those devices disappear.
2175 This command is the same as running C<vgchange -a y|n>");
2177 ("vg_activate", (RErr, [Bool "activate"; StringList "volgroups"]), 104, [],
2179 "activate or deactivate some volume groups",
2181 This command activates or (if C<activate> is false) deactivates
2182 all logical volumes in the listed volume groups C<volgroups>.
2183 If activated, then they are made known to the
2184 kernel, ie. they appear as C</dev/mapper> devices. If deactivated,
2185 then those devices disappear.
2187 This command is the same as running C<vgchange -a y|n volgroups...>
2189 Note that if C<volgroups> is an empty list then B<all> volume groups
2190 are activated or deactivated.");
2192 ("lvresize", (RErr, [String "device"; Int "mbytes"]), 105, [],
2193 [InitNone, Always, TestOutput (
2194 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
2195 ["pvcreate"; "/dev/sda1"];
2196 ["vgcreate"; "VG"; "/dev/sda1"];
2197 ["lvcreate"; "LV"; "VG"; "10"];
2198 ["mkfs"; "ext2"; "/dev/VG/LV"];
2199 ["mount"; "/dev/VG/LV"; "/"];
2200 ["write_file"; "/new"; "test content"; "0"];
2202 ["lvresize"; "/dev/VG/LV"; "20"];
2203 ["e2fsck_f"; "/dev/VG/LV"];
2204 ["resize2fs"; "/dev/VG/LV"];
2205 ["mount"; "/dev/VG/LV"; "/"];
2206 ["cat"; "/new"]], "test content")],
2207 "resize an LVM logical volume",
2209 This resizes (expands or shrinks) an existing LVM logical
2210 volume to C<mbytes>. When reducing, data in the reduced part
2213 ("resize2fs", (RErr, [String "device"]), 106, [],
2214 [], (* lvresize tests this *)
2215 "resize an ext2/ext3 filesystem",
2217 This resizes an ext2 or ext3 filesystem to match the size of
2218 the underlying device.
2220 I<Note:> It is sometimes required that you run C<guestfs_e2fsck_f>
2221 on the C<device> before calling this command. For unknown reasons
2222 C<resize2fs> sometimes gives an error about this and sometimes not.
2223 In any case, it is always safe to call C<guestfs_e2fsck_f> before
2224 calling this function.");
2226 ("find", (RStringList "names", [String "directory"]), 107, [],
2227 [InitBasicFS, Always, TestOutputList (
2228 [["find"; "/"]], ["lost+found"]);
2229 InitBasicFS, Always, TestOutputList (
2233 ["find"; "/"]], ["a"; "b"; "b/c"; "lost+found"]);
2234 InitBasicFS, Always, TestOutputList (
2235 [["mkdir_p"; "/a/b/c"];
2236 ["touch"; "/a/b/c/d"];
2237 ["find"; "/a/b/"]], ["c"; "c/d"])],
2238 "find all files and directories",
2240 This command lists out all files and directories, recursively,
2241 starting at C<directory>. It is essentially equivalent to
2242 running the shell command C<find directory -print> but some
2243 post-processing happens on the output, described below.
2245 This returns a list of strings I<without any prefix>. Thus
2246 if the directory structure was:
2252 then the returned list from C<guestfs_find> C</tmp> would be
2260 If C<directory> is not a directory, then this command returns
2263 The returned list is sorted.");
2265 ("e2fsck_f", (RErr, [String "device"]), 108, [],
2266 [], (* lvresize tests this *)
2267 "check an ext2/ext3 filesystem",
2269 This runs C<e2fsck -p -f device>, ie. runs the ext2/ext3
2270 filesystem checker on C<device>, noninteractively (C<-p>),
2271 even if the filesystem appears to be clean (C<-f>).
2273 This command is only needed because of C<guestfs_resize2fs>
2274 (q.v.). Normally you should use C<guestfs_fsck>.");
2278 let all_functions = non_daemon_functions @ daemon_functions
2280 (* In some places we want the functions to be displayed sorted
2281 * alphabetically, so this is useful:
2283 let all_functions_sorted =
2284 List.sort (fun (n1,_,_,_,_,_,_) (n2,_,_,_,_,_,_) ->
2285 compare n1 n2) all_functions
2287 (* Column names and types from LVM PVs/VGs/LVs. *)
2296 "pv_attr", `String (* XXX *);
2297 "pv_pe_count", `Int;
2298 "pv_pe_alloc_count", `Int;
2301 "pv_mda_count", `Int;
2302 "pv_mda_free", `Bytes;
2303 (* Not in Fedora 10:
2304 "pv_mda_size", `Bytes;
2311 "vg_attr", `String (* XXX *);
2314 "vg_sysid", `String;
2315 "vg_extent_size", `Bytes;
2316 "vg_extent_count", `Int;
2317 "vg_free_count", `Int;
2325 "vg_mda_count", `Int;
2326 "vg_mda_free", `Bytes;
2327 (* Not in Fedora 10:
2328 "vg_mda_size", `Bytes;
2334 "lv_attr", `String (* XXX *);
2337 "lv_kernel_major", `Int;
2338 "lv_kernel_minor", `Int;
2342 "snap_percent", `OptPercent;
2343 "copy_percent", `OptPercent;
2346 "mirror_log", `String;
2350 (* Column names and types from stat structures.
2351 * NB. Can't use things like 'st_atime' because glibc header files
2352 * define some of these as macros. Ugh.
2369 let statvfs_cols = [
2383 (* Used for testing language bindings. *)
2385 | CallString of string
2386 | CallOptString of string option
2387 | CallStringList of string list
2391 (* Useful functions.
2392 * Note we don't want to use any external OCaml libraries which
2393 * makes this a bit harder than it should be.
2395 let failwithf fs = ksprintf failwith fs
2397 let replace_char s c1 c2 =
2398 let s2 = String.copy s in
2399 let r = ref false in
2400 for i = 0 to String.length s2 - 1 do
2401 if String.unsafe_get s2 i = c1 then (
2402 String.unsafe_set s2 i c2;
2406 if not !r then s else s2
2410 (* || c = '\f' *) || c = '\n' || c = '\r' || c = '\t' (* || c = '\v' *)
2412 let triml ?(test = isspace) str =
2414 let n = ref (String.length str) in
2415 while !n > 0 && test str.[!i]; do
2420 else String.sub str !i !n
2422 let trimr ?(test = isspace) str =
2423 let n = ref (String.length str) in
2424 while !n > 0 && test str.[!n-1]; do
2427 if !n = String.length str then str
2428 else String.sub str 0 !n
2430 let trim ?(test = isspace) str =
2431 trimr ~test (triml ~test str)
2433 let rec find s sub =
2434 let len = String.length s in
2435 let sublen = String.length sub in
2437 if i <= len-sublen then (
2439 if j < sublen then (
2440 if s.[i+j] = sub.[j] then loop2 (j+1)
2446 if r = -1 then loop (i+1) else r
2452 let rec replace_str s s1 s2 =
2453 let len = String.length s in
2454 let sublen = String.length s1 in
2455 let i = find s s1 in
2458 let s' = String.sub s 0 i in
2459 let s'' = String.sub s (i+sublen) (len-i-sublen) in
2460 s' ^ s2 ^ replace_str s'' s1 s2
2463 let rec string_split sep str =
2464 let len = String.length str in
2465 let seplen = String.length sep in
2466 let i = find str sep in
2467 if i = -1 then [str]
2469 let s' = String.sub str 0 i in
2470 let s'' = String.sub str (i+seplen) (len-i-seplen) in
2471 s' :: string_split sep s''
2474 let files_equal n1 n2 =
2475 let cmd = sprintf "cmp -s %s %s" (Filename.quote n1) (Filename.quote n2) in
2476 match Sys.command cmd with
2479 | i -> failwithf "%s: failed with error code %d" cmd i
2481 let rec find_map f = function
2482 | [] -> raise Not_found
2486 | None -> find_map f xs
2489 let rec loop i = function
2491 | x :: xs -> f i x; loop (i+1) xs
2496 let rec loop i = function
2498 | x :: xs -> let r = f i x in r :: loop (i+1) xs
2502 let name_of_argt = function
2503 | String n | OptString n | StringList n | Bool n | Int n
2504 | FileIn n | FileOut n -> n
2506 let seq_of_test = function
2507 | TestRun s | TestOutput (s, _) | TestOutputList (s, _)
2508 | TestOutputInt (s, _) | TestOutputTrue s | TestOutputFalse s
2509 | TestOutputLength (s, _) | TestOutputStruct (s, _)
2510 | TestLastFail s -> s
2512 (* Check function names etc. for consistency. *)
2513 let check_functions () =
2514 let contains_uppercase str =
2515 let len = String.length str in
2517 if i >= len then false
2520 if c >= 'A' && c <= 'Z' then true
2527 (* Check function names. *)
2529 fun (name, _, _, _, _, _, _) ->
2530 if String.length name >= 7 && String.sub name 0 7 = "guestfs" then
2531 failwithf "function name %s does not need 'guestfs' prefix" name;
2533 failwithf "function name is empty";
2534 if name.[0] < 'a' || name.[0] > 'z' then
2535 failwithf "function name %s must start with lowercase a-z" name;
2536 if String.contains name '-' then
2537 failwithf "function name %s should not contain '-', use '_' instead."
2541 (* Check function parameter/return names. *)
2543 fun (name, style, _, _, _, _, _) ->
2544 let check_arg_ret_name n =
2545 if contains_uppercase n then
2546 failwithf "%s param/ret %s should not contain uppercase chars"
2548 if String.contains n '-' || String.contains n '_' then
2549 failwithf "%s param/ret %s should not contain '-' or '_'"
2552 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;
2553 if n = "int" || n = "char" || n = "short" || n = "long" then
2554 failwithf "%s has a param/ret which conflicts with a C type (eg. 'int', 'char' etc.)" name;
2556 failwithf "%s has a param/ret called 'i', which will cause some conflicts in the generated code" name;
2557 if n = "argv" || n = "args" then
2558 failwithf "%s has a param/ret called 'argv' or 'args', which will cause some conflicts in the generated code" name
2561 (match fst style with
2563 | RInt n | RInt64 n | RBool n | RConstString n | RString n
2564 | RStringList n | RPVList n | RVGList n | RLVList n
2565 | RStat n | RStatVFS n
2567 check_arg_ret_name n
2569 check_arg_ret_name n;
2570 check_arg_ret_name m
2572 List.iter (fun arg -> check_arg_ret_name (name_of_argt arg)) (snd style)
2575 (* Check short descriptions. *)
2577 fun (name, _, _, _, _, shortdesc, _) ->
2578 if shortdesc.[0] <> Char.lowercase shortdesc.[0] then
2579 failwithf "short description of %s should begin with lowercase." name;
2580 let c = shortdesc.[String.length shortdesc-1] in
2581 if c = '\n' || c = '.' then
2582 failwithf "short description of %s should not end with . or \\n." name
2585 (* Check long dscriptions. *)
2587 fun (name, _, _, _, _, _, longdesc) ->
2588 if longdesc.[String.length longdesc-1] = '\n' then
2589 failwithf "long description of %s should not end with \\n." name
2592 (* Check proc_nrs. *)
2594 fun (name, _, proc_nr, _, _, _, _) ->
2595 if proc_nr <= 0 then
2596 failwithf "daemon function %s should have proc_nr > 0" name
2600 fun (name, _, proc_nr, _, _, _, _) ->
2601 if proc_nr <> -1 then
2602 failwithf "non-daemon function %s should have proc_nr -1" name
2603 ) non_daemon_functions;
2606 List.map (fun (name, _, proc_nr, _, _, _, _) -> name, proc_nr)
2609 List.sort (fun (_,nr1) (_,nr2) -> compare nr1 nr2) proc_nrs in
2610 let rec loop = function
2613 | (name1,nr1) :: ((name2,nr2) :: _ as rest) when nr1 < nr2 ->
2615 | (name1,nr1) :: (name2,nr2) :: _ ->
2616 failwithf "%s and %s have conflicting procedure numbers (%d, %d)"
2624 (* Ignore functions that have no tests. We generate a
2625 * warning when the user does 'make check' instead.
2627 | name, _, _, _, [], _, _ -> ()
2628 | name, _, _, _, tests, _, _ ->
2632 match seq_of_test test with
2634 failwithf "%s has a test containing an empty sequence" name
2635 | cmds -> List.map List.hd cmds
2637 let funcs = List.flatten funcs in
2639 let tested = List.mem name funcs in
2642 failwithf "function %s has tests but does not test itself" name
2645 (* 'pr' prints to the current output file. *)
2646 let chan = ref stdout
2647 let pr fs = ksprintf (output_string !chan) fs
2649 (* Generate a header block in a number of standard styles. *)
2650 type comment_style = CStyle | HashStyle | OCamlStyle | HaskellStyle
2651 type license = GPLv2 | LGPLv2
2653 let generate_header comment license =
2654 let c = match comment with
2655 | CStyle -> pr "/* "; " *"
2656 | HashStyle -> pr "# "; "#"
2657 | OCamlStyle -> pr "(* "; " *"
2658 | HaskellStyle -> pr "{- "; " " in
2659 pr "libguestfs generated file\n";
2660 pr "%s WARNING: THIS FILE IS GENERATED BY 'src/generator.ml'.\n" c;
2661 pr "%s ANY CHANGES YOU MAKE TO THIS FILE WILL BE LOST.\n" c;
2663 pr "%s Copyright (C) 2009 Red Hat Inc.\n" c;
2667 pr "%s This program is free software; you can redistribute it and/or modify\n" c;
2668 pr "%s it under the terms of the GNU General Public License as published by\n" c;
2669 pr "%s the Free Software Foundation; either version 2 of the License, or\n" c;
2670 pr "%s (at your option) any later version.\n" c;
2672 pr "%s This program is distributed in the hope that it will be useful,\n" c;
2673 pr "%s but WITHOUT ANY WARRANTY; without even the implied warranty of\n" c;
2674 pr "%s MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" c;
2675 pr "%s GNU General Public License for more details.\n" c;
2677 pr "%s You should have received a copy of the GNU General Public License along\n" c;
2678 pr "%s with this program; if not, write to the Free Software Foundation, Inc.,\n" c;
2679 pr "%s 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\n" c;
2682 pr "%s This library is free software; you can redistribute it and/or\n" c;
2683 pr "%s modify it under the terms of the GNU Lesser General Public\n" c;
2684 pr "%s License as published by the Free Software Foundation; either\n" c;
2685 pr "%s version 2 of the License, or (at your option) any later version.\n" c;
2687 pr "%s This library is distributed in the hope that it will be useful,\n" c;
2688 pr "%s but WITHOUT ANY WARRANTY; without even the implied warranty of\n" c;
2689 pr "%s MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n" c;
2690 pr "%s Lesser General Public License for more details.\n" c;
2692 pr "%s You should have received a copy of the GNU Lesser General Public\n" c;
2693 pr "%s License along with this library; if not, write to the Free Software\n" c;
2694 pr "%s Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n" c;
2697 | CStyle -> pr " */\n"
2699 | OCamlStyle -> pr " *)\n"
2700 | HaskellStyle -> pr "-}\n"
2704 (* Start of main code generation functions below this line. *)
2706 (* Generate the pod documentation for the C API. *)
2707 let rec generate_actions_pod () =
2709 fun (shortname, style, _, flags, _, _, longdesc) ->
2710 if not (List.mem NotInDocs flags) then (
2711 let name = "guestfs_" ^ shortname in
2712 pr "=head2 %s\n\n" name;
2714 generate_prototype ~extern:false ~handle:"handle" name style;
2716 pr "%s\n\n" longdesc;
2717 (match fst style with
2719 pr "This function returns 0 on success or -1 on error.\n\n"
2721 pr "On error this function returns -1.\n\n"
2723 pr "On error this function returns -1.\n\n"
2725 pr "This function returns a C truth value on success or -1 on error.\n\n"
2727 pr "This function returns a string, or NULL on error.
2728 The string is owned by the guest handle and must I<not> be freed.\n\n"
2730 pr "This function returns a string, or NULL on error.
2731 I<The caller must free the returned string after use>.\n\n"
2733 pr "This function returns a NULL-terminated array of strings
2734 (like L<environ(3)>), or NULL if there was an error.
2735 I<The caller must free the strings and the array after use>.\n\n"
2737 pr "This function returns a C<struct guestfs_int_bool *>,
2738 or NULL if there was an error.
2739 I<The caller must call C<guestfs_free_int_bool> after use>.\n\n"
2741 pr "This function returns a C<struct guestfs_lvm_pv_list *>
2742 (see E<lt>guestfs-structs.hE<gt>),
2743 or NULL if there was an error.
2744 I<The caller must call C<guestfs_free_lvm_pv_list> after use>.\n\n"
2746 pr "This function returns a C<struct guestfs_lvm_vg_list *>
2747 (see E<lt>guestfs-structs.hE<gt>),
2748 or NULL if there was an error.
2749 I<The caller must call C<guestfs_free_lvm_vg_list> after use>.\n\n"
2751 pr "This function returns a C<struct guestfs_lvm_lv_list *>
2752 (see E<lt>guestfs-structs.hE<gt>),
2753 or NULL if there was an error.
2754 I<The caller must call C<guestfs_free_lvm_lv_list> after use>.\n\n"
2756 pr "This function returns a C<struct guestfs_stat *>
2757 (see L<stat(2)> and E<lt>guestfs-structs.hE<gt>),
2758 or NULL if there was an error.
2759 I<The caller must call C<free> after use>.\n\n"
2761 pr "This function returns a C<struct guestfs_statvfs *>
2762 (see L<statvfs(2)> and E<lt>guestfs-structs.hE<gt>),
2763 or NULL if there was an error.
2764 I<The caller must call C<free> after use>.\n\n"
2766 pr "This function returns a NULL-terminated array of
2767 strings, or NULL if there was an error.
2768 The array of strings will always have length C<2n+1>, where
2769 C<n> keys and values alternate, followed by the trailing NULL entry.
2770 I<The caller must free the strings and the array after use>.\n\n"
2772 if List.mem ProtocolLimitWarning flags then
2773 pr "%s\n\n" protocol_limit_warning;
2774 if List.mem DangerWillRobinson flags then
2775 pr "%s\n\n" danger_will_robinson
2777 ) all_functions_sorted
2779 and generate_structs_pod () =
2780 (* LVM structs documentation. *)
2783 pr "=head2 guestfs_lvm_%s\n" typ;
2785 pr " struct guestfs_lvm_%s {\n" typ;
2788 | name, `String -> pr " char *%s;\n" name
2790 pr " /* The next field is NOT nul-terminated, be careful when printing it: */\n";
2791 pr " char %s[32];\n" name
2792 | name, `Bytes -> pr " uint64_t %s;\n" name
2793 | name, `Int -> pr " int64_t %s;\n" name
2794 | name, `OptPercent ->
2795 pr " /* The next field is [0..100] or -1 meaning 'not present': */\n";
2796 pr " float %s;\n" name
2799 pr " struct guestfs_lvm_%s_list {\n" typ;
2800 pr " uint32_t len; /* Number of elements in list. */\n";
2801 pr " struct guestfs_lvm_%s *val; /* Elements. */\n" typ;
2804 pr " void guestfs_free_lvm_%s_list (struct guestfs_free_lvm_%s_list *);\n"
2807 ) ["pv", pv_cols; "vg", vg_cols; "lv", lv_cols]
2809 (* Generate the protocol (XDR) file, 'guestfs_protocol.x' and
2810 * indirectly 'guestfs_protocol.h' and 'guestfs_protocol.c'.
2812 * We have to use an underscore instead of a dash because otherwise
2813 * rpcgen generates incorrect code.
2815 * This header is NOT exported to clients, but see also generate_structs_h.
2817 and generate_xdr () =
2818 generate_header CStyle LGPLv2;
2820 (* This has to be defined to get around a limitation in Sun's rpcgen. *)
2821 pr "typedef string str<>;\n";
2824 (* LVM internal structures. *)
2828 pr "struct guestfs_lvm_int_%s {\n" typ;
2830 | name, `String -> pr " string %s<>;\n" name
2831 | name, `UUID -> pr " opaque %s[32];\n" name
2832 | name, `Bytes -> pr " hyper %s;\n" name
2833 | name, `Int -> pr " hyper %s;\n" name
2834 | name, `OptPercent -> pr " float %s;\n" name
2838 pr "typedef struct guestfs_lvm_int_%s guestfs_lvm_int_%s_list<>;\n" typ typ;
2840 ) ["pv", pv_cols; "vg", vg_cols; "lv", lv_cols];
2842 (* Stat internal structures. *)
2846 pr "struct guestfs_int_%s {\n" typ;
2848 | name, `Int -> pr " hyper %s;\n" name
2852 ) ["stat", stat_cols; "statvfs", statvfs_cols];
2855 fun (shortname, style, _, _, _, _, _) ->
2856 let name = "guestfs_" ^ shortname in
2858 (match snd style with
2861 pr "struct %s_args {\n" name;
2864 | String n -> pr " string %s<>;\n" n
2865 | OptString n -> pr " str *%s;\n" n
2866 | StringList n -> pr " str %s<>;\n" n
2867 | Bool n -> pr " bool %s;\n" n
2868 | Int n -> pr " int %s;\n" n
2869 | FileIn _ | FileOut _ -> ()
2873 (match fst style with
2876 pr "struct %s_ret {\n" name;
2880 pr "struct %s_ret {\n" name;
2881 pr " hyper %s;\n" n;
2884 pr "struct %s_ret {\n" name;
2888 failwithf "RConstString cannot be returned from a daemon function"
2890 pr "struct %s_ret {\n" name;
2891 pr " string %s<>;\n" n;
2894 pr "struct %s_ret {\n" name;
2895 pr " str %s<>;\n" n;
2898 pr "struct %s_ret {\n" name;
2903 pr "struct %s_ret {\n" name;
2904 pr " guestfs_lvm_int_pv_list %s;\n" n;
2907 pr "struct %s_ret {\n" name;
2908 pr " guestfs_lvm_int_vg_list %s;\n" n;
2911 pr "struct %s_ret {\n" name;
2912 pr " guestfs_lvm_int_lv_list %s;\n" n;
2915 pr "struct %s_ret {\n" name;
2916 pr " guestfs_int_stat %s;\n" n;
2919 pr "struct %s_ret {\n" name;
2920 pr " guestfs_int_statvfs %s;\n" n;
2923 pr "struct %s_ret {\n" name;
2924 pr " str %s<>;\n" n;
2929 (* Table of procedure numbers. *)
2930 pr "enum guestfs_procedure {\n";
2932 fun (shortname, _, proc_nr, _, _, _, _) ->
2933 pr " GUESTFS_PROC_%s = %d,\n" (String.uppercase shortname) proc_nr
2935 pr " GUESTFS_PROC_NR_PROCS\n";
2939 (* Having to choose a maximum message size is annoying for several
2940 * reasons (it limits what we can do in the API), but it (a) makes
2941 * the protocol a lot simpler, and (b) provides a bound on the size
2942 * of the daemon which operates in limited memory space. For large
2943 * file transfers you should use FTP.
2945 pr "const GUESTFS_MESSAGE_MAX = %d;\n" (4 * 1024 * 1024);
2948 (* Message header, etc. *)
2950 /* The communication protocol is now documented in the guestfs(3)
2954 const GUESTFS_PROGRAM = 0x2000F5F5;
2955 const GUESTFS_PROTOCOL_VERSION = 1;
2957 /* These constants must be larger than any possible message length. */
2958 const GUESTFS_LAUNCH_FLAG = 0xf5f55ff5;
2959 const GUESTFS_CANCEL_FLAG = 0xffffeeee;
2961 enum guestfs_message_direction {
2962 GUESTFS_DIRECTION_CALL = 0, /* client -> daemon */
2963 GUESTFS_DIRECTION_REPLY = 1 /* daemon -> client */
2966 enum guestfs_message_status {
2967 GUESTFS_STATUS_OK = 0,
2968 GUESTFS_STATUS_ERROR = 1
2971 const GUESTFS_ERROR_LEN = 256;
2973 struct guestfs_message_error {
2974 string error_message<GUESTFS_ERROR_LEN>;
2977 struct guestfs_message_header {
2978 unsigned prog; /* GUESTFS_PROGRAM */
2979 unsigned vers; /* GUESTFS_PROTOCOL_VERSION */
2980 guestfs_procedure proc; /* GUESTFS_PROC_x */
2981 guestfs_message_direction direction;
2982 unsigned serial; /* message serial number */
2983 guestfs_message_status status;
2986 const GUESTFS_MAX_CHUNK_SIZE = 8192;
2988 struct guestfs_chunk {
2989 int cancel; /* if non-zero, transfer is cancelled */
2990 /* data size is 0 bytes if the transfer has finished successfully */
2991 opaque data<GUESTFS_MAX_CHUNK_SIZE>;
2995 (* Generate the guestfs-structs.h file. *)
2996 and generate_structs_h () =
2997 generate_header CStyle LGPLv2;
2999 (* This is a public exported header file containing various
3000 * structures. The structures are carefully written to have
3001 * exactly the same in-memory format as the XDR structures that
3002 * we use on the wire to the daemon. The reason for creating
3003 * copies of these structures here is just so we don't have to
3004 * export the whole of guestfs_protocol.h (which includes much
3005 * unrelated and XDR-dependent stuff that we don't want to be
3006 * public, or required by clients).
3008 * To reiterate, we will pass these structures to and from the
3009 * client with a simple assignment or memcpy, so the format
3010 * must be identical to what rpcgen / the RFC defines.
3013 (* guestfs_int_bool structure. *)
3014 pr "struct guestfs_int_bool {\n";
3020 (* LVM public structures. *)
3024 pr "struct guestfs_lvm_%s {\n" typ;
3027 | name, `String -> pr " char *%s;\n" name
3028 | name, `UUID -> pr " char %s[32]; /* this is NOT nul-terminated, be careful when printing */\n" name
3029 | name, `Bytes -> pr " uint64_t %s;\n" name
3030 | name, `Int -> pr " int64_t %s;\n" name
3031 | name, `OptPercent -> pr " float %s; /* [0..100] or -1 */\n" name
3035 pr "struct guestfs_lvm_%s_list {\n" typ;
3036 pr " uint32_t len;\n";
3037 pr " struct guestfs_lvm_%s *val;\n" typ;
3040 ) ["pv", pv_cols; "vg", vg_cols; "lv", lv_cols];
3042 (* Stat structures. *)
3046 pr "struct guestfs_%s {\n" typ;
3049 | name, `Int -> pr " int64_t %s;\n" name
3053 ) ["stat", stat_cols; "statvfs", statvfs_cols]
3055 (* Generate the guestfs-actions.h file. *)
3056 and generate_actions_h () =
3057 generate_header CStyle LGPLv2;
3059 fun (shortname, style, _, _, _, _, _) ->
3060 let name = "guestfs_" ^ shortname in
3061 generate_prototype ~single_line:true ~newline:true ~handle:"handle"
3065 (* Generate the client-side dispatch stubs. *)
3066 and generate_client_actions () =
3067 generate_header CStyle LGPLv2;
3073 #include \"guestfs.h\"
3074 #include \"guestfs_protocol.h\"
3076 #define error guestfs_error
3077 #define perrorf guestfs_perrorf
3078 #define safe_malloc guestfs_safe_malloc
3079 #define safe_realloc guestfs_safe_realloc
3080 #define safe_strdup guestfs_safe_strdup
3081 #define safe_memdup guestfs_safe_memdup
3083 /* Check the return message from a call for validity. */
3085 check_reply_header (guestfs_h *g,
3086 const struct guestfs_message_header *hdr,
3087 int proc_nr, int serial)
3089 if (hdr->prog != GUESTFS_PROGRAM) {
3090 error (g, \"wrong program (%%d/%%d)\", hdr->prog, GUESTFS_PROGRAM);
3093 if (hdr->vers != GUESTFS_PROTOCOL_VERSION) {
3094 error (g, \"wrong protocol version (%%d/%%d)\",
3095 hdr->vers, GUESTFS_PROTOCOL_VERSION);
3098 if (hdr->direction != GUESTFS_DIRECTION_REPLY) {
3099 error (g, \"unexpected message direction (%%d/%%d)\",
3100 hdr->direction, GUESTFS_DIRECTION_REPLY);
3103 if (hdr->proc != proc_nr) {
3104 error (g, \"unexpected procedure number (%%d/%%d)\", hdr->proc, proc_nr);
3107 if (hdr->serial != serial) {
3108 error (g, \"unexpected serial (%%d/%%d)\", hdr->serial, serial);
3115 /* Check we are in the right state to run a high-level action. */
3117 check_state (guestfs_h *g, const char *caller)
3119 if (!guestfs_is_ready (g)) {
3120 if (guestfs_is_config (g))
3121 error (g, \"%%s: call launch() before using this function\",
3123 else if (guestfs_is_launching (g))
3124 error (g, \"%%s: call wait_ready() before using this function\",
3127 error (g, \"%%s called from the wrong state, %%d != READY\",
3128 caller, guestfs_get_state (g));
3136 (* Client-side stubs for each function. *)
3138 fun (shortname, style, _, _, _, _, _) ->
3139 let name = "guestfs_" ^ shortname in
3141 (* Generate the context struct which stores the high-level
3142 * state between callback functions.
3144 pr "struct %s_ctx {\n" shortname;
3145 pr " /* This flag is set by the callbacks, so we know we've done\n";
3146 pr " * the callbacks as expected, and in the right sequence.\n";
3147 pr " * 0 = not called, 1 = reply_cb called.\n";
3149 pr " int cb_sequence;\n";
3150 pr " struct guestfs_message_header hdr;\n";
3151 pr " struct guestfs_message_error err;\n";
3152 (match fst style with
3155 failwithf "RConstString cannot be returned from a daemon function"
3157 | RBool _ | RString _ | RStringList _
3159 | RPVList _ | RVGList _ | RLVList _
3160 | RStat _ | RStatVFS _
3162 pr " struct %s_ret ret;\n" name
3167 (* Generate the reply callback function. *)
3168 pr "static void %s_reply_cb (guestfs_h *g, void *data, XDR *xdr)\n" shortname;
3170 pr " guestfs_main_loop *ml = guestfs_get_main_loop (g);\n";
3171 pr " struct %s_ctx *ctx = (struct %s_ctx *) data;\n" shortname shortname;
3173 pr " /* This should definitely not happen. */\n";
3174 pr " if (ctx->cb_sequence != 0) {\n";
3175 pr " ctx->cb_sequence = 9999;\n";
3176 pr " error (g, \"%%s: internal error: reply callback called twice\", \"%s\");\n" name;
3180 pr " ml->main_loop_quit (ml, g);\n";
3182 pr " if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {\n";
3183 pr " error (g, \"%%s: failed to parse reply header\", \"%s\");\n" name;
3186 pr " if (ctx->hdr.status == GUESTFS_STATUS_ERROR) {\n";
3187 pr " if (!xdr_guestfs_message_error (xdr, &ctx->err)) {\n";
3188 pr " error (g, \"%%s: failed to parse reply error\", \"%s\");\n"
3195 (match fst style with
3198 failwithf "RConstString cannot be returned from a daemon function"
3200 | RBool _ | RString _ | RStringList _
3202 | RPVList _ | RVGList _ | RLVList _
3203 | RStat _ | RStatVFS _
3205 pr " if (!xdr_%s_ret (xdr, &ctx->ret)) {\n" name;
3206 pr " error (g, \"%%s: failed to parse reply\", \"%s\");\n" name;
3212 pr " ctx->cb_sequence = 1;\n";
3215 (* Generate the action stub. *)
3216 generate_prototype ~extern:false ~semicolon:false ~newline:true
3217 ~handle:"g" name style;
3220 match fst style with
3221 | RErr | RInt _ | RInt64 _ | RBool _ -> "-1"
3223 failwithf "RConstString cannot be returned from a daemon function"
3224 | RString _ | RStringList _ | RIntBool _
3225 | RPVList _ | RVGList _ | RLVList _
3226 | RStat _ | RStatVFS _
3232 (match snd style with
3234 | _ -> pr " struct %s_args args;\n" name
3237 pr " struct %s_ctx ctx;\n" shortname;
3238 pr " guestfs_main_loop *ml = guestfs_get_main_loop (g);\n";
3239 pr " int serial;\n";
3241 pr " if (check_state (g, \"%s\") == -1) return %s;\n" name error_code;
3242 pr " guestfs_set_busy (g);\n";
3244 pr " memset (&ctx, 0, sizeof ctx);\n";
3247 (* Send the main header and arguments. *)
3248 (match snd style with
3250 pr " serial = guestfs__send_sync (g, GUESTFS_PROC_%s, NULL, NULL);\n"
3251 (String.uppercase shortname)
3256 pr " args.%s = (char *) %s;\n" n n
3258 pr " args.%s = %s ? (char **) &%s : NULL;\n" n n n
3260 pr " args.%s.%s_val = (char **) %s;\n" n n n;
3261 pr " for (args.%s.%s_len = 0; %s[args.%s.%s_len]; args.%s.%s_len++) ;\n" n n n n n n n;
3263 pr " args.%s = %s;\n" n n
3265 pr " args.%s = %s;\n" n n
3266 | FileIn _ | FileOut _ -> ()
3268 pr " serial = guestfs__send_sync (g, GUESTFS_PROC_%s,\n"
3269 (String.uppercase shortname);
3270 pr " (xdrproc_t) xdr_%s_args, (char *) &args);\n"
3273 pr " if (serial == -1) {\n";
3274 pr " guestfs_end_busy (g);\n";
3275 pr " return %s;\n" error_code;
3279 (* Send any additional files (FileIn) requested. *)
3280 let need_read_reply_label = ref false in
3287 pr " r = guestfs__send_file_sync (g, %s);\n" n;
3288 pr " if (r == -1) {\n";
3289 pr " guestfs_end_busy (g);\n";
3290 pr " return %s;\n" error_code;
3292 pr " if (r == -2) /* daemon cancelled */\n";
3293 pr " goto read_reply;\n";
3294 need_read_reply_label := true;
3300 (* Wait for the reply from the remote end. *)
3301 if !need_read_reply_label then pr " read_reply:\n";
3302 pr " guestfs__switch_to_receiving (g);\n";
3303 pr " ctx.cb_sequence = 0;\n";
3304 pr " guestfs_set_reply_callback (g, %s_reply_cb, &ctx);\n" shortname;
3305 pr " (void) ml->main_loop_run (ml, g);\n";
3306 pr " guestfs_set_reply_callback (g, NULL, NULL);\n";
3307 pr " if (ctx.cb_sequence != 1) {\n";
3308 pr " error (g, \"%%s reply failed, see earlier error messages\", \"%s\");\n" name;
3309 pr " guestfs_end_busy (g);\n";
3310 pr " return %s;\n" error_code;
3314 pr " if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_%s, serial) == -1) {\n"
3315 (String.uppercase shortname);
3316 pr " guestfs_end_busy (g);\n";
3317 pr " return %s;\n" error_code;
3321 pr " if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {\n";
3322 pr " error (g, \"%%s\", ctx.err.error_message);\n";
3323 pr " free (ctx.err.error_message);\n";
3324 pr " guestfs_end_busy (g);\n";
3325 pr " return %s;\n" error_code;
3329 (* Expecting to receive further files (FileOut)? *)
3333 pr " if (guestfs__receive_file_sync (g, %s) == -1) {\n" n;
3334 pr " guestfs_end_busy (g);\n";
3335 pr " return %s;\n" error_code;
3341 pr " guestfs_end_busy (g);\n";
3343 (match fst style with
3344 | RErr -> pr " return 0;\n"
3345 | RInt n | RInt64 n | RBool n ->
3346 pr " return ctx.ret.%s;\n" n
3348 failwithf "RConstString cannot be returned from a daemon function"
3350 pr " return ctx.ret.%s; /* caller will free */\n" n
3351 | RStringList n | RHashtable n ->
3352 pr " /* caller will free this, but we need to add a NULL entry */\n";
3353 pr " ctx.ret.%s.%s_val =\n" n n;
3354 pr " safe_realloc (g, ctx.ret.%s.%s_val,\n" n n;
3355 pr " sizeof (char *) * (ctx.ret.%s.%s_len + 1));\n"
3357 pr " ctx.ret.%s.%s_val[ctx.ret.%s.%s_len] = NULL;\n" n n n n;
3358 pr " return ctx.ret.%s.%s_val;\n" n n
3360 pr " /* caller with free this */\n";
3361 pr " return safe_memdup (g, &ctx.ret, sizeof (ctx.ret));\n"
3362 | RPVList n | RVGList n | RLVList n
3363 | RStat n | RStatVFS n ->
3364 pr " /* caller will free this */\n";
3365 pr " return safe_memdup (g, &ctx.ret.%s, sizeof (ctx.ret.%s));\n" n n
3371 (* Generate daemon/actions.h. *)
3372 and generate_daemon_actions_h () =
3373 generate_header CStyle GPLv2;
3375 pr "#include \"../src/guestfs_protocol.h\"\n";
3379 fun (name, style, _, _, _, _, _) ->
3381 ~single_line:true ~newline:true ~in_daemon:true ~prefix:"do_"
3385 (* Generate the server-side stubs. *)
3386 and generate_daemon_actions () =
3387 generate_header CStyle GPLv2;
3389 pr "#include <config.h>\n";
3391 pr "#include <stdio.h>\n";
3392 pr "#include <stdlib.h>\n";
3393 pr "#include <string.h>\n";
3394 pr "#include <inttypes.h>\n";
3395 pr "#include <ctype.h>\n";
3396 pr "#include <rpc/types.h>\n";
3397 pr "#include <rpc/xdr.h>\n";
3399 pr "#include \"daemon.h\"\n";
3400 pr "#include \"../src/guestfs_protocol.h\"\n";
3401 pr "#include \"actions.h\"\n";
3405 fun (name, style, _, _, _, _, _) ->
3406 (* Generate server-side stubs. *)
3407 pr "static void %s_stub (XDR *xdr_in)\n" name;
3410 match fst style with
3411 | RErr | RInt _ -> pr " int r;\n"; "-1"
3412 | RInt64 _ -> pr " int64_t r;\n"; "-1"
3413 | RBool _ -> pr " int r;\n"; "-1"
3415 failwithf "RConstString cannot be returned from a daemon function"
3416 | RString _ -> pr " char *r;\n"; "NULL"
3417 | RStringList _ | RHashtable _ -> pr " char **r;\n"; "NULL"
3418 | RIntBool _ -> pr " guestfs_%s_ret *r;\n" name; "NULL"
3419 | RPVList _ -> pr " guestfs_lvm_int_pv_list *r;\n"; "NULL"
3420 | RVGList _ -> pr " guestfs_lvm_int_vg_list *r;\n"; "NULL"
3421 | RLVList _ -> pr " guestfs_lvm_int_lv_list *r;\n"; "NULL"
3422 | RStat _ -> pr " guestfs_int_stat *r;\n"; "NULL"
3423 | RStatVFS _ -> pr " guestfs_int_statvfs *r;\n"; "NULL" in
3425 (match snd style with
3428 pr " struct guestfs_%s_args args;\n" name;
3432 | OptString n -> pr " const char *%s;\n" n
3433 | StringList n -> pr " char **%s;\n" n
3434 | Bool n -> pr " int %s;\n" n
3435 | Int n -> pr " int %s;\n" n
3436 | FileIn _ | FileOut _ -> ()
3441 (match snd style with
3444 pr " memset (&args, 0, sizeof args);\n";
3446 pr " if (!xdr_guestfs_%s_args (xdr_in, &args)) {\n" name;
3447 pr " reply_with_error (\"%%s: daemon failed to decode procedure arguments\", \"%s\");\n" name;
3452 | String n -> pr " %s = args.%s;\n" n n
3453 | OptString n -> pr " %s = args.%s ? *args.%s : NULL;\n" n n n
3455 pr " %s = realloc (args.%s.%s_val,\n" n n n;
3456 pr " sizeof (char *) * (args.%s.%s_len+1));\n" n n;
3457 pr " if (%s == NULL) {\n" n;
3458 pr " reply_with_perror (\"realloc\");\n";
3461 pr " %s[args.%s.%s_len] = NULL;\n" n n n;
3462 pr " args.%s.%s_val = %s;\n" n n n;
3463 | Bool n -> pr " %s = args.%s;\n" n n
3464 | Int n -> pr " %s = args.%s;\n" n n
3465 | FileIn _ | FileOut _ -> ()
3470 (* Don't want to call the impl with any FileIn or FileOut
3471 * parameters, since these go "outside" the RPC protocol.
3474 List.filter (function FileIn _ | FileOut _ -> false | _ -> true)
3476 pr " r = do_%s " name;
3477 generate_call_args argsnofile;
3480 pr " if (r == %s)\n" error_code;
3481 pr " /* do_%s has already called reply_with_error */\n" name;
3485 (* If there are any FileOut parameters, then the impl must
3486 * send its own reply.
3489 List.exists (function FileOut _ -> true | _ -> false) (snd style) in
3491 pr " /* do_%s has already sent a reply */\n" name
3493 match fst style with
3494 | RErr -> pr " reply (NULL, NULL);\n"
3495 | RInt n | RInt64 n | RBool n ->
3496 pr " struct guestfs_%s_ret ret;\n" name;
3497 pr " ret.%s = r;\n" n;
3498 pr " reply ((xdrproc_t) &xdr_guestfs_%s_ret, (char *) &ret);\n"
3501 failwithf "RConstString cannot be returned from a daemon function"
3503 pr " struct guestfs_%s_ret ret;\n" name;
3504 pr " ret.%s = r;\n" n;
3505 pr " reply ((xdrproc_t) &xdr_guestfs_%s_ret, (char *) &ret);\n"
3508 | RStringList n | RHashtable n ->
3509 pr " struct guestfs_%s_ret ret;\n" name;
3510 pr " ret.%s.%s_len = count_strings (r);\n" n n;
3511 pr " ret.%s.%s_val = r;\n" n n;
3512 pr " reply ((xdrproc_t) &xdr_guestfs_%s_ret, (char *) &ret);\n"
3514 pr " free_strings (r);\n"
3516 pr " reply ((xdrproc_t) xdr_guestfs_%s_ret, (char *) r);\n"
3518 pr " xdr_free ((xdrproc_t) xdr_guestfs_%s_ret, (char *) r);\n" name
3519 | RPVList n | RVGList n | RLVList n
3520 | RStat n | RStatVFS n ->
3521 pr " struct guestfs_%s_ret ret;\n" name;
3522 pr " ret.%s = *r;\n" n;
3523 pr " reply ((xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret);\n"
3525 pr " xdr_free ((xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret);\n"
3529 (* Free the args. *)
3530 (match snd style with
3535 pr " xdr_free ((xdrproc_t) xdr_guestfs_%s_args, (char *) &args);\n"
3542 (* Dispatch function. *)
3543 pr "void dispatch_incoming_message (XDR *xdr_in)\n";
3545 pr " switch (proc_nr) {\n";
3548 fun (name, style, _, _, _, _, _) ->
3549 pr " case GUESTFS_PROC_%s:\n" (String.uppercase name);
3550 pr " %s_stub (xdr_in);\n" name;
3555 pr " reply_with_error (\"dispatch_incoming_message: unknown procedure number %%d\", proc_nr);\n";
3560 (* LVM columns and tokenization functions. *)
3561 (* XXX This generates crap code. We should rethink how we
3567 pr "static const char *lvm_%s_cols = \"%s\";\n"
3568 typ (String.concat "," (List.map fst cols));
3571 pr "static int lvm_tokenize_%s (char *str, struct guestfs_lvm_int_%s *r)\n" typ typ;
3573 pr " char *tok, *p, *next;\n";
3577 pr " fprintf (stderr, \"%%s: <<%%s>>\\n\", __func__, str);\n";
3580 pr " if (!str) {\n";
3581 pr " fprintf (stderr, \"%%s: failed: passed a NULL string\\n\", __func__);\n";
3584 pr " if (!*str || isspace (*str)) {\n";
3585 pr " fprintf (stderr, \"%%s: failed: passed a empty string or one beginning with whitespace\\n\", __func__);\n";
3590 fun (name, coltype) ->
3591 pr " if (!tok) {\n";
3592 pr " fprintf (stderr, \"%%s: failed: string finished early, around token %%s\\n\", __func__, \"%s\");\n" name;
3595 pr " p = strchrnul (tok, ',');\n";
3596 pr " if (*p) next = p+1; else next = NULL;\n";
3597 pr " *p = '\\0';\n";
3600 pr " r->%s = strdup (tok);\n" name;
3601 pr " if (r->%s == NULL) {\n" name;
3602 pr " perror (\"strdup\");\n";
3606 pr " for (i = j = 0; i < 32; ++j) {\n";
3607 pr " if (tok[j] == '\\0') {\n";
3608 pr " fprintf (stderr, \"%%s: failed to parse UUID from '%%s'\\n\", __func__, tok);\n";
3610 pr " } else if (tok[j] != '-')\n";
3611 pr " r->%s[i++] = tok[j];\n" name;
3614 pr " if (sscanf (tok, \"%%\"SCNu64, &r->%s) != 1) {\n" name;
3615 pr " fprintf (stderr, \"%%s: failed to parse size '%%s' from token %%s\\n\", __func__, tok, \"%s\");\n" name;
3619 pr " if (sscanf (tok, \"%%\"SCNi64, &r->%s) != 1) {\n" name;
3620 pr " fprintf (stderr, \"%%s: failed to parse int '%%s' from token %%s\\n\", __func__, tok, \"%s\");\n" name;
3624 pr " if (tok[0] == '\\0')\n";
3625 pr " r->%s = -1;\n" name;
3626 pr " else if (sscanf (tok, \"%%f\", &r->%s) != 1) {\n" name;
3627 pr " fprintf (stderr, \"%%s: failed to parse float '%%s' from token %%s\\n\", __func__, tok, \"%s\");\n" name;
3631 pr " tok = next;\n";
3634 pr " if (tok != NULL) {\n";
3635 pr " fprintf (stderr, \"%%s: failed: extra tokens at end of string\\n\", __func__);\n";
3642 pr "guestfs_lvm_int_%s_list *\n" typ;
3643 pr "parse_command_line_%ss (void)\n" typ;
3645 pr " char *out, *err;\n";
3646 pr " char *p, *pend;\n";
3648 pr " guestfs_lvm_int_%s_list *ret;\n" typ;
3649 pr " void *newp;\n";
3651 pr " ret = malloc (sizeof *ret);\n";
3652 pr " if (!ret) {\n";
3653 pr " reply_with_perror (\"malloc\");\n";
3654 pr " return NULL;\n";
3657 pr " ret->guestfs_lvm_int_%s_list_len = 0;\n" typ;
3658 pr " ret->guestfs_lvm_int_%s_list_val = NULL;\n" typ;
3660 pr " r = command (&out, &err,\n";
3661 pr " \"/sbin/lvm\", \"%ss\",\n" typ;
3662 pr " \"-o\", lvm_%s_cols, \"--unbuffered\", \"--noheadings\",\n" typ;
3663 pr " \"--nosuffix\", \"--separator\", \",\", \"--units\", \"b\", NULL);\n";
3664 pr " if (r == -1) {\n";
3665 pr " reply_with_error (\"%%s\", err);\n";
3666 pr " free (out);\n";
3667 pr " free (err);\n";
3668 pr " free (ret);\n";
3669 pr " return NULL;\n";
3672 pr " free (err);\n";
3674 pr " /* Tokenize each line of the output. */\n";
3677 pr " while (p) {\n";
3678 pr " pend = strchr (p, '\\n'); /* Get the next line of output. */\n";
3679 pr " if (pend) {\n";
3680 pr " *pend = '\\0';\n";
3684 pr " while (*p && isspace (*p)) /* Skip any leading whitespace. */\n";
3687 pr " if (!*p) { /* Empty line? Skip it. */\n";
3692 pr " /* Allocate some space to store this next entry. */\n";
3693 pr " newp = realloc (ret->guestfs_lvm_int_%s_list_val,\n" typ;
3694 pr " sizeof (guestfs_lvm_int_%s) * (i+1));\n" typ;
3695 pr " if (newp == NULL) {\n";
3696 pr " reply_with_perror (\"realloc\");\n";
3697 pr " free (ret->guestfs_lvm_int_%s_list_val);\n" typ;
3698 pr " free (ret);\n";
3699 pr " free (out);\n";
3700 pr " return NULL;\n";
3702 pr " ret->guestfs_lvm_int_%s_list_val = newp;\n" typ;
3704 pr " /* Tokenize the next entry. */\n";
3705 pr " r = lvm_tokenize_%s (p, &ret->guestfs_lvm_int_%s_list_val[i]);\n" typ typ;
3706 pr " if (r == -1) {\n";
3707 pr " reply_with_error (\"failed to parse output of '%ss' command\");\n" typ;
3708 pr " free (ret->guestfs_lvm_int_%s_list_val);\n" typ;
3709 pr " free (ret);\n";
3710 pr " free (out);\n";
3711 pr " return NULL;\n";
3718 pr " ret->guestfs_lvm_int_%s_list_len = i;\n" typ;
3720 pr " free (out);\n";
3721 pr " return ret;\n";
3724 ) ["pv", pv_cols; "vg", vg_cols; "lv", lv_cols]
3726 (* Generate the tests. *)
3727 and generate_tests () =
3728 generate_header CStyle GPLv2;
3735 #include <sys/types.h>
3738 #include \"guestfs.h\"
3740 static guestfs_h *g;
3741 static int suppress_error = 0;
3743 /* This will be 's' or 'h' depending on whether the guest kernel
3744 * names IDE devices /dev/sd* or /dev/hd*.
3746 static char devchar = 's';
3748 static void print_error (guestfs_h *g, void *data, const char *msg)
3750 if (!suppress_error)
3751 fprintf (stderr, \"%%s\\n\", msg);
3754 static void print_strings (char * const * const argv)
3758 for (argc = 0; argv[argc] != NULL; ++argc)
3759 printf (\"\\t%%s\\n\", argv[argc]);
3763 static void print_table (char * const * const argv)
3767 for (i = 0; argv[i] != NULL; i += 2)
3768 printf (\"%%s: %%s\\n\", argv[i], argv[i+1]);
3772 static void no_test_warnings (void)
3778 | name, _, _, _, [], _, _ ->
3779 pr " fprintf (stderr, \"warning: \\\"guestfs_%s\\\" has no tests\\n\");\n" name
3780 | name, _, _, _, tests, _, _ -> ()
3786 (* Generate the actual tests. Note that we generate the tests
3787 * in reverse order, deliberately, so that (in general) the
3788 * newest tests run first. This makes it quicker and easier to
3793 fun (name, _, _, _, tests, _, _) ->
3794 mapi (generate_one_test name) tests
3795 ) (List.rev all_functions) in
3796 let test_names = List.concat test_names in
3797 let nr_tests = List.length test_names in
3800 int main (int argc, char *argv[])
3805 const char *filename;
3807 int nr_tests, test_num = 0;
3810 no_test_warnings ();
3812 g = guestfs_create ();
3814 printf (\"guestfs_create FAILED\\n\");
3818 guestfs_set_error_handler (g, print_error, NULL);
3820 guestfs_set_path (g, \"../appliance\");
3822 filename = \"test1.img\";
3823 fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666);
3828 if (lseek (fd, %d, SEEK_SET) == -1) {
3834 if (write (fd, &c, 1) == -1) {
3840 if (close (fd) == -1) {
3845 if (guestfs_add_drive (g, filename) == -1) {
3846 printf (\"guestfs_add_drive %%s FAILED\\n\", filename);
3850 filename = \"test2.img\";
3851 fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666);
3856 if (lseek (fd, %d, SEEK_SET) == -1) {
3862 if (write (fd, &c, 1) == -1) {
3868 if (close (fd) == -1) {
3873 if (guestfs_add_drive (g, filename) == -1) {
3874 printf (\"guestfs_add_drive %%s FAILED\\n\", filename);
3878 filename = \"test3.img\";
3879 fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666);
3884 if (lseek (fd, %d, SEEK_SET) == -1) {
3890 if (write (fd, &c, 1) == -1) {
3896 if (close (fd) == -1) {
3901 if (guestfs_add_drive (g, filename) == -1) {
3902 printf (\"guestfs_add_drive %%s FAILED\\n\", filename);
3906 if (guestfs_launch (g) == -1) {
3907 printf (\"guestfs_launch FAILED\\n\");
3910 if (guestfs_wait_ready (g) == -1) {
3911 printf (\"guestfs_wait_ready FAILED\\n\");
3915 /* Detect if the appliance uses /dev/sd* or /dev/hd* in device
3916 * names. This changed between RHEL 5 and RHEL 6 so we have to
3919 devs = guestfs_list_devices (g);
3920 if (devs == NULL || devs[0] == NULL) {
3921 printf (\"guestfs_list_devices FAILED\\n\");
3924 if (strncmp (devs[0], \"/dev/sd\", 7) == 0)
3926 else if (strncmp (devs[0], \"/dev/hd\", 7) == 0)
3929 printf (\"guestfs_list_devices returned unexpected string '%%s'\\n\",
3933 for (i = 0; devs[i] != NULL; ++i)
3939 " (500 * 1024 * 1024) (50 * 1024 * 1024) (10 * 1024 * 1024) nr_tests;
3943 pr " test_num++;\n";
3944 pr " printf (\"%%3d/%%3d %s\\n\", test_num, nr_tests);\n" test_name;
3945 pr " if (%s () == -1) {\n" test_name;
3946 pr " printf (\"%s FAILED\\n\");\n" test_name;
3952 pr " guestfs_close (g);\n";
3953 pr " unlink (\"test1.img\");\n";
3954 pr " unlink (\"test2.img\");\n";
3955 pr " unlink (\"test3.img\");\n";
3958 pr " if (failed > 0) {\n";
3959 pr " printf (\"***** %%d / %%d tests FAILED *****\\n\", failed, nr_tests);\n";
3967 and generate_one_test name i (init, prereq, test) =
3968 let test_name = sprintf "test_%s_%d" name i in
3971 static int %s_skip (void)
3975 str = getenv (\"SKIP_%s\");
3976 if (str && strcmp (str, \"1\") == 0) return 1;
3977 str = getenv (\"SKIP_TEST_%s\");
3978 if (str && strcmp (str, \"1\") == 0) return 1;
3982 " test_name (String.uppercase test_name) (String.uppercase name);
3985 | Disabled | Always -> ()
3986 | If code | Unless code ->
3987 pr "static int %s_prereq (void)\n" test_name;
3995 static int %s (void)
3998 printf (\"%%s skipped (reason: SKIP_TEST_* variable set)\\n\", \"%s\");
4002 " test_name test_name test_name;
4006 pr " printf (\"%%s skipped (reason: test disabled in generator)\\n\", \"%s\");\n" test_name
4008 pr " if (! %s_prereq ()) {\n" test_name;
4009 pr " printf (\"%%s skipped (reason: test prerequisite)\\n\", \"%s\");\n" test_name;
4013 generate_one_test_body name i test_name init test;
4015 pr " if (%s_prereq ()) {\n" test_name;
4016 pr " printf (\"%%s skipped (reason: test prerequisite)\\n\", \"%s\");\n" test_name;
4020 generate_one_test_body name i test_name init test;
4022 generate_one_test_body name i test_name init test
4030 and generate_one_test_body name i test_name init test =
4034 pr " /* InitNone|InitEmpty for %s */\n" test_name;
4035 List.iter (generate_test_command_call test_name)
4036 [["blockdev_setrw"; "/dev/sda"];
4040 pr " /* InitBasicFS for %s: create ext2 on /dev/sda1 */\n" test_name;
4041 List.iter (generate_test_command_call test_name)
4042 [["blockdev_setrw"; "/dev/sda"];
4045 ["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
4046 ["mkfs"; "ext2"; "/dev/sda1"];
4047 ["mount"; "/dev/sda1"; "/"]]
4048 | InitBasicFSonLVM ->
4049 pr " /* InitBasicFSonLVM for %s: create ext2 on /dev/VG/LV */\n"
4051 List.iter (generate_test_command_call test_name)
4052 [["blockdev_setrw"; "/dev/sda"];
4055 ["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
4056 ["pvcreate"; "/dev/sda1"];
4057 ["vgcreate"; "VG"; "/dev/sda1"];
4058 ["lvcreate"; "LV"; "VG"; "8"];
4059 ["mkfs"; "ext2"; "/dev/VG/LV"];
4060 ["mount"; "/dev/VG/LV"; "/"]]
4063 let get_seq_last = function
4065 failwithf "%s: you cannot use [] (empty list) when expecting a command"
4068 let seq = List.rev seq in
4069 List.rev (List.tl seq), List.hd seq
4074 pr " /* TestRun for %s (%d) */\n" name i;
4075 List.iter (generate_test_command_call test_name) seq
4076 | TestOutput (seq, expected) ->
4077 pr " /* TestOutput for %s (%d) */\n" name i;
4078 pr " char expected[] = \"%s\";\n" (c_quote expected);
4079 if String.length expected > 7 &&
4080 String.sub expected 0 7 = "/dev/sd" then
4081 pr " expected[5] = devchar;\n";
4082 let seq, last = get_seq_last seq in
4084 pr " if (strcmp (r, expected) != 0) {\n";
4085 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r);\n" test_name;
4089 List.iter (generate_test_command_call test_name) seq;
4090 generate_test_command_call ~test test_name last
4091 | TestOutputList (seq, expected) ->
4092 pr " /* TestOutputList for %s (%d) */\n" name i;
4093 let seq, last = get_seq_last seq in
4097 pr " if (!r[%d]) {\n" i;
4098 pr " fprintf (stderr, \"%s: short list returned from command\\n\");\n" test_name;
4099 pr " print_strings (r);\n";
4103 pr " char expected[] = \"%s\";\n" (c_quote str);
4104 if String.length str > 7 && String.sub str 0 7 = "/dev/sd" then
4105 pr " expected[5] = devchar;\n";
4106 pr " if (strcmp (r[%d], expected) != 0) {\n" i;
4107 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r[%d]);\n" test_name i;
4112 pr " if (r[%d] != NULL) {\n" (List.length expected);
4113 pr " fprintf (stderr, \"%s: extra elements returned from command\\n\");\n"
4115 pr " print_strings (r);\n";
4119 List.iter (generate_test_command_call test_name) seq;
4120 generate_test_command_call ~test test_name last
4121 | TestOutputInt (seq, expected) ->
4122 pr " /* TestOutputInt for %s (%d) */\n" name i;
4123 let seq, last = get_seq_last seq in
4125 pr " if (r != %d) {\n" expected;
4126 pr " fprintf (stderr, \"%s: expected %d but got %%d\\n\","
4132 List.iter (generate_test_command_call test_name) seq;
4133 generate_test_command_call ~test test_name last
4134 | TestOutputTrue seq ->
4135 pr " /* TestOutputTrue for %s (%d) */\n" name i;
4136 let seq, last = get_seq_last seq in
4139 pr " fprintf (stderr, \"%s: expected true, got false\\n\");\n"
4144 List.iter (generate_test_command_call test_name) seq;
4145 generate_test_command_call ~test test_name last
4146 | TestOutputFalse seq ->
4147 pr " /* TestOutputFalse for %s (%d) */\n" name i;
4148 let seq, last = get_seq_last seq in
4151 pr " fprintf (stderr, \"%s: expected false, got true\\n\");\n"
4156 List.iter (generate_test_command_call test_name) seq;
4157 generate_test_command_call ~test test_name last
4158 | TestOutputLength (seq, expected) ->
4159 pr " /* TestOutputLength for %s (%d) */\n" name i;
4160 let seq, last = get_seq_last seq in
4163 pr " for (j = 0; j < %d; ++j)\n" expected;
4164 pr " if (r[j] == NULL) {\n";
4165 pr " fprintf (stderr, \"%s: short list returned\\n\");\n"
4167 pr " print_strings (r);\n";
4170 pr " if (r[j] != NULL) {\n";
4171 pr " fprintf (stderr, \"%s: long list returned\\n\");\n"
4173 pr " print_strings (r);\n";
4177 List.iter (generate_test_command_call test_name) seq;
4178 generate_test_command_call ~test test_name last
4179 | TestOutputStruct (seq, checks) ->
4180 pr " /* TestOutputStruct for %s (%d) */\n" name i;
4181 let seq, last = get_seq_last seq in
4185 | CompareWithInt (field, expected) ->
4186 pr " if (r->%s != %d) {\n" field expected;
4187 pr " fprintf (stderr, \"%s: %s was %%d, expected %d\\n\",\n"
4188 test_name field expected;
4189 pr " (int) r->%s);\n" field;
4192 | CompareWithString (field, expected) ->
4193 pr " if (strcmp (r->%s, \"%s\") != 0) {\n" field expected;
4194 pr " fprintf (stderr, \"%s: %s was \"%%s\", expected \"%s\"\\n\",\n"
4195 test_name field expected;
4196 pr " r->%s);\n" field;
4199 | CompareFieldsIntEq (field1, field2) ->
4200 pr " if (r->%s != r->%s) {\n" field1 field2;
4201 pr " fprintf (stderr, \"%s: %s (%%d) <> %s (%%d)\\n\",\n"
4202 test_name field1 field2;
4203 pr " (int) r->%s, (int) r->%s);\n" field1 field2;
4206 | CompareFieldsStrEq (field1, field2) ->
4207 pr " if (strcmp (r->%s, r->%s) != 0) {\n" field1 field2;
4208 pr " fprintf (stderr, \"%s: %s (\"%%s\") <> %s (\"%%s\")\\n\",\n"
4209 test_name field1 field2;
4210 pr " r->%s, r->%s);\n" field1 field2;
4215 List.iter (generate_test_command_call test_name) seq;
4216 generate_test_command_call ~test test_name last
4217 | TestLastFail seq ->
4218 pr " /* TestLastFail for %s (%d) */\n" name i;
4219 let seq, last = get_seq_last seq in
4220 List.iter (generate_test_command_call test_name) seq;
4221 generate_test_command_call test_name ~expect_error:true last
4223 (* Generate the code to run a command, leaving the result in 'r'.
4224 * If you expect to get an error then you should set expect_error:true.
4226 and generate_test_command_call ?(expect_error = false) ?test test_name cmd =
4228 | [] -> assert false
4230 (* Look up the command to find out what args/ret it has. *)
4233 let _, style, _, _, _, _, _ =
4234 List.find (fun (n, _, _, _, _, _, _) -> n = name) all_functions in
4237 failwithf "%s: in test, command %s was not found" test_name name in
4239 if List.length (snd style) <> List.length args then
4240 failwithf "%s: in test, wrong number of args given to %s"
4247 | OptString n, "NULL" -> ()
4249 | OptString n, arg ->
4250 pr " char %s[] = \"%s\";\n" n (c_quote arg);
4251 if String.length arg > 7 && String.sub arg 0 7 = "/dev/sd" then
4252 pr " %s[5] = devchar;\n" n
4255 | FileIn _, _ | FileOut _, _ -> ()
4256 | StringList n, arg ->
4257 let strs = string_split " " arg in
4260 pr " char %s_%d[] = \"%s\";\n" n i (c_quote str);
4261 if String.length str > 7 && String.sub str 0 7 = "/dev/sd" then
4262 pr " %s_%d[5] = devchar;\n" n i
4264 pr " char *%s[] = {\n" n;
4266 fun i _ -> pr " %s_%d,\n" n i
4270 ) (List.combine (snd style) args);
4273 match fst style with
4274 | RErr | RInt _ | RBool _ -> pr " int r;\n"; "-1"
4275 | RInt64 _ -> pr " int64_t r;\n"; "-1"
4276 | RConstString _ -> pr " const char *r;\n"; "NULL"
4277 | RString _ -> pr " char *r;\n"; "NULL"
4278 | RStringList _ | RHashtable _ ->
4283 pr " struct guestfs_int_bool *r;\n"; "NULL"
4285 pr " struct guestfs_lvm_pv_list *r;\n"; "NULL"
4287 pr " struct guestfs_lvm_vg_list *r;\n"; "NULL"
4289 pr " struct guestfs_lvm_lv_list *r;\n"; "NULL"
4291 pr " struct guestfs_stat *r;\n"; "NULL"
4293 pr " struct guestfs_statvfs *r;\n"; "NULL" in
4295 pr " suppress_error = %d;\n" (if expect_error then 1 else 0);
4296 pr " r = guestfs_%s (g" name;
4298 (* Generate the parameters. *)
4301 | OptString _, "NULL" -> pr ", NULL"
4305 | FileIn _, arg | FileOut _, arg ->
4306 pr ", \"%s\"" (c_quote arg)
4307 | StringList n, _ ->
4311 try int_of_string arg
4312 with Failure "int_of_string" ->
4313 failwithf "%s: expecting an int, but got '%s'" test_name arg in
4316 let b = bool_of_string arg in pr ", %d" (if b then 1 else 0)
4317 ) (List.combine (snd style) args);
4320 if not expect_error then
4321 pr " if (r == %s)\n" error_code
4323 pr " if (r != %s)\n" error_code;
4326 (* Insert the test code. *)
4332 (match fst style with
4333 | RErr | RInt _ | RInt64 _ | RBool _ | RConstString _ -> ()
4334 | RString _ -> pr " free (r);\n"
4335 | RStringList _ | RHashtable _ ->
4336 pr " for (i = 0; r[i] != NULL; ++i)\n";
4337 pr " free (r[i]);\n";
4340 pr " guestfs_free_int_bool (r);\n"
4342 pr " guestfs_free_lvm_pv_list (r);\n"
4344 pr " guestfs_free_lvm_vg_list (r);\n"
4346 pr " guestfs_free_lvm_lv_list (r);\n"
4347 | RStat _ | RStatVFS _ ->
4354 let str = replace_str str "\r" "\\r" in
4355 let str = replace_str str "\n" "\\n" in
4356 let str = replace_str str "\t" "\\t" in
4357 let str = replace_str str "\000" "\\0" in
4360 (* Generate a lot of different functions for guestfish. *)
4361 and generate_fish_cmds () =
4362 generate_header CStyle GPLv2;
4366 fun (_, _, _, flags, _, _, _) -> not (List.mem NotInFish flags)
4368 let all_functions_sorted =
4370 fun (_, _, _, flags, _, _, _) -> not (List.mem NotInFish flags)
4371 ) all_functions_sorted in
4373 pr "#include <stdio.h>\n";
4374 pr "#include <stdlib.h>\n";
4375 pr "#include <string.h>\n";
4376 pr "#include <inttypes.h>\n";
4378 pr "#include <guestfs.h>\n";
4379 pr "#include \"fish.h\"\n";
4382 (* list_commands function, which implements guestfish -h *)
4383 pr "void list_commands (void)\n";
4385 pr " printf (\" %%-16s %%s\\n\", \"Command\", \"Description\");\n";
4386 pr " list_builtin_commands ();\n";
4388 fun (name, _, _, flags, _, shortdesc, _) ->
4389 let name = replace_char name '_' '-' in
4390 pr " printf (\"%%-20s %%s\\n\", \"%s\", \"%s\");\n"
4392 ) all_functions_sorted;
4393 pr " printf (\" Use -h <cmd> / help <cmd> to show detailed help for a command.\\n\");\n";
4397 (* display_command function, which implements guestfish -h cmd *)
4398 pr "void display_command (const char *cmd)\n";
4401 fun (name, style, _, flags, _, shortdesc, longdesc) ->
4402 let name2 = replace_char name '_' '-' in
4404 try find_map (function FishAlias n -> Some n | _ -> None) flags
4405 with Not_found -> name in
4406 let longdesc = replace_str longdesc "C<guestfs_" "C<" in
4408 match snd style with
4412 name2 (String.concat "> <" (List.map name_of_argt args)) in
4415 if List.mem ProtocolLimitWarning flags then
4416 ("\n\n" ^ protocol_limit_warning)
4419 (* For DangerWillRobinson commands, we should probably have
4420 * guestfish prompt before allowing you to use them (especially
4421 * in interactive mode). XXX
4425 if List.mem DangerWillRobinson flags then
4426 ("\n\n" ^ danger_will_robinson)
4429 let describe_alias =
4430 if name <> alias then
4431 sprintf "\n\nYou can use '%s' as an alias for this command." alias
4435 pr "strcasecmp (cmd, \"%s\") == 0" name;
4436 if name <> name2 then
4437 pr " || strcasecmp (cmd, \"%s\") == 0" name2;
4438 if name <> alias then
4439 pr " || strcasecmp (cmd, \"%s\") == 0" alias;
4441 pr " pod2text (\"%s - %s\", %S);\n"
4443 (" " ^ synopsis ^ "\n\n" ^ longdesc ^ warnings ^ describe_alias);
4446 pr " display_builtin_command (cmd);\n";
4450 (* print_{pv,vg,lv}_list functions *)
4454 pr "static void print_%s (struct guestfs_lvm_%s *%s)\n" typ typ typ;
4461 pr " printf (\"%s: %%s\\n\", %s->%s);\n" name typ name
4463 pr " printf (\"%s: \");\n" name;
4464 pr " for (i = 0; i < 32; ++i)\n";
4465 pr " printf (\"%%c\", %s->%s[i]);\n" typ name;
4466 pr " printf (\"\\n\");\n"
4468 pr " printf (\"%s: %%\" PRIu64 \"\\n\", %s->%s);\n" name typ name
4470 pr " printf (\"%s: %%\" PRIi64 \"\\n\", %s->%s);\n" name typ name
4471 | name, `OptPercent ->
4472 pr " if (%s->%s >= 0) printf (\"%s: %%g %%%%\\n\", %s->%s);\n"
4473 typ name name typ name;
4474 pr " else printf (\"%s: \\n\");\n" name
4478 pr "static void print_%s_list (struct guestfs_lvm_%s_list *%ss)\n"
4483 pr " for (i = 0; i < %ss->len; ++i)\n" typ;
4484 pr " print_%s (&%ss->val[i]);\n" typ typ;
4487 ) ["pv", pv_cols; "vg", vg_cols; "lv", lv_cols];
4489 (* print_{stat,statvfs} functions *)
4493 pr "static void print_%s (struct guestfs_%s *%s)\n" typ typ typ;
4498 pr " printf (\"%s: %%\" PRIi64 \"\\n\", %s->%s);\n" name typ name
4502 ) ["stat", stat_cols; "statvfs", statvfs_cols];
4504 (* run_<action> actions *)
4506 fun (name, style, _, flags, _, _, _) ->
4507 pr "static int run_%s (const char *cmd, int argc, char *argv[])\n" name;
4509 (match fst style with
4512 | RBool _ -> pr " int r;\n"
4513 | RInt64 _ -> pr " int64_t r;\n"
4514 | RConstString _ -> pr " const char *r;\n"
4515 | RString _ -> pr " char *r;\n"
4516 | RStringList _ | RHashtable _ -> pr " char **r;\n"
4517 | RIntBool _ -> pr " struct guestfs_int_bool *r;\n"
4518 | RPVList _ -> pr " struct guestfs_lvm_pv_list *r;\n"
4519 | RVGList _ -> pr " struct guestfs_lvm_vg_list *r;\n"
4520 | RLVList _ -> pr " struct guestfs_lvm_lv_list *r;\n"
4521 | RStat _ -> pr " struct guestfs_stat *r;\n"
4522 | RStatVFS _ -> pr " struct guestfs_statvfs *r;\n"
4529 | FileOut n -> pr " const char *%s;\n" n
4530 | StringList n -> pr " char **%s;\n" n
4531 | Bool n -> pr " int %s;\n" n
4532 | Int n -> pr " int %s;\n" n
4535 (* Check and convert parameters. *)
4536 let argc_expected = List.length (snd style) in
4537 pr " if (argc != %d) {\n" argc_expected;
4538 pr " fprintf (stderr, \"%%s should have %d parameter(s)\\n\", cmd);\n"
4540 pr " fprintf (stderr, \"type 'help %%s' for help on %%s\\n\", cmd, cmd);\n";
4546 | String name -> pr " %s = argv[%d];\n" name i
4548 pr " %s = strcmp (argv[%d], \"\") != 0 ? argv[%d] : NULL;\n"
4551 pr " %s = strcmp (argv[%d], \"-\") != 0 ? argv[%d] : \"/dev/stdin\";\n"
4554 pr " %s = strcmp (argv[%d], \"-\") != 0 ? argv[%d] : \"/dev/stdout\";\n"
4556 | StringList name ->
4557 pr " %s = parse_string_list (argv[%d]);\n" name i
4559 pr " %s = is_true (argv[%d]) ? 1 : 0;\n" name i
4561 pr " %s = atoi (argv[%d]);\n" name i
4564 (* Call C API function. *)
4566 try find_map (function FishAction n -> Some n | _ -> None) flags
4567 with Not_found -> sprintf "guestfs_%s" name in
4569 generate_call_args ~handle:"g" (snd style);
4572 (* Check return value for errors and display command results. *)
4573 (match fst style with
4574 | RErr -> pr " return r;\n"
4576 pr " if (r == -1) return -1;\n";
4577 pr " printf (\"%%d\\n\", r);\n";
4580 pr " if (r == -1) return -1;\n";
4581 pr " printf (\"%%\" PRIi64 \"\\n\", r);\n";
4584 pr " if (r == -1) return -1;\n";
4585 pr " if (r) printf (\"true\\n\"); else printf (\"false\\n\");\n";
4588 pr " if (r == NULL) return -1;\n";
4589 pr " printf (\"%%s\\n\", r);\n";
4592 pr " if (r == NULL) return -1;\n";
4593 pr " printf (\"%%s\\n\", r);\n";
4597 pr " if (r == NULL) return -1;\n";
4598 pr " print_strings (r);\n";
4599 pr " free_strings (r);\n";
4602 pr " if (r == NULL) return -1;\n";
4603 pr " printf (\"%%d, %%s\\n\", r->i,\n";
4604 pr " r->b ? \"true\" : \"false\");\n";
4605 pr " guestfs_free_int_bool (r);\n";
4608 pr " if (r == NULL) return -1;\n";
4609 pr " print_pv_list (r);\n";
4610 pr " guestfs_free_lvm_pv_list (r);\n";
4613 pr " if (r == NULL) return -1;\n";
4614 pr " print_vg_list (r);\n";
4615 pr " guestfs_free_lvm_vg_list (r);\n";
4618 pr " if (r == NULL) return -1;\n";
4619 pr " print_lv_list (r);\n";
4620 pr " guestfs_free_lvm_lv_list (r);\n";
4623 pr " if (r == NULL) return -1;\n";
4624 pr " print_stat (r);\n";
4628 pr " if (r == NULL) return -1;\n";
4629 pr " print_statvfs (r);\n";
4633 pr " if (r == NULL) return -1;\n";
4634 pr " print_table (r);\n";
4635 pr " free_strings (r);\n";
4642 (* run_action function *)
4643 pr "int run_action (const char *cmd, int argc, char *argv[])\n";
4646 fun (name, _, _, flags, _, _, _) ->
4647 let name2 = replace_char name '_' '-' in
4649 try find_map (function FishAlias n -> Some n | _ -> None) flags
4650 with Not_found -> name in
4652 pr "strcasecmp (cmd, \"%s\") == 0" name;
4653 if name <> name2 then
4654 pr " || strcasecmp (cmd, \"%s\") == 0" name2;
4655 if name <> alias then
4656 pr " || strcasecmp (cmd, \"%s\") == 0" alias;
4658 pr " return run_%s (cmd, argc, argv);\n" name;
4662 pr " fprintf (stderr, \"%%s: unknown command\\n\", cmd);\n";
4669 (* Readline completion for guestfish. *)
4670 and generate_fish_completion () =
4671 generate_header CStyle GPLv2;
4675 fun (_, _, _, flags, _, _, _) -> not (List.mem NotInFish flags)
4685 #ifdef HAVE_LIBREADLINE
4686 #include <readline/readline.h>
4691 #ifdef HAVE_LIBREADLINE
4693 static const char *const commands[] = {
4694 BUILTIN_COMMANDS_FOR_COMPLETION,
4697 (* Get the commands, including the aliases. They don't need to be
4698 * sorted - the generator() function just does a dumb linear search.
4702 fun (name, _, _, flags, _, _, _) ->
4703 let name2 = replace_char name '_' '-' in
4705 try find_map (function FishAlias n -> Some n | _ -> None) flags
4706 with Not_found -> name in
4708 if name <> alias then [name2; alias] else [name2]
4710 let commands = List.flatten commands in
4712 List.iter (pr " \"%s\",\n") commands;
4718 generator (const char *text, int state)
4720 static int index, len;
4725 len = strlen (text);
4728 while ((name = commands[index]) != NULL) {
4730 if (strncasecmp (name, text, len) == 0)
4731 return strdup (name);
4737 #endif /* HAVE_LIBREADLINE */
4739 char **do_completion (const char *text, int start, int end)
4741 char **matches = NULL;
4743 #ifdef HAVE_LIBREADLINE
4745 matches = rl_completion_matches (text, generator);
4752 (* Generate the POD documentation for guestfish. *)
4753 and generate_fish_actions_pod () =
4754 let all_functions_sorted =
4756 fun (_, _, _, flags, _, _, _) ->
4757 not (List.mem NotInFish flags || List.mem NotInDocs flags)
4758 ) all_functions_sorted in
4760 let rex = Str.regexp "C<guestfs_\\([^>]+\\)>" in
4763 fun (name, style, _, flags, _, _, longdesc) ->
4765 Str.global_substitute rex (
4768 try Str.matched_group 1 s
4770 failwithf "error substituting C<guestfs_...> in longdesc of function %s" name in
4771 "C<" ^ replace_char sub '_' '-' ^ ">"
4773 let name = replace_char name '_' '-' in
4775 try find_map (function FishAlias n -> Some n | _ -> None) flags
4776 with Not_found -> name in
4778 pr "=head2 %s" name;
4779 if name <> alias then
4786 | String n -> pr " %s" n
4787 | OptString n -> pr " %s" n
4788 | StringList n -> pr " '%s ...'" n
4789 | Bool _ -> pr " true|false"
4790 | Int n -> pr " %s" n
4791 | FileIn n | FileOut n -> pr " (%s|-)" n
4795 pr "%s\n\n" longdesc;
4797 if List.exists (function FileIn _ | FileOut _ -> true
4798 | _ -> false) (snd style) then
4799 pr "Use C<-> instead of a filename to read/write from stdin/stdout.\n\n";
4801 if List.mem ProtocolLimitWarning flags then
4802 pr "%s\n\n" protocol_limit_warning;
4804 if List.mem DangerWillRobinson flags then
4805 pr "%s\n\n" danger_will_robinson
4806 ) all_functions_sorted
4808 (* Generate a C function prototype. *)
4809 and generate_prototype ?(extern = true) ?(static = false) ?(semicolon = true)
4810 ?(single_line = false) ?(newline = false) ?(in_daemon = false)
4812 ?handle name style =
4813 if extern then pr "extern ";
4814 if static then pr "static ";
4815 (match fst style with
4817 | RInt _ -> pr "int "
4818 | RInt64 _ -> pr "int64_t "
4819 | RBool _ -> pr "int "
4820 | RConstString _ -> pr "const char *"
4821 | RString _ -> pr "char *"
4822 | RStringList _ | RHashtable _ -> pr "char **"
4824 if not in_daemon then pr "struct guestfs_int_bool *"
4825 else pr "guestfs_%s_ret *" name
4827 if not in_daemon then pr "struct guestfs_lvm_pv_list *"
4828 else pr "guestfs_lvm_int_pv_list *"
4830 if not in_daemon then pr "struct guestfs_lvm_vg_list *"
4831 else pr "guestfs_lvm_int_vg_list *"
4833 if not in_daemon then pr "struct guestfs_lvm_lv_list *"
4834 else pr "guestfs_lvm_int_lv_list *"
4836 if not in_daemon then pr "struct guestfs_stat *"
4837 else pr "guestfs_int_stat *"
4839 if not in_daemon then pr "struct guestfs_statvfs *"
4840 else pr "guestfs_int_statvfs *"
4842 pr "%s%s (" prefix name;
4843 if handle = None && List.length (snd style) = 0 then
4846 let comma = ref false in
4849 | Some handle -> pr "guestfs_h *%s" handle; comma := true
4853 if single_line then pr ", " else pr ",\n\t\t"
4860 | OptString n -> next (); pr "const char *%s" n
4861 | StringList n -> next (); pr "char * const* const %s" n
4862 | Bool n -> next (); pr "int %s" n
4863 | Int n -> next (); pr "int %s" n
4866 if not in_daemon then (next (); pr "const char *%s" n)
4870 if semicolon then pr ";";
4871 if newline then pr "\n"
4873 (* Generate C call arguments, eg "(handle, foo, bar)" *)
4874 and generate_call_args ?handle args =
4876 let comma = ref false in
4879 | Some handle -> pr "%s" handle; comma := true
4883 if !comma then pr ", ";
4885 pr "%s" (name_of_argt arg)
4889 (* Generate the OCaml bindings interface. *)
4890 and generate_ocaml_mli () =
4891 generate_header OCamlStyle LGPLv2;
4894 (** For API documentation you should refer to the C API
4895 in the guestfs(3) manual page. The OCaml API uses almost
4896 exactly the same calls. *)
4899 (** A [guestfs_h] handle. *)
4901 exception Error of string
4902 (** This exception is raised when there is an error. *)
4904 val create : unit -> t
4906 val close : t -> unit
4907 (** Handles are closed by the garbage collector when they become
4908 unreferenced, but callers can also call this in order to
4909 provide predictable cleanup. *)
4912 generate_ocaml_lvm_structure_decls ();
4914 generate_ocaml_stat_structure_decls ();
4918 fun (name, style, _, _, _, shortdesc, _) ->
4919 generate_ocaml_prototype name style;
4920 pr "(** %s *)\n" shortdesc;
4924 (* Generate the OCaml bindings implementation. *)
4925 and generate_ocaml_ml () =
4926 generate_header OCamlStyle LGPLv2;
4930 exception Error of string
4931 external create : unit -> t = \"ocaml_guestfs_create\"
4932 external close : t -> unit = \"ocaml_guestfs_close\"
4935 Callback.register_exception \"ocaml_guestfs_error\" (Error \"\")
4939 generate_ocaml_lvm_structure_decls ();
4941 generate_ocaml_stat_structure_decls ();
4945 fun (name, style, _, _, _, shortdesc, _) ->
4946 generate_ocaml_prototype ~is_external:true name style;
4949 (* Generate the OCaml bindings C implementation. *)
4950 and generate_ocaml_c () =
4951 generate_header CStyle LGPLv2;
4958 #include <caml/config.h>
4959 #include <caml/alloc.h>
4960 #include <caml/callback.h>
4961 #include <caml/fail.h>
4962 #include <caml/memory.h>
4963 #include <caml/mlvalues.h>
4964 #include <caml/signals.h>
4966 #include <guestfs.h>
4968 #include \"guestfs_c.h\"
4970 /* Copy a hashtable of string pairs into an assoc-list. We return
4971 * the list in reverse order, but hashtables aren't supposed to be
4974 static CAMLprim value
4975 copy_table (char * const * argv)
4978 CAMLlocal5 (rv, pairv, kv, vv, cons);
4982 for (i = 0; argv[i] != NULL; i += 2) {
4983 kv = caml_copy_string (argv[i]);
4984 vv = caml_copy_string (argv[i+1]);
4985 pairv = caml_alloc (2, 0);
4986 Store_field (pairv, 0, kv);
4987 Store_field (pairv, 1, vv);
4988 cons = caml_alloc (2, 0);
4989 Store_field (cons, 1, rv);
4991 Store_field (cons, 0, pairv);
4999 (* LVM struct copy functions. *)
5002 let has_optpercent_col =
5003 List.exists (function (_, `OptPercent) -> true | _ -> false) cols in
5005 pr "static CAMLprim value\n";
5006 pr "copy_lvm_%s (const struct guestfs_lvm_%s *%s)\n" typ typ typ;
5008 pr " CAMLparam0 ();\n";
5009 if has_optpercent_col then
5010 pr " CAMLlocal3 (rv, v, v2);\n"
5012 pr " CAMLlocal2 (rv, v);\n";
5014 pr " rv = caml_alloc (%d, 0);\n" (List.length cols);
5019 pr " v = caml_copy_string (%s->%s);\n" typ name
5021 pr " v = caml_alloc_string (32);\n";
5022 pr " memcpy (String_val (v), %s->%s, 32);\n" typ name
5025 pr " v = caml_copy_int64 (%s->%s);\n" typ name
5026 | name, `OptPercent ->
5027 pr " if (%s->%s >= 0) { /* Some %s */\n" typ name name;
5028 pr " v2 = caml_copy_double (%s->%s);\n" typ name;
5029 pr " v = caml_alloc (1, 0);\n";
5030 pr " Store_field (v, 0, v2);\n";
5031 pr " } else /* None */\n";
5032 pr " v = Val_int (0);\n";
5034 pr " Store_field (rv, %d, v);\n" i
5036 pr " CAMLreturn (rv);\n";
5040 pr "static CAMLprim value\n";
5041 pr "copy_lvm_%s_list (const struct guestfs_lvm_%s_list *%ss)\n"
5044 pr " CAMLparam0 ();\n";
5045 pr " CAMLlocal2 (rv, v);\n";
5048 pr " if (%ss->len == 0)\n" typ;
5049 pr " CAMLreturn (Atom (0));\n";
5051 pr " rv = caml_alloc (%ss->len, 0);\n" typ;
5052 pr " for (i = 0; i < %ss->len; ++i) {\n" typ;
5053 pr " v = copy_lvm_%s (&%ss->val[i]);\n" typ typ;
5054 pr " caml_modify (&Field (rv, i), v);\n";
5056 pr " CAMLreturn (rv);\n";
5060 ) ["pv", pv_cols; "vg", vg_cols; "lv", lv_cols];
5062 (* Stat copy functions. *)
5065 pr "static CAMLprim value\n";
5066 pr "copy_%s (const struct guestfs_%s *%s)\n" typ typ typ;
5068 pr " CAMLparam0 ();\n";
5069 pr " CAMLlocal2 (rv, v);\n";
5071 pr " rv = caml_alloc (%d, 0);\n" (List.length cols);
5076 pr " v = caml_copy_int64 (%s->%s);\n" typ name
5078 pr " Store_field (rv, %d, v);\n" i
5080 pr " CAMLreturn (rv);\n";
5083 ) ["stat", stat_cols; "statvfs", statvfs_cols];
5087 fun (name, style, _, _, _, _, _) ->
5089 "gv" :: List.map (fun arg -> name_of_argt arg ^ "v") (snd style) in
5091 pr "CAMLprim value\n";
5092 pr "ocaml_guestfs_%s (value %s" name (List.hd params);
5093 List.iter (pr ", value %s") (List.tl params);
5098 | [p1; p2; p3; p4; p5] ->
5099 pr " CAMLparam5 (%s);\n" (String.concat ", " params)
5100 | p1 :: p2 :: p3 :: p4 :: p5 :: rest ->
5101 pr " CAMLparam5 (%s);\n" (String.concat ", " [p1; p2; p3; p4; p5]);
5102 pr " CAMLxparam%d (%s);\n"
5103 (List.length rest) (String.concat ", " rest)
5105 pr " CAMLparam%d (%s);\n" (List.length ps) (String.concat ", " ps)
5107 pr " CAMLlocal1 (rv);\n";
5110 pr " guestfs_h *g = Guestfs_val (gv);\n";
5111 pr " if (g == NULL)\n";
5112 pr " caml_failwith (\"%s: used handle after closing it\");\n" name;
5120 pr " const char *%s = String_val (%sv);\n" n n
5122 pr " const char *%s =\n" n;
5123 pr " %sv != Val_int (0) ? String_val (Field (%sv, 0)) : NULL;\n"
5126 pr " char **%s = ocaml_guestfs_strings_val (g, %sv);\n" n n
5128 pr " int %s = Bool_val (%sv);\n" n n
5130 pr " int %s = Int_val (%sv);\n" n n
5133 match fst style with
5134 | RErr -> pr " int r;\n"; "-1"
5135 | RInt _ -> pr " int r;\n"; "-1"
5136 | RInt64 _ -> pr " int64_t r;\n"; "-1"
5137 | RBool _ -> pr " int r;\n"; "-1"
5138 | RConstString _ -> pr " const char *r;\n"; "NULL"
5139 | RString _ -> pr " char *r;\n"; "NULL"
5145 pr " struct guestfs_int_bool *r;\n"; "NULL"
5147 pr " struct guestfs_lvm_pv_list *r;\n"; "NULL"
5149 pr " struct guestfs_lvm_vg_list *r;\n"; "NULL"
5151 pr " struct guestfs_lvm_lv_list *r;\n"; "NULL"
5153 pr " struct guestfs_stat *r;\n"; "NULL"
5155 pr " struct guestfs_statvfs *r;\n"; "NULL"
5162 pr " caml_enter_blocking_section ();\n";
5163 pr " r = guestfs_%s " name;
5164 generate_call_args ~handle:"g" (snd style);
5166 pr " caml_leave_blocking_section ();\n";
5171 pr " ocaml_guestfs_free_strings (%s);\n" n;
5172 | String _ | OptString _ | Bool _ | Int _ | FileIn _ | FileOut _ -> ()
5175 pr " if (r == %s)\n" error_code;
5176 pr " ocaml_guestfs_raise_error (g, \"%s\");\n" name;
5179 (match fst style with
5180 | RErr -> pr " rv = Val_unit;\n"
5181 | RInt _ -> pr " rv = Val_int (r);\n"
5183 pr " rv = caml_copy_int64 (r);\n"
5184 | RBool _ -> pr " rv = Val_bool (r);\n"
5185 | RConstString _ -> pr " rv = caml_copy_string (r);\n"
5187 pr " rv = caml_copy_string (r);\n";
5190 pr " rv = caml_copy_string_array ((const char **) r);\n";
5191 pr " for (i = 0; r[i] != NULL; ++i) free (r[i]);\n";
5194 pr " rv = caml_alloc (2, 0);\n";
5195 pr " Store_field (rv, 0, Val_int (r->i));\n";
5196 pr " Store_field (rv, 1, Val_bool (r->b));\n";
5197 pr " guestfs_free_int_bool (r);\n";
5199 pr " rv = copy_lvm_pv_list (r);\n";
5200 pr " guestfs_free_lvm_pv_list (r);\n";
5202 pr " rv = copy_lvm_vg_list (r);\n";
5203 pr " guestfs_free_lvm_vg_list (r);\n";
5205 pr " rv = copy_lvm_lv_list (r);\n";
5206 pr " guestfs_free_lvm_lv_list (r);\n";
5208 pr " rv = copy_stat (r);\n";
5211 pr " rv = copy_statvfs (r);\n";
5214 pr " rv = copy_table (r);\n";
5215 pr " for (i = 0; r[i] != NULL; ++i) free (r[i]);\n";
5219 pr " CAMLreturn (rv);\n";
5223 if List.length params > 5 then (
5224 pr "CAMLprim value\n";
5225 pr "ocaml_guestfs_%s_byte (value *argv, int argn)\n" name;
5227 pr " return ocaml_guestfs_%s (argv[0]" name;
5228 iteri (fun i _ -> pr ", argv[%d]" i) (List.tl params);
5235 and generate_ocaml_lvm_structure_decls () =
5238 pr "type lvm_%s = {\n" typ;
5241 | name, `String -> pr " %s : string;\n" name
5242 | name, `UUID -> pr " %s : string;\n" name
5243 | name, `Bytes -> pr " %s : int64;\n" name
5244 | name, `Int -> pr " %s : int64;\n" name
5245 | name, `OptPercent -> pr " %s : float option;\n" name
5249 ) ["pv", pv_cols; "vg", vg_cols; "lv", lv_cols]
5251 and generate_ocaml_stat_structure_decls () =
5254 pr "type %s = {\n" typ;
5257 | name, `Int -> pr " %s : int64;\n" name
5261 ) ["stat", stat_cols; "statvfs", statvfs_cols]
5263 and generate_ocaml_prototype ?(is_external = false) name style =
5264 if is_external then pr "external " else pr "val ";
5265 pr "%s : t -> " name;
5268 | String _ | FileIn _ | FileOut _ -> pr "string -> "
5269 | OptString _ -> pr "string option -> "
5270 | StringList _ -> pr "string array -> "
5271 | Bool _ -> pr "bool -> "
5272 | Int _ -> pr "int -> "
5274 (match fst style with
5275 | RErr -> pr "unit" (* all errors are turned into exceptions *)
5276 | RInt _ -> pr "int"
5277 | RInt64 _ -> pr "int64"
5278 | RBool _ -> pr "bool"
5279 | RConstString _ -> pr "string"
5280 | RString _ -> pr "string"
5281 | RStringList _ -> pr "string array"
5282 | RIntBool _ -> pr "int * bool"
5283 | RPVList _ -> pr "lvm_pv array"
5284 | RVGList _ -> pr "lvm_vg array"
5285 | RLVList _ -> pr "lvm_lv array"
5286 | RStat _ -> pr "stat"
5287 | RStatVFS _ -> pr "statvfs"
5288 | RHashtable _ -> pr "(string * string) list"
5290 if is_external then (
5292 if List.length (snd style) + 1 > 5 then
5293 pr "\"ocaml_guestfs_%s_byte\" " name;
5294 pr "\"ocaml_guestfs_%s\"" name
5298 (* Generate Perl xs code, a sort of crazy variation of C with macros. *)
5299 and generate_perl_xs () =
5300 generate_header CStyle LGPLv2;
5303 #include \"EXTERN.h\"
5307 #include <guestfs.h>
5310 #define PRId64 \"lld\"
5314 my_newSVll(long long val) {
5315 #ifdef USE_64_BIT_ALL
5316 return newSViv(val);
5320 len = snprintf(buf, 100, \"%%\" PRId64, val);
5321 return newSVpv(buf, len);
5326 #define PRIu64 \"llu\"
5330 my_newSVull(unsigned long long val) {
5331 #ifdef USE_64_BIT_ALL
5332 return newSVuv(val);
5336 len = snprintf(buf, 100, \"%%\" PRIu64, val);
5337 return newSVpv(buf, len);
5341 /* http://www.perlmonks.org/?node_id=680842 */
5343 XS_unpack_charPtrPtr (SV *arg) {
5348 if (!arg || !SvOK (arg) || !SvROK (arg) || SvTYPE (SvRV (arg)) != SVt_PVAV)
5349 croak (\"array reference expected\");
5351 av = (AV *)SvRV (arg);
5352 ret = malloc ((av_len (av) + 1 + 1) * sizeof (char *));
5354 croak (\"malloc failed\");
5356 for (i = 0; i <= av_len (av); i++) {
5357 SV **elem = av_fetch (av, i, 0);
5359 if (!elem || !*elem)
5360 croak (\"missing element in list\");
5362 ret[i] = SvPV_nolen (*elem);
5370 MODULE = Sys::Guestfs PACKAGE = Sys::Guestfs
5377 RETVAL = guestfs_create ();
5379 croak (\"could not create guestfs handle\");
5380 guestfs_set_error_handler (RETVAL, NULL, NULL);
5393 fun (name, style, _, _, _, _, _) ->
5394 (match fst style with
5395 | RErr -> pr "void\n"
5396 | RInt _ -> pr "SV *\n"
5397 | RInt64 _ -> pr "SV *\n"
5398 | RBool _ -> pr "SV *\n"
5399 | RConstString _ -> pr "SV *\n"
5400 | RString _ -> pr "SV *\n"
5403 | RPVList _ | RVGList _ | RLVList _
5404 | RStat _ | RStatVFS _
5406 pr "void\n" (* all lists returned implictly on the stack *)
5408 (* Call and arguments. *)
5410 generate_call_args ~handle:"g" (snd style);
5412 pr " guestfs_h *g;\n";
5416 | String n | FileIn n | FileOut n -> pr " char *%s;\n" n
5418 (* http://www.perlmonks.org/?node_id=554277
5419 * Note that the implicit handle argument means we have
5420 * to add 1 to the ST(x) operator.
5422 pr " char *%s = SvOK(ST(%d)) ? SvPV_nolen(ST(%d)) : NULL;\n" n (i+1) (i+1)
5423 | StringList n -> pr " char **%s;\n" n
5424 | Bool n -> pr " int %s;\n" n
5425 | Int n -> pr " int %s;\n" n
5428 let do_cleanups () =
5431 | String _ | OptString _ | Bool _ | Int _
5432 | FileIn _ | FileOut _ -> ()
5433 | StringList n -> pr " free (%s);\n" n
5438 (match fst style with
5443 pr " r = guestfs_%s " name;
5444 generate_call_args ~handle:"g" (snd style);
5447 pr " if (r == -1)\n";
5448 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
5454 pr " %s = guestfs_%s " n name;
5455 generate_call_args ~handle:"g" (snd style);
5458 pr " if (%s == -1)\n" n;
5459 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
5460 pr " RETVAL = newSViv (%s);\n" n;
5465 pr " int64_t %s;\n" n;
5467 pr " %s = guestfs_%s " n name;
5468 generate_call_args ~handle:"g" (snd style);
5471 pr " if (%s == -1)\n" n;
5472 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
5473 pr " RETVAL = my_newSVll (%s);\n" n;
5478 pr " const char *%s;\n" n;
5480 pr " %s = guestfs_%s " n name;
5481 generate_call_args ~handle:"g" (snd style);
5484 pr " if (%s == NULL)\n" n;
5485 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
5486 pr " RETVAL = newSVpv (%s, 0);\n" n;
5491 pr " char *%s;\n" n;
5493 pr " %s = guestfs_%s " n name;
5494 generate_call_args ~handle:"g" (snd style);
5497 pr " if (%s == NULL)\n" n;
5498 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
5499 pr " RETVAL = newSVpv (%s, 0);\n" n;
5500 pr " free (%s);\n" n;
5503 | RStringList n | RHashtable n ->
5505 pr " char **%s;\n" n;
5508 pr " %s = guestfs_%s " n name;
5509 generate_call_args ~handle:"g" (snd style);
5512 pr " if (%s == NULL)\n" n;
5513 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
5514 pr " for (n = 0; %s[n] != NULL; ++n) /**/;\n" n;
5515 pr " EXTEND (SP, n);\n";
5516 pr " for (i = 0; i < n; ++i) {\n";
5517 pr " PUSHs (sv_2mortal (newSVpv (%s[i], 0)));\n" n;
5518 pr " free (%s[i]);\n" n;
5520 pr " free (%s);\n" n;
5523 pr " struct guestfs_int_bool *r;\n";
5525 pr " r = guestfs_%s " name;
5526 generate_call_args ~handle:"g" (snd style);
5529 pr " if (r == NULL)\n";
5530 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
5531 pr " EXTEND (SP, 2);\n";
5532 pr " PUSHs (sv_2mortal (newSViv (r->i)));\n";
5533 pr " PUSHs (sv_2mortal (newSViv (r->b)));\n";
5534 pr " guestfs_free_int_bool (r);\n";
5536 generate_perl_lvm_code "pv" pv_cols name style n do_cleanups
5538 generate_perl_lvm_code "vg" vg_cols name style n do_cleanups
5540 generate_perl_lvm_code "lv" lv_cols name style n do_cleanups
5542 generate_perl_stat_code "stat" stat_cols name style n do_cleanups
5544 generate_perl_stat_code
5545 "statvfs" statvfs_cols name style n do_cleanups
5551 and generate_perl_lvm_code typ cols name style n do_cleanups =
5553 pr " struct guestfs_lvm_%s_list *%s;\n" typ n;
5557 pr " %s = guestfs_%s " n name;
5558 generate_call_args ~handle:"g" (snd style);
5561 pr " if (%s == NULL)\n" n;
5562 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
5563 pr " EXTEND (SP, %s->len);\n" n;
5564 pr " for (i = 0; i < %s->len; ++i) {\n" n;
5565 pr " hv = newHV ();\n";
5569 pr " (void) hv_store (hv, \"%s\", %d, newSVpv (%s->val[i].%s, 0), 0);\n"
5570 name (String.length name) n name
5572 pr " (void) hv_store (hv, \"%s\", %d, newSVpv (%s->val[i].%s, 32), 0);\n"
5573 name (String.length name) n name
5575 pr " (void) hv_store (hv, \"%s\", %d, my_newSVull (%s->val[i].%s), 0);\n"
5576 name (String.length name) n name
5578 pr " (void) hv_store (hv, \"%s\", %d, my_newSVll (%s->val[i].%s), 0);\n"
5579 name (String.length name) n name
5580 | name, `OptPercent ->
5581 pr " (void) hv_store (hv, \"%s\", %d, newSVnv (%s->val[i].%s), 0);\n"
5582 name (String.length name) n name
5584 pr " PUSHs (sv_2mortal ((SV *) hv));\n";
5586 pr " guestfs_free_lvm_%s_list (%s);\n" typ n
5588 and generate_perl_stat_code typ cols name style n do_cleanups =
5590 pr " struct guestfs_%s *%s;\n" typ n;
5592 pr " %s = guestfs_%s " n name;
5593 generate_call_args ~handle:"g" (snd style);
5596 pr " if (%s == NULL)\n" n;
5597 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
5598 pr " EXTEND (SP, %d);\n" (List.length cols);
5602 pr " PUSHs (sv_2mortal (my_newSVll (%s->%s)));\n" n name
5604 pr " free (%s);\n" n
5606 (* Generate Sys/Guestfs.pm. *)
5607 and generate_perl_pm () =
5608 generate_header HashStyle LGPLv2;
5615 Sys::Guestfs - Perl bindings for libguestfs
5621 my $h = Sys::Guestfs->new ();
5622 $h->add_drive ('guest.img');
5625 $h->mount ('/dev/sda1', '/');
5626 $h->touch ('/hello');
5631 The C<Sys::Guestfs> module provides a Perl XS binding to the
5632 libguestfs API for examining and modifying virtual machine
5635 Amongst the things this is good for: making batch configuration
5636 changes to guests, getting disk used/free statistics (see also:
5637 virt-df), migrating between virtualization systems (see also:
5638 virt-p2v), performing partial backups, performing partial guest
5639 clones, cloning guests and changing registry/UUID/hostname info, and
5642 Libguestfs uses Linux kernel and qemu code, and can access any type of
5643 guest filesystem that Linux and qemu can, including but not limited
5644 to: ext2/3/4, btrfs, FAT and NTFS, LVM, many different disk partition
5645 schemes, qcow, qcow2, vmdk.
5647 Libguestfs provides ways to enumerate guest storage (eg. partitions,
5648 LVs, what filesystem is in each LV, etc.). It can also run commands
5649 in the context of the guest. Also you can access filesystems over FTP.
5653 All errors turn into calls to C<croak> (see L<Carp(3)>).
5661 package Sys::Guestfs;
5667 XSLoader::load ('Sys::Guestfs');
5669 =item $h = Sys::Guestfs->new ();
5671 Create a new guestfs handle.
5677 my $class = ref ($proto) || $proto;
5679 my $self = Sys::Guestfs::_create ();
5680 bless $self, $class;
5686 (* Actions. We only need to print documentation for these as
5687 * they are pulled in from the XS code automatically.
5690 fun (name, style, _, flags, _, _, longdesc) ->
5691 if not (List.mem NotInDocs flags) then (
5692 let longdesc = replace_str longdesc "C<guestfs_" "C<$h-E<gt>" in
5694 generate_perl_prototype name style;
5696 pr "%s\n\n" longdesc;
5697 if List.mem ProtocolLimitWarning flags then
5698 pr "%s\n\n" protocol_limit_warning;
5699 if List.mem DangerWillRobinson flags then
5700 pr "%s\n\n" danger_will_robinson
5702 ) all_functions_sorted;
5714 Copyright (C) 2009 Red Hat Inc.
5718 Please see the file COPYING.LIB for the full license.
5722 L<guestfs(3)>, L<guestfish(1)>.
5727 and generate_perl_prototype name style =
5728 (match fst style with
5734 | RString n -> pr "$%s = " n
5735 | RIntBool (n, m) -> pr "($%s, $%s) = " n m
5739 | RLVList n -> pr "@%s = " n
5742 | RHashtable n -> pr "%%%s = " n
5745 let comma = ref false in
5748 if !comma then pr ", ";
5751 | String n | OptString n | Bool n | Int n | FileIn n | FileOut n ->
5758 (* Generate Python C module. *)
5759 and generate_python_c () =
5760 generate_header CStyle LGPLv2;
5769 #include \"guestfs.h\"
5777 get_handle (PyObject *obj)
5780 assert (obj != Py_None);
5781 return ((Pyguestfs_Object *) obj)->g;
5785 put_handle (guestfs_h *g)
5789 PyCObject_FromVoidPtrAndDesc ((void *) g, (char *) \"guestfs_h\", NULL);
5792 /* This list should be freed (but not the strings) after use. */
5793 static const char **
5794 get_string_list (PyObject *obj)
5801 if (!PyList_Check (obj)) {
5802 PyErr_SetString (PyExc_RuntimeError, \"expecting a list parameter\");
5806 len = PyList_Size (obj);
5807 r = malloc (sizeof (char *) * (len+1));
5809 PyErr_SetString (PyExc_RuntimeError, \"get_string_list: out of memory\");
5813 for (i = 0; i < len; ++i)
5814 r[i] = PyString_AsString (PyList_GetItem (obj, i));
5821 put_string_list (char * const * const argv)
5826 for (argc = 0; argv[argc] != NULL; ++argc)
5829 list = PyList_New (argc);
5830 for (i = 0; i < argc; ++i)
5831 PyList_SetItem (list, i, PyString_FromString (argv[i]));
5837 put_table (char * const * const argv)
5839 PyObject *list, *item;
5842 for (argc = 0; argv[argc] != NULL; ++argc)
5845 list = PyList_New (argc >> 1);
5846 for (i = 0; i < argc; i += 2) {
5847 item = PyTuple_New (2);
5848 PyTuple_SetItem (item, 0, PyString_FromString (argv[i]));
5849 PyTuple_SetItem (item, 1, PyString_FromString (argv[i+1]));
5850 PyList_SetItem (list, i >> 1, item);
5857 free_strings (char **argv)
5861 for (argc = 0; argv[argc] != NULL; ++argc)
5867 py_guestfs_create (PyObject *self, PyObject *args)
5871 g = guestfs_create ();
5873 PyErr_SetString (PyExc_RuntimeError,
5874 \"guestfs.create: failed to allocate handle\");
5877 guestfs_set_error_handler (g, NULL, NULL);
5878 return put_handle (g);
5882 py_guestfs_close (PyObject *self, PyObject *args)
5887 if (!PyArg_ParseTuple (args, (char *) \"O:guestfs_close\", &py_g))
5889 g = get_handle (py_g);
5893 Py_INCREF (Py_None);
5899 (* LVM structures, turned into Python dictionaries. *)
5902 pr "static PyObject *\n";
5903 pr "put_lvm_%s (struct guestfs_lvm_%s *%s)\n" typ typ typ;
5905 pr " PyObject *dict;\n";
5907 pr " dict = PyDict_New ();\n";
5911 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
5912 pr " PyString_FromString (%s->%s));\n"
5915 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
5916 pr " PyString_FromStringAndSize (%s->%s, 32));\n"
5919 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
5920 pr " PyLong_FromUnsignedLongLong (%s->%s));\n"
5923 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
5924 pr " PyLong_FromLongLong (%s->%s));\n"
5926 | name, `OptPercent ->
5927 pr " if (%s->%s >= 0)\n" typ name;
5928 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
5929 pr " PyFloat_FromDouble ((double) %s->%s));\n"
5932 pr " Py_INCREF (Py_None);\n";
5933 pr " PyDict_SetItemString (dict, \"%s\", Py_None);" name;
5936 pr " return dict;\n";
5940 pr "static PyObject *\n";
5941 pr "put_lvm_%s_list (struct guestfs_lvm_%s_list *%ss)\n" typ typ typ;
5943 pr " PyObject *list;\n";
5946 pr " list = PyList_New (%ss->len);\n" typ;
5947 pr " for (i = 0; i < %ss->len; ++i)\n" typ;
5948 pr " PyList_SetItem (list, i, put_lvm_%s (&%ss->val[i]));\n" typ typ;
5949 pr " return list;\n";
5952 ) ["pv", pv_cols; "vg", vg_cols; "lv", lv_cols];
5954 (* Stat structures, turned into Python dictionaries. *)
5957 pr "static PyObject *\n";
5958 pr "put_%s (struct guestfs_%s *%s)\n" typ typ typ;
5960 pr " PyObject *dict;\n";
5962 pr " dict = PyDict_New ();\n";
5966 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
5967 pr " PyLong_FromLongLong (%s->%s));\n"
5970 pr " return dict;\n";
5973 ) ["stat", stat_cols; "statvfs", statvfs_cols];
5975 (* Python wrapper functions. *)
5977 fun (name, style, _, _, _, _, _) ->
5978 pr "static PyObject *\n";
5979 pr "py_guestfs_%s (PyObject *self, PyObject *args)\n" name;
5982 pr " PyObject *py_g;\n";
5983 pr " guestfs_h *g;\n";
5984 pr " PyObject *py_r;\n";
5987 match fst style with
5988 | RErr | RInt _ | RBool _ -> pr " int r;\n"; "-1"
5989 | RInt64 _ -> pr " int64_t r;\n"; "-1"
5990 | RConstString _ -> pr " const char *r;\n"; "NULL"
5991 | RString _ -> pr " char *r;\n"; "NULL"
5992 | RStringList _ | RHashtable _ -> pr " char **r;\n"; "NULL"
5993 | RIntBool _ -> pr " struct guestfs_int_bool *r;\n"; "NULL"
5994 | RPVList n -> pr " struct guestfs_lvm_pv_list *r;\n"; "NULL"
5995 | RVGList n -> pr " struct guestfs_lvm_vg_list *r;\n"; "NULL"
5996 | RLVList n -> pr " struct guestfs_lvm_lv_list *r;\n"; "NULL"
5997 | RStat n -> pr " struct guestfs_stat *r;\n"; "NULL"
5998 | RStatVFS n -> pr " struct guestfs_statvfs *r;\n"; "NULL" in
6002 | String n | FileIn n | FileOut n -> pr " const char *%s;\n" n
6003 | OptString n -> pr " const char *%s;\n" n
6005 pr " PyObject *py_%s;\n" n;
6006 pr " const char **%s;\n" n
6007 | Bool n -> pr " int %s;\n" n
6008 | Int n -> pr " int %s;\n" n
6013 (* Convert the parameters. *)
6014 pr " if (!PyArg_ParseTuple (args, (char *) \"O";
6017 | String _ | FileIn _ | FileOut _ -> pr "s"
6018 | OptString _ -> pr "z"
6019 | StringList _ -> pr "O"
6020 | Bool _ -> pr "i" (* XXX Python has booleans? *)
6023 pr ":guestfs_%s\",\n" name;
6027 | String n | FileIn n | FileOut n -> pr ", &%s" n
6028 | OptString n -> pr ", &%s" n
6029 | StringList n -> pr ", &py_%s" n
6030 | Bool n -> pr ", &%s" n
6031 | Int n -> pr ", &%s" n
6035 pr " return NULL;\n";
6037 pr " g = get_handle (py_g);\n";
6040 | String _ | FileIn _ | FileOut _ | OptString _ | Bool _ | Int _ -> ()
6042 pr " %s = get_string_list (py_%s);\n" n n;
6043 pr " if (!%s) return NULL;\n" n
6048 pr " r = guestfs_%s " name;
6049 generate_call_args ~handle:"g" (snd style);
6054 | String _ | FileIn _ | FileOut _ | OptString _ | Bool _ | Int _ -> ()
6056 pr " free (%s);\n" n
6059 pr " if (r == %s) {\n" error_code;
6060 pr " PyErr_SetString (PyExc_RuntimeError, guestfs_last_error (g));\n";
6061 pr " return NULL;\n";
6065 (match fst style with
6067 pr " Py_INCREF (Py_None);\n";
6068 pr " py_r = Py_None;\n"
6070 | RBool _ -> pr " py_r = PyInt_FromLong ((long) r);\n"
6071 | RInt64 _ -> pr " py_r = PyLong_FromLongLong (r);\n"
6072 | RConstString _ -> pr " py_r = PyString_FromString (r);\n"
6074 pr " py_r = PyString_FromString (r);\n";
6077 pr " py_r = put_string_list (r);\n";
6078 pr " free_strings (r);\n"
6080 pr " py_r = PyTuple_New (2);\n";
6081 pr " PyTuple_SetItem (py_r, 0, PyInt_FromLong ((long) r->i));\n";
6082 pr " PyTuple_SetItem (py_r, 1, PyInt_FromLong ((long) r->b));\n";
6083 pr " guestfs_free_int_bool (r);\n"
6085 pr " py_r = put_lvm_pv_list (r);\n";
6086 pr " guestfs_free_lvm_pv_list (r);\n"
6088 pr " py_r = put_lvm_vg_list (r);\n";
6089 pr " guestfs_free_lvm_vg_list (r);\n"
6091 pr " py_r = put_lvm_lv_list (r);\n";
6092 pr " guestfs_free_lvm_lv_list (r);\n"
6094 pr " py_r = put_stat (r);\n";
6097 pr " py_r = put_statvfs (r);\n";
6100 pr " py_r = put_table (r);\n";
6101 pr " free_strings (r);\n"
6104 pr " return py_r;\n";
6109 (* Table of functions. *)
6110 pr "static PyMethodDef methods[] = {\n";
6111 pr " { (char *) \"create\", py_guestfs_create, METH_VARARGS, NULL },\n";
6112 pr " { (char *) \"close\", py_guestfs_close, METH_VARARGS, NULL },\n";
6114 fun (name, _, _, _, _, _, _) ->
6115 pr " { (char *) \"%s\", py_guestfs_%s, METH_VARARGS, NULL },\n"
6118 pr " { NULL, NULL, 0, NULL }\n";
6122 (* Init function. *)
6125 initlibguestfsmod (void)
6127 static int initialized = 0;
6129 if (initialized) return;
6130 Py_InitModule ((char *) \"libguestfsmod\", methods);
6135 (* Generate Python module. *)
6136 and generate_python_py () =
6137 generate_header HashStyle LGPLv2;
6140 u\"\"\"Python bindings for libguestfs
6143 g = guestfs.GuestFS ()
6144 g.add_drive (\"guest.img\")
6147 parts = g.list_partitions ()
6149 The guestfs module provides a Python binding to the libguestfs API
6150 for examining and modifying virtual machine disk images.
6152 Amongst the things this is good for: making batch configuration
6153 changes to guests, getting disk used/free statistics (see also:
6154 virt-df), migrating between virtualization systems (see also:
6155 virt-p2v), performing partial backups, performing partial guest
6156 clones, cloning guests and changing registry/UUID/hostname info, and
6159 Libguestfs uses Linux kernel and qemu code, and can access any type of
6160 guest filesystem that Linux and qemu can, including but not limited
6161 to: ext2/3/4, btrfs, FAT and NTFS, LVM, many different disk partition
6162 schemes, qcow, qcow2, vmdk.
6164 Libguestfs provides ways to enumerate guest storage (eg. partitions,
6165 LVs, what filesystem is in each LV, etc.). It can also run commands
6166 in the context of the guest. Also you can access filesystems over FTP.
6168 Errors which happen while using the API are turned into Python
6169 RuntimeError exceptions.
6171 To create a guestfs handle you usually have to perform the following
6174 # Create the handle, call add_drive at least once, and possibly
6175 # several times if the guest has multiple block devices:
6176 g = guestfs.GuestFS ()
6177 g.add_drive (\"guest.img\")
6179 # Launch the qemu subprocess and wait for it to become ready:
6183 # Now you can issue commands, for example:
6188 import libguestfsmod
6191 \"\"\"Instances of this class are libguestfs API handles.\"\"\"
6193 def __init__ (self):
6194 \"\"\"Create a new libguestfs handle.\"\"\"
6195 self._o = libguestfsmod.create ()
6198 libguestfsmod.close (self._o)
6203 fun (name, style, _, flags, _, _, longdesc) ->
6205 generate_call_args ~handle:"self" (snd style);
6208 if not (List.mem NotInDocs flags) then (
6209 let doc = replace_str longdesc "C<guestfs_" "C<g." in
6211 match fst style with
6212 | RErr | RInt _ | RInt64 _ | RBool _ | RConstString _
6215 doc ^ "\n\nThis function returns a list of strings."
6217 doc ^ "\n\nThis function returns a tuple (int, bool).\n"
6219 doc ^ "\n\nThis function returns a list of PVs. Each PV is represented as a dictionary."
6221 doc ^ "\n\nThis function returns a list of VGs. Each VG is represented as a dictionary."
6223 doc ^ "\n\nThis function returns a list of LVs. Each LV is represented as a dictionary."
6225 doc ^ "\n\nThis function returns a dictionary, with keys matching the various fields in the stat structure."
6227 doc ^ "\n\nThis function returns a dictionary, with keys matching the various fields in the statvfs structure."
6229 doc ^ "\n\nThis function returns a dictionary." in
6231 if List.mem ProtocolLimitWarning flags then
6232 doc ^ "\n\n" ^ protocol_limit_warning
6235 if List.mem DangerWillRobinson flags then
6236 doc ^ "\n\n" ^ danger_will_robinson
6238 let doc = pod2text ~width:60 name doc in
6239 let doc = List.map (fun line -> replace_str line "\\" "\\\\") doc in
6240 let doc = String.concat "\n " doc in
6241 pr " u\"\"\"%s\"\"\"\n" doc;
6243 pr " return libguestfsmod.%s " name;
6244 generate_call_args ~handle:"self._o" (snd style);
6249 (* Useful if you need the longdesc POD text as plain text. Returns a
6252 * This is the slowest thing about autogeneration.
6254 and pod2text ~width name longdesc =
6255 let filename, chan = Filename.open_temp_file "gen" ".tmp" in
6256 fprintf chan "=head1 %s\n\n%s\n" name longdesc;
6258 let cmd = sprintf "pod2text -w %d %s" width (Filename.quote filename) in
6259 let chan = Unix.open_process_in cmd in
6260 let lines = ref [] in
6262 let line = input_line chan in
6263 if i = 1 then (* discard the first line of output *)
6266 let line = triml line in
6267 lines := line :: !lines;
6270 let lines = try loop 1 with End_of_file -> List.rev !lines in
6271 Unix.unlink filename;
6272 match Unix.close_process_in chan with
6273 | Unix.WEXITED 0 -> lines
6275 failwithf "pod2text: process exited with non-zero status (%d)" i
6276 | Unix.WSIGNALED i | Unix.WSTOPPED i ->
6277 failwithf "pod2text: process signalled or stopped by signal %d" i
6279 (* Generate ruby bindings. *)
6280 and generate_ruby_c () =
6281 generate_header CStyle LGPLv2;
6289 #include \"guestfs.h\"
6291 #include \"extconf.h\"
6293 /* For Ruby < 1.9 */
6295 #define RARRAY_LEN(r) (RARRAY((r))->len)
6298 static VALUE m_guestfs; /* guestfs module */
6299 static VALUE c_guestfs; /* guestfs_h handle */
6300 static VALUE e_Error; /* used for all errors */
6302 static void ruby_guestfs_free (void *p)
6305 guestfs_close ((guestfs_h *) p);
6308 static VALUE ruby_guestfs_create (VALUE m)
6312 g = guestfs_create ();
6314 rb_raise (e_Error, \"failed to create guestfs handle\");
6316 /* Don't print error messages to stderr by default. */
6317 guestfs_set_error_handler (g, NULL, NULL);
6319 /* Wrap it, and make sure the close function is called when the
6322 return Data_Wrap_Struct (c_guestfs, NULL, ruby_guestfs_free, g);
6325 static VALUE ruby_guestfs_close (VALUE gv)
6328 Data_Get_Struct (gv, guestfs_h, g);
6330 ruby_guestfs_free (g);
6331 DATA_PTR (gv) = NULL;
6339 fun (name, style, _, _, _, _, _) ->
6340 pr "static VALUE ruby_guestfs_%s (VALUE gv" name;
6341 List.iter (fun arg -> pr ", VALUE %sv" (name_of_argt arg)) (snd style);
6344 pr " guestfs_h *g;\n";
6345 pr " Data_Get_Struct (gv, guestfs_h, g);\n";
6347 pr " rb_raise (rb_eArgError, \"%%s: used handle after closing it\", \"%s\");\n"
6353 | String n | FileIn n | FileOut n ->
6354 pr " const char *%s = StringValueCStr (%sv);\n" n n;
6356 pr " rb_raise (rb_eTypeError, \"expected string for parameter %%s of %%s\",\n";
6357 pr " \"%s\", \"%s\");\n" n name
6359 pr " const char *%s = !NIL_P (%sv) ? StringValueCStr (%sv) : NULL;\n" n n n
6363 pr " int i, len;\n";
6364 pr " len = RARRAY_LEN (%sv);\n" n;
6365 pr " %s = guestfs_safe_malloc (g, sizeof (char *) * (len+1));\n"
6367 pr " for (i = 0; i < len; ++i) {\n";
6368 pr " VALUE v = rb_ary_entry (%sv, i);\n" n;
6369 pr " %s[i] = StringValueCStr (v);\n" n;
6371 pr " %s[len] = NULL;\n" n;
6374 pr " int %s = RTEST (%sv);\n" n n
6376 pr " int %s = NUM2INT (%sv);\n" n n
6381 match fst style with
6382 | RErr | RInt _ | RBool _ -> pr " int r;\n"; "-1"
6383 | RInt64 _ -> pr " int64_t r;\n"; "-1"
6384 | RConstString _ -> pr " const char *r;\n"; "NULL"
6385 | RString _ -> pr " char *r;\n"; "NULL"
6386 | RStringList _ | RHashtable _ -> pr " char **r;\n"; "NULL"
6387 | RIntBool _ -> pr " struct guestfs_int_bool *r;\n"; "NULL"
6388 | RPVList n -> pr " struct guestfs_lvm_pv_list *r;\n"; "NULL"
6389 | RVGList n -> pr " struct guestfs_lvm_vg_list *r;\n"; "NULL"
6390 | RLVList n -> pr " struct guestfs_lvm_lv_list *r;\n"; "NULL"
6391 | RStat n -> pr " struct guestfs_stat *r;\n"; "NULL"
6392 | RStatVFS n -> pr " struct guestfs_statvfs *r;\n"; "NULL" in
6395 pr " r = guestfs_%s " name;
6396 generate_call_args ~handle:"g" (snd style);
6401 | String _ | FileIn _ | FileOut _ | OptString _ | Bool _ | Int _ -> ()
6403 pr " free (%s);\n" n
6406 pr " if (r == %s)\n" error_code;
6407 pr " rb_raise (e_Error, \"%%s\", guestfs_last_error (g));\n";
6410 (match fst style with
6412 pr " return Qnil;\n"
6413 | RInt _ | RBool _ ->
6414 pr " return INT2NUM (r);\n"
6416 pr " return ULL2NUM (r);\n"
6418 pr " return rb_str_new2 (r);\n";
6420 pr " VALUE rv = rb_str_new2 (r);\n";
6424 pr " int i, len = 0;\n";
6425 pr " for (i = 0; r[i] != NULL; ++i) len++;\n";
6426 pr " VALUE rv = rb_ary_new2 (len);\n";
6427 pr " for (i = 0; r[i] != NULL; ++i) {\n";
6428 pr " rb_ary_push (rv, rb_str_new2 (r[i]));\n";
6429 pr " free (r[i]);\n";
6434 pr " VALUE rv = rb_ary_new2 (2);\n";
6435 pr " rb_ary_push (rv, INT2NUM (r->i));\n";
6436 pr " rb_ary_push (rv, INT2NUM (r->b));\n";
6437 pr " guestfs_free_int_bool (r);\n";
6440 generate_ruby_lvm_code "pv" pv_cols
6442 generate_ruby_lvm_code "vg" vg_cols
6444 generate_ruby_lvm_code "lv" lv_cols
6446 pr " VALUE rv = rb_hash_new ();\n";
6450 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), ULL2NUM (r->%s));\n" name name
6455 pr " VALUE rv = rb_hash_new ();\n";
6459 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), ULL2NUM (r->%s));\n" name name
6464 pr " VALUE rv = rb_hash_new ();\n";
6466 pr " for (i = 0; r[i] != NULL; i+=2) {\n";
6467 pr " rb_hash_aset (rv, rb_str_new2 (r[i]), rb_str_new2 (r[i+1]));\n";
6468 pr " free (r[i]);\n";
6469 pr " free (r[i+1]);\n";
6480 /* Initialize the module. */
6481 void Init__guestfs ()
6483 m_guestfs = rb_define_module (\"Guestfs\");
6484 c_guestfs = rb_define_class_under (m_guestfs, \"Guestfs\", rb_cObject);
6485 e_Error = rb_define_class_under (m_guestfs, \"Error\", rb_eStandardError);
6487 rb_define_module_function (m_guestfs, \"create\", ruby_guestfs_create, 0);
6488 rb_define_method (c_guestfs, \"close\", ruby_guestfs_close, 0);
6491 (* Define the rest of the methods. *)
6493 fun (name, style, _, _, _, _, _) ->
6494 pr " rb_define_method (c_guestfs, \"%s\",\n" name;
6495 pr " ruby_guestfs_%s, %d);\n" name (List.length (snd style))
6500 (* Ruby code to return an LVM struct list. *)
6501 and generate_ruby_lvm_code typ cols =
6502 pr " VALUE rv = rb_ary_new2 (r->len);\n";
6504 pr " for (i = 0; i < r->len; ++i) {\n";
6505 pr " VALUE hv = rb_hash_new ();\n";
6509 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), rb_str_new2 (r->val[i].%s));\n" name name
6511 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), rb_str_new (r->val[i].%s, 32));\n" name name
6514 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), ULL2NUM (r->val[i].%s));\n" name name
6515 | name, `OptPercent ->
6516 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), rb_dbl2big (r->val[i].%s));\n" name name
6518 pr " rb_ary_push (rv, hv);\n";
6520 pr " guestfs_free_lvm_%s_list (r);\n" typ;
6523 (* Generate Java bindings GuestFS.java file. *)
6524 and generate_java_java () =
6525 generate_header CStyle LGPLv2;
6528 package com.redhat.et.libguestfs;
6530 import java.util.HashMap;
6531 import com.redhat.et.libguestfs.LibGuestFSException;
6532 import com.redhat.et.libguestfs.PV;
6533 import com.redhat.et.libguestfs.VG;
6534 import com.redhat.et.libguestfs.LV;
6535 import com.redhat.et.libguestfs.Stat;
6536 import com.redhat.et.libguestfs.StatVFS;
6537 import com.redhat.et.libguestfs.IntBool;
6540 * The GuestFS object is a libguestfs handle.
6544 public class GuestFS {
6545 // Load the native code.
6547 System.loadLibrary (\"guestfs_jni\");
6551 * The native guestfs_h pointer.
6556 * Create a libguestfs handle.
6558 * @throws LibGuestFSException
6560 public GuestFS () throws LibGuestFSException
6564 private native long _create () throws LibGuestFSException;
6567 * Close a libguestfs handle.
6569 * You can also leave handles to be collected by the garbage
6570 * collector, but this method ensures that the resources used
6571 * by the handle are freed up immediately. If you call any
6572 * other methods after closing the handle, you will get an
6575 * @throws LibGuestFSException
6577 public void close () throws LibGuestFSException
6583 private native void _close (long g) throws LibGuestFSException;
6585 public void finalize () throws LibGuestFSException
6593 fun (name, style, _, flags, _, shortdesc, longdesc) ->
6594 if not (List.mem NotInDocs flags); then (
6595 let doc = replace_str longdesc "C<guestfs_" "C<g." in
6597 if List.mem ProtocolLimitWarning flags then
6598 doc ^ "\n\n" ^ protocol_limit_warning
6601 if List.mem DangerWillRobinson flags then
6602 doc ^ "\n\n" ^ danger_will_robinson
6604 let doc = pod2text ~width:60 name doc in
6605 let doc = List.map ( (* RHBZ#501883 *)
6608 | nonempty -> nonempty
6610 let doc = String.concat "\n * " doc in
6613 pr " * %s\n" shortdesc;
6616 pr " * @throws LibGuestFSException\n";
6620 generate_java_prototype ~public:true ~semicolon:false name style;
6623 pr " if (g == 0)\n";
6624 pr " throw new LibGuestFSException (\"%s: handle is closed\");\n"
6627 if fst style <> RErr then pr "return ";
6629 generate_call_args ~handle:"g" (snd style);
6633 generate_java_prototype ~privat:true ~native:true name style;
6640 and generate_java_prototype ?(public=false) ?(privat=false) ?(native=false)
6641 ?(semicolon=true) name style =
6642 if privat then pr "private ";
6643 if public then pr "public ";
6644 if native then pr "native ";
6647 (match fst style with
6648 | RErr -> pr "void ";
6649 | RInt _ -> pr "int ";
6650 | RInt64 _ -> pr "long ";
6651 | RBool _ -> pr "boolean ";
6652 | RConstString _ | RString _ -> pr "String ";
6653 | RStringList _ -> pr "String[] ";
6654 | RIntBool _ -> pr "IntBool ";
6655 | RPVList _ -> pr "PV[] ";
6656 | RVGList _ -> pr "VG[] ";
6657 | RLVList _ -> pr "LV[] ";
6658 | RStat _ -> pr "Stat ";
6659 | RStatVFS _ -> pr "StatVFS ";
6660 | RHashtable _ -> pr "HashMap<String,String> ";
6663 if native then pr "_%s " name else pr "%s " name;
6665 let needs_comma = ref false in
6674 if !needs_comma then pr ", ";
6675 needs_comma := true;
6692 pr " throws LibGuestFSException";
6693 if semicolon then pr ";"
6695 and generate_java_struct typ cols =
6696 generate_header CStyle LGPLv2;
6699 package com.redhat.et.libguestfs;
6702 * Libguestfs %s structure.
6713 | name, `UUID -> pr " public String %s;\n" name
6715 | name, `Int -> pr " public long %s;\n" name
6716 | name, `OptPercent ->
6717 pr " /* The next field is [0..100] or -1 meaning 'not present': */\n";
6718 pr " public float %s;\n" name
6723 and generate_java_c () =
6724 generate_header CStyle LGPLv2;
6731 #include \"com_redhat_et_libguestfs_GuestFS.h\"
6732 #include \"guestfs.h\"
6734 /* Note that this function returns. The exception is not thrown
6735 * until after the wrapper function returns.
6738 throw_exception (JNIEnv *env, const char *msg)
6741 cl = (*env)->FindClass (env,
6742 \"com/redhat/et/libguestfs/LibGuestFSException\");
6743 (*env)->ThrowNew (env, cl, msg);
6746 JNIEXPORT jlong JNICALL
6747 Java_com_redhat_et_libguestfs_GuestFS__1create
6748 (JNIEnv *env, jobject obj)
6752 g = guestfs_create ();
6754 throw_exception (env, \"GuestFS.create: failed to allocate handle\");
6757 guestfs_set_error_handler (g, NULL, NULL);
6758 return (jlong) (long) g;
6761 JNIEXPORT void JNICALL
6762 Java_com_redhat_et_libguestfs_GuestFS__1close
6763 (JNIEnv *env, jobject obj, jlong jg)
6765 guestfs_h *g = (guestfs_h *) (long) jg;
6772 fun (name, style, _, _, _, _, _) ->
6774 (match fst style with
6775 | RErr -> pr "void ";
6776 | RInt _ -> pr "jint ";
6777 | RInt64 _ -> pr "jlong ";
6778 | RBool _ -> pr "jboolean ";
6779 | RConstString _ | RString _ -> pr "jstring ";
6780 | RIntBool _ | RStat _ | RStatVFS _ | RHashtable _ ->
6782 | RStringList _ | RPVList _ | RVGList _ | RLVList _ ->
6786 pr "Java_com_redhat_et_libguestfs_GuestFS_";
6787 pr "%s" (replace_str ("_" ^ name) "_" "_1");
6789 pr " (JNIEnv *env, jobject obj, jlong jg";
6796 pr ", jstring j%s" n
6798 pr ", jobjectArray j%s" n
6800 pr ", jboolean j%s" n
6806 pr " guestfs_h *g = (guestfs_h *) (long) jg;\n";
6807 let error_code, no_ret =
6808 match fst style with
6809 | RErr -> pr " int r;\n"; "-1", ""
6811 | RInt _ -> pr " int r;\n"; "-1", "0"
6812 | RInt64 _ -> pr " int64_t r;\n"; "-1", "0"
6813 | RConstString _ -> pr " const char *r;\n"; "NULL", "NULL"
6815 pr " jstring jr;\n";
6816 pr " char *r;\n"; "NULL", "NULL"
6818 pr " jobjectArray jr;\n";
6821 pr " jstring jstr;\n";
6822 pr " char **r;\n"; "NULL", "NULL"
6824 pr " jobject jr;\n";
6826 pr " jfieldID fl;\n";
6827 pr " struct guestfs_int_bool *r;\n"; "NULL", "NULL"
6829 pr " jobject jr;\n";
6831 pr " jfieldID fl;\n";
6832 pr " struct guestfs_stat *r;\n"; "NULL", "NULL"
6834 pr " jobject jr;\n";
6836 pr " jfieldID fl;\n";
6837 pr " struct guestfs_statvfs *r;\n"; "NULL", "NULL"
6839 pr " jobjectArray jr;\n";
6841 pr " jfieldID fl;\n";
6842 pr " jobject jfl;\n";
6843 pr " struct guestfs_lvm_pv_list *r;\n"; "NULL", "NULL"
6845 pr " jobjectArray jr;\n";
6847 pr " jfieldID fl;\n";
6848 pr " jobject jfl;\n";
6849 pr " struct guestfs_lvm_vg_list *r;\n"; "NULL", "NULL"
6851 pr " jobjectArray jr;\n";
6853 pr " jfieldID fl;\n";
6854 pr " jobject jfl;\n";
6855 pr " struct guestfs_lvm_lv_list *r;\n"; "NULL", "NULL"
6856 | RHashtable _ -> pr " char **r;\n"; "NULL", "NULL" in
6863 pr " const char *%s;\n" n
6865 pr " int %s_len;\n" n;
6866 pr " const char **%s;\n" n
6873 (match fst style with
6874 | RStringList _ | RPVList _ | RVGList _ | RLVList _ -> true
6875 | RErr | RBool _ | RInt _ | RInt64 _ | RConstString _
6876 | RString _ | RIntBool _ | RStat _ | RStatVFS _
6877 | RHashtable _ -> false) ||
6878 List.exists (function StringList _ -> true | _ -> false) (snd style) in
6884 (* Get the parameters. *)
6890 pr " %s = (*env)->GetStringUTFChars (env, j%s, NULL);\n" n n
6892 (* This is completely undocumented, but Java null becomes
6895 pr " %s = j%s ? (*env)->GetStringUTFChars (env, j%s, NULL) : NULL;\n" n n n
6897 pr " %s_len = (*env)->GetArrayLength (env, j%s);\n" n n;
6898 pr " %s = guestfs_safe_malloc (g, sizeof (char *) * (%s_len+1));\n" n n;
6899 pr " for (i = 0; i < %s_len; ++i) {\n" n;
6900 pr " jobject o = (*env)->GetObjectArrayElement (env, j%s, i);\n"
6902 pr " %s[i] = (*env)->GetStringUTFChars (env, o, NULL);\n" n;
6904 pr " %s[%s_len] = NULL;\n" n n;
6907 pr " %s = j%s;\n" n n
6910 (* Make the call. *)
6911 pr " r = guestfs_%s " name;
6912 generate_call_args ~handle:"g" (snd style);
6915 (* Release the parameters. *)
6921 pr " (*env)->ReleaseStringUTFChars (env, j%s, %s);\n" n n
6924 pr " (*env)->ReleaseStringUTFChars (env, j%s, %s);\n" n n
6926 pr " for (i = 0; i < %s_len; ++i) {\n" n;
6927 pr " jobject o = (*env)->GetObjectArrayElement (env, j%s, i);\n"
6929 pr " (*env)->ReleaseStringUTFChars (env, o, %s[i]);\n" n;
6931 pr " free (%s);\n" n
6936 (* Check for errors. *)
6937 pr " if (r == %s) {\n" error_code;
6938 pr " throw_exception (env, guestfs_last_error (g));\n";
6939 pr " return %s;\n" no_ret;
6943 (match fst style with
6945 | RInt _ -> pr " return (jint) r;\n"
6946 | RBool _ -> pr " return (jboolean) r;\n"
6947 | RInt64 _ -> pr " return (jlong) r;\n"
6948 | RConstString _ -> pr " return (*env)->NewStringUTF (env, r);\n"
6950 pr " jr = (*env)->NewStringUTF (env, r);\n";
6954 pr " for (r_len = 0; r[r_len] != NULL; ++r_len) ;\n";
6955 pr " cl = (*env)->FindClass (env, \"java/lang/String\");\n";
6956 pr " jstr = (*env)->NewStringUTF (env, \"\");\n";
6957 pr " jr = (*env)->NewObjectArray (env, r_len, cl, jstr);\n";
6958 pr " for (i = 0; i < r_len; ++i) {\n";
6959 pr " jstr = (*env)->NewStringUTF (env, r[i]);\n";
6960 pr " (*env)->SetObjectArrayElement (env, jr, i, jstr);\n";
6961 pr " free (r[i]);\n";
6966 pr " cl = (*env)->FindClass (env, \"com/redhat/et/libguestfs/IntBool\");\n";
6967 pr " jr = (*env)->AllocObject (env, cl);\n";
6968 pr " fl = (*env)->GetFieldID (env, cl, \"i\", \"I\");\n";
6969 pr " (*env)->SetIntField (env, jr, fl, r->i);\n";
6970 pr " fl = (*env)->GetFieldID (env, cl, \"i\", \"Z\");\n";
6971 pr " (*env)->SetBooleanField (env, jr, fl, r->b);\n";
6972 pr " guestfs_free_int_bool (r);\n";
6975 pr " cl = (*env)->FindClass (env, \"com/redhat/et/libguestfs/Stat\");\n";
6976 pr " jr = (*env)->AllocObject (env, cl);\n";
6980 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"J\");\n"
6982 pr " (*env)->SetLongField (env, jr, fl, r->%s);\n" name;
6987 pr " cl = (*env)->FindClass (env, \"com/redhat/et/libguestfs/StatVFS\");\n";
6988 pr " jr = (*env)->AllocObject (env, cl);\n";
6992 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"J\");\n"
6994 pr " (*env)->SetLongField (env, jr, fl, r->%s);\n" name;
6999 generate_java_lvm_return "pv" "PV" pv_cols
7001 generate_java_lvm_return "vg" "VG" vg_cols
7003 generate_java_lvm_return "lv" "LV" lv_cols
7006 pr " throw_exception (env, \"%s: internal error: please let us know how to make a Java HashMap from JNI bindings!\");\n" name;
7007 pr " return NULL;\n"
7014 and generate_java_lvm_return typ jtyp cols =
7015 pr " cl = (*env)->FindClass (env, \"com/redhat/et/libguestfs/%s\");\n" jtyp;
7016 pr " jr = (*env)->NewObjectArray (env, r->len, cl, NULL);\n";
7017 pr " for (i = 0; i < r->len; ++i) {\n";
7018 pr " jfl = (*env)->AllocObject (env, cl);\n";
7022 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
7023 pr " (*env)->SetObjectField (env, jfl, fl, (*env)->NewStringUTF (env, r->val[i].%s));\n" name;
7026 pr " char s[33];\n";
7027 pr " memcpy (s, r->val[i].%s, 32);\n" name;
7029 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
7030 pr " (*env)->SetObjectField (env, jfl, fl, (*env)->NewStringUTF (env, s));\n";
7032 | name, (`Bytes|`Int) ->
7033 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"J\");\n" name;
7034 pr " (*env)->SetLongField (env, jfl, fl, r->val[i].%s);\n" name;
7035 | name, `OptPercent ->
7036 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"F\");\n" name;
7037 pr " (*env)->SetFloatField (env, jfl, fl, r->val[i].%s);\n" name;
7039 pr " (*env)->SetObjectArrayElement (env, jfl, i, jfl);\n";
7041 pr " guestfs_free_lvm_%s_list (r);\n" typ;
7044 and generate_haskell_hs () =
7045 generate_header HaskellStyle LGPLv2;
7047 (* XXX We only know how to generate partial FFI for Haskell
7048 * at the moment. Please help out!
7050 let can_generate style =
7051 let check_no_bad_args =
7052 List.for_all (function Bool _ | Int _ -> false | _ -> true)
7055 | RErr, args -> check_no_bad_args args
7068 | RHashtable _, _ -> false in
7071 {-# INCLUDE <guestfs.h> #-}
7072 {-# LANGUAGE ForeignFunctionInterface #-}
7077 (* List out the names of the actions we want to export. *)
7079 fun (name, style, _, _, _, _, _) ->
7080 if can_generate style then pr ",\n %s" name
7088 import Control.Exception
7089 import Data.Typeable
7091 data GuestfsS = GuestfsS -- represents the opaque C struct
7092 type GuestfsP = Ptr GuestfsS -- guestfs_h *
7093 type GuestfsH = ForeignPtr GuestfsS -- guestfs_h * with attached finalizer
7095 -- XXX define properly later XXX
7099 data IntBool = IntBool
7101 data StatVFS = StatVFS
7102 data Hashtable = Hashtable
7104 foreign import ccall unsafe \"guestfs_create\" c_create
7106 foreign import ccall unsafe \"&guestfs_close\" c_close
7107 :: FunPtr (GuestfsP -> IO ())
7108 foreign import ccall unsafe \"guestfs_set_error_handler\" c_set_error_handler
7109 :: GuestfsP -> Ptr CInt -> Ptr CInt -> IO ()
7111 create :: IO GuestfsH
7114 c_set_error_handler p nullPtr nullPtr
7115 h <- newForeignPtr c_close p
7118 foreign import ccall unsafe \"guestfs_last_error\" c_last_error
7119 :: GuestfsP -> IO CString
7121 -- last_error :: GuestfsH -> IO (Maybe String)
7122 -- last_error h = do
7123 -- str <- withForeignPtr h (\\p -> c_last_error p)
7124 -- maybePeek peekCString str
7126 last_error :: GuestfsH -> IO (String)
7128 str <- withForeignPtr h (\\p -> c_last_error p)
7130 then return \"no error\"
7131 else peekCString str
7135 (* Generate wrappers for each foreign function. *)
7137 fun (name, style, _, _, _, _, _) ->
7138 if can_generate style then (
7139 pr "foreign import ccall unsafe \"guestfs_%s\" c_%s\n" name name;
7141 generate_haskell_prototype ~handle:"GuestfsP" style;
7145 generate_haskell_prototype ~handle:"GuestfsH" ~hs:true style;
7147 pr "%s %s = do\n" name
7148 (String.concat " " ("h" :: List.map name_of_argt (snd style)));
7154 | String n -> pr "withCString %s $ \\%s -> " n n
7155 | OptString n -> pr "maybeWith withCString %s $ \\%s -> " n n
7156 | StringList n -> pr "withMany withCString %s $ \\%s -> withArray0 nullPtr %s $ \\%s -> " n n n n
7158 (* XXX this doesn't work *)
7160 pr " %s = case %s of\n" n n;
7163 pr " in fromIntegral %s $ \\%s ->\n" n n
7164 | Int n -> pr "fromIntegral %s $ \\%s -> " n n
7166 pr "withForeignPtr h (\\p -> c_%s %s)\n" name
7167 (String.concat " " ("p" :: List.map name_of_argt (snd style)));
7168 (match fst style with
7169 | RErr | RInt _ | RInt64 _ | RBool _ ->
7170 pr " if (r == -1)\n";
7172 pr " err <- last_error h\n";
7174 | RConstString _ | RString _ | RStringList _ | RIntBool _
7175 | RPVList _ | RVGList _ | RLVList _ | RStat _ | RStatVFS _
7177 pr " if (r == nullPtr)\n";
7179 pr " err <- last_error h\n";
7182 (match fst style with
7184 pr " else return ()\n"
7186 pr " else return (fromIntegral r)\n"
7188 pr " else return (fromIntegral r)\n"
7190 pr " else return (toBool r)\n"
7201 pr " else return ()\n" (* XXXXXXXXXXXXXXXXXXXX *)
7207 and generate_haskell_prototype ~handle ?(hs = false) style =
7209 let string = if hs then "String" else "CString" in
7210 let int = if hs then "Int" else "CInt" in
7211 let bool = if hs then "Bool" else "CInt" in
7212 let int64 = if hs then "Integer" else "Int64" in
7216 | String _ -> pr "%s" string
7217 | OptString _ -> if hs then pr "Maybe String" else pr "CString"
7218 | StringList _ -> if hs then pr "[String]" else pr "Ptr CString"
7219 | Bool _ -> pr "%s" bool
7220 | Int _ -> pr "%s" int
7221 | FileIn _ -> pr "%s" string
7222 | FileOut _ -> pr "%s" string
7227 (match fst style with
7228 | RErr -> if not hs then pr "CInt"
7229 | RInt _ -> pr "%s" int
7230 | RInt64 _ -> pr "%s" int64
7231 | RBool _ -> pr "%s" bool
7232 | RConstString _ -> pr "%s" string
7233 | RString _ -> pr "%s" string
7234 | RStringList _ -> pr "[%s]" string
7235 | RIntBool _ -> pr "IntBool"
7236 | RPVList _ -> pr "[PV]"
7237 | RVGList _ -> pr "[VG]"
7238 | RLVList _ -> pr "[LV]"
7239 | RStat _ -> pr "Stat"
7240 | RStatVFS _ -> pr "StatVFS"
7241 | RHashtable _ -> pr "Hashtable"
7245 and generate_bindtests () =
7246 generate_header CStyle LGPLv2;
7251 #include <inttypes.h>
7254 #include \"guestfs.h\"
7255 #include \"guestfs_protocol.h\"
7257 #define error guestfs_error
7260 print_strings (char * const* const argv)
7265 for (argc = 0; argv[argc] != NULL; ++argc) {
7266 if (argc > 0) printf (\", \");
7267 printf (\"\\\"%%s\\\"\", argv[argc]);
7272 /* The test0 function prints its parameters to stdout. */
7276 match test_functions with
7277 | [] -> assert false
7278 | test0 :: tests -> test0, tests in
7281 let (name, style, _, _, _, _, _) = test0 in
7282 generate_prototype ~extern:false ~semicolon:false ~newline:true
7283 ~handle:"g" ~prefix:"guestfs_" name style;
7289 | FileOut n -> pr " printf (\"%%s\\n\", %s);\n" n
7290 | OptString n -> pr " printf (\"%%s\\n\", %s ? %s : \"null\");\n" n n
7291 | StringList n -> pr " print_strings (%s);\n" n
7292 | Bool n -> pr " printf (\"%%s\\n\", %s ? \"true\" : \"false\");\n" n
7293 | Int n -> pr " printf (\"%%d\\n\", %s);\n" n
7295 pr " /* Java changes stdout line buffering so we need this: */\n";
7296 pr " fflush (stdout);\n";
7302 fun (name, style, _, _, _, _, _) ->
7303 if String.sub name (String.length name - 3) 3 <> "err" then (
7304 pr "/* Test normal return. */\n";
7305 generate_prototype ~extern:false ~semicolon:false ~newline:true
7306 ~handle:"g" ~prefix:"guestfs_" name style;
7308 (match fst style with
7313 pr " sscanf (val, \"%%d\", &r);\n";
7317 pr " sscanf (val, \"%%\" SCNi64, &r);\n";
7320 pr " return strcmp (val, \"true\") == 0;\n"
7322 (* Can't return the input string here. Return a static
7323 * string so we ensure we get a segfault if the caller
7326 pr " return \"static string\";\n"
7328 pr " return strdup (val);\n"
7330 pr " char **strs;\n";
7332 pr " sscanf (val, \"%%d\", &n);\n";
7333 pr " strs = malloc ((n+1) * sizeof (char *));\n";
7334 pr " for (i = 0; i < n; ++i) {\n";
7335 pr " strs[i] = malloc (16);\n";
7336 pr " snprintf (strs[i], 16, \"%%d\", i);\n";
7338 pr " strs[n] = NULL;\n";
7339 pr " return strs;\n"
7341 pr " struct guestfs_int_bool *r;\n";
7342 pr " r = malloc (sizeof (struct guestfs_int_bool));\n";
7343 pr " sscanf (val, \"%%\" SCNi32, &r->i);\n";
7347 pr " struct guestfs_lvm_pv_list *r;\n";
7349 pr " r = malloc (sizeof (struct guestfs_lvm_pv_list));\n";
7350 pr " sscanf (val, \"%%d\", &r->len);\n";
7351 pr " r->val = calloc (r->len, sizeof (struct guestfs_lvm_pv));\n";
7352 pr " for (i = 0; i < r->len; ++i) {\n";
7353 pr " r->val[i].pv_name = malloc (16);\n";
7354 pr " snprintf (r->val[i].pv_name, 16, \"%%d\", i);\n";
7358 pr " struct guestfs_lvm_vg_list *r;\n";
7360 pr " r = malloc (sizeof (struct guestfs_lvm_vg_list));\n";
7361 pr " sscanf (val, \"%%d\", &r->len);\n";
7362 pr " r->val = calloc (r->len, sizeof (struct guestfs_lvm_vg));\n";
7363 pr " for (i = 0; i < r->len; ++i) {\n";
7364 pr " r->val[i].vg_name = malloc (16);\n";
7365 pr " snprintf (r->val[i].vg_name, 16, \"%%d\", i);\n";
7369 pr " struct guestfs_lvm_lv_list *r;\n";
7371 pr " r = malloc (sizeof (struct guestfs_lvm_lv_list));\n";
7372 pr " sscanf (val, \"%%d\", &r->len);\n";
7373 pr " r->val = calloc (r->len, sizeof (struct guestfs_lvm_lv));\n";
7374 pr " for (i = 0; i < r->len; ++i) {\n";
7375 pr " r->val[i].lv_name = malloc (16);\n";
7376 pr " snprintf (r->val[i].lv_name, 16, \"%%d\", i);\n";
7380 pr " struct guestfs_stat *r;\n";
7381 pr " r = calloc (1, sizeof (*r));\n";
7382 pr " sscanf (val, \"%%\" SCNi64, &r->dev);\n";
7385 pr " struct guestfs_statvfs *r;\n";
7386 pr " r = calloc (1, sizeof (*r));\n";
7387 pr " sscanf (val, \"%%\" SCNi64, &r->bsize);\n";
7390 pr " char **strs;\n";
7392 pr " sscanf (val, \"%%d\", &n);\n";
7393 pr " strs = malloc ((n*2+1) * sizeof (char *));\n";
7394 pr " for (i = 0; i < n; ++i) {\n";
7395 pr " strs[i*2] = malloc (16);\n";
7396 pr " strs[i*2+1] = malloc (16);\n";
7397 pr " snprintf (strs[i*2], 16, \"%%d\", i);\n";
7398 pr " snprintf (strs[i*2+1], 16, \"%%d\", i);\n";
7400 pr " strs[n*2] = NULL;\n";
7401 pr " return strs;\n"
7406 pr "/* Test error return. */\n";
7407 generate_prototype ~extern:false ~semicolon:false ~newline:true
7408 ~handle:"g" ~prefix:"guestfs_" name style;
7410 pr " error (g, \"error\");\n";
7411 (match fst style with
7412 | RErr | RInt _ | RInt64 _ | RBool _ ->
7415 | RString _ | RStringList _ | RIntBool _
7416 | RPVList _ | RVGList _ | RLVList _ | RStat _ | RStatVFS _
7418 pr " return NULL;\n"
7425 and generate_ocaml_bindtests () =
7426 generate_header OCamlStyle GPLv2;
7430 let g = Guestfs.create () in
7437 | CallString s -> "\"" ^ s ^ "\""
7438 | CallOptString None -> "None"
7439 | CallOptString (Some s) -> sprintf "(Some \"%s\")" s
7440 | CallStringList xs ->
7441 "[|" ^ String.concat ";" (List.map (sprintf "\"%s\"") xs) ^ "|]"
7442 | CallInt i when i >= 0 -> string_of_int i
7443 | CallInt i (* when i < 0 *) -> "(" ^ string_of_int i ^ ")"
7444 | CallBool b -> string_of_bool b
7449 generate_lang_bindtests (
7450 fun f args -> pr " Guestfs.%s g %s;\n" f (mkargs args)
7453 pr "print_endline \"EOF\"\n"
7455 and generate_perl_bindtests () =
7456 pr "#!/usr/bin/perl -w\n";
7457 generate_header HashStyle GPLv2;
7464 my $g = Sys::Guestfs->new ();
7468 String.concat ", " (
7471 | CallString s -> "\"" ^ s ^ "\""
7472 | CallOptString None -> "undef"
7473 | CallOptString (Some s) -> sprintf "\"%s\"" s
7474 | CallStringList xs ->
7475 "[" ^ String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "]"
7476 | CallInt i -> string_of_int i
7477 | CallBool b -> if b then "1" else "0"
7482 generate_lang_bindtests (
7483 fun f args -> pr "$g->%s (%s);\n" f (mkargs args)
7486 pr "print \"EOF\\n\"\n"
7488 and generate_python_bindtests () =
7489 generate_header HashStyle GPLv2;
7494 g = guestfs.GuestFS ()
7498 String.concat ", " (
7501 | CallString s -> "\"" ^ s ^ "\""
7502 | CallOptString None -> "None"
7503 | CallOptString (Some s) -> sprintf "\"%s\"" s
7504 | CallStringList xs ->
7505 "[" ^ String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "]"
7506 | CallInt i -> string_of_int i
7507 | CallBool b -> if b then "1" else "0"
7512 generate_lang_bindtests (
7513 fun f args -> pr "g.%s (%s)\n" f (mkargs args)
7516 pr "print \"EOF\"\n"
7518 and generate_ruby_bindtests () =
7519 generate_header HashStyle GPLv2;
7524 g = Guestfs::create()
7528 String.concat ", " (
7531 | CallString s -> "\"" ^ s ^ "\""
7532 | CallOptString None -> "nil"
7533 | CallOptString (Some s) -> sprintf "\"%s\"" s
7534 | CallStringList xs ->
7535 "[" ^ String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "]"
7536 | CallInt i -> string_of_int i
7537 | CallBool b -> string_of_bool b
7542 generate_lang_bindtests (
7543 fun f args -> pr "g.%s(%s)\n" f (mkargs args)
7546 pr "print \"EOF\\n\"\n"
7548 and generate_java_bindtests () =
7549 generate_header CStyle GPLv2;
7552 import com.redhat.et.libguestfs.*;
7554 public class Bindtests {
7555 public static void main (String[] argv)
7558 GuestFS g = new GuestFS ();
7562 String.concat ", " (
7565 | CallString s -> "\"" ^ s ^ "\""
7566 | CallOptString None -> "null"
7567 | CallOptString (Some s) -> sprintf "\"%s\"" s
7568 | CallStringList xs ->
7570 String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "}"
7571 | CallInt i -> string_of_int i
7572 | CallBool b -> string_of_bool b
7577 generate_lang_bindtests (
7578 fun f args -> pr " g.%s (%s);\n" f (mkargs args)
7582 System.out.println (\"EOF\");
7584 catch (Exception exn) {
7585 System.err.println (exn);
7592 and generate_haskell_bindtests () =
7593 () (* XXX Haskell bindings need to be fleshed out. *)
7595 (* Language-independent bindings tests - we do it this way to
7596 * ensure there is parity in testing bindings across all languages.
7598 and generate_lang_bindtests call =
7599 call "test0" [CallString "abc"; CallOptString (Some "def");
7600 CallStringList []; CallBool false;
7601 CallInt 0; CallString "123"; CallString "456"];
7602 call "test0" [CallString "abc"; CallOptString None;
7603 CallStringList []; CallBool false;
7604 CallInt 0; CallString "123"; CallString "456"];
7605 call "test0" [CallString ""; CallOptString (Some "def");
7606 CallStringList []; CallBool false;
7607 CallInt 0; CallString "123"; CallString "456"];
7608 call "test0" [CallString ""; CallOptString (Some "");
7609 CallStringList []; CallBool false;
7610 CallInt 0; CallString "123"; CallString "456"];
7611 call "test0" [CallString "abc"; CallOptString (Some "def");
7612 CallStringList ["1"]; CallBool false;
7613 CallInt 0; CallString "123"; CallString "456"];
7614 call "test0" [CallString "abc"; CallOptString (Some "def");
7615 CallStringList ["1"; "2"]; CallBool false;
7616 CallInt 0; CallString "123"; CallString "456"];
7617 call "test0" [CallString "abc"; CallOptString (Some "def");
7618 CallStringList ["1"]; CallBool true;
7619 CallInt 0; CallString "123"; CallString "456"];
7620 call "test0" [CallString "abc"; CallOptString (Some "def");
7621 CallStringList ["1"]; CallBool false;
7622 CallInt (-1); CallString "123"; CallString "456"];
7623 call "test0" [CallString "abc"; CallOptString (Some "def");
7624 CallStringList ["1"]; CallBool false;
7625 CallInt (-2); CallString "123"; CallString "456"];
7626 call "test0" [CallString "abc"; CallOptString (Some "def");
7627 CallStringList ["1"]; CallBool false;
7628 CallInt 1; CallString "123"; CallString "456"];
7629 call "test0" [CallString "abc"; CallOptString (Some "def");
7630 CallStringList ["1"]; CallBool false;
7631 CallInt 2; CallString "123"; CallString "456"];
7632 call "test0" [CallString "abc"; CallOptString (Some "def");
7633 CallStringList ["1"]; CallBool false;
7634 CallInt 4095; CallString "123"; CallString "456"];
7635 call "test0" [CallString "abc"; CallOptString (Some "def");
7636 CallStringList ["1"]; CallBool false;
7637 CallInt 0; CallString ""; CallString ""]
7639 (* XXX Add here tests of the return and error functions. *)
7641 let output_to filename =
7642 let filename_new = filename ^ ".new" in
7643 chan := open_out filename_new;
7648 (* Is the new file different from the current file? *)
7649 if Sys.file_exists filename && files_equal filename filename_new then
7650 Unix.unlink filename_new (* same, so skip it *)
7652 (* different, overwrite old one *)
7653 (try Unix.chmod filename 0o644 with Unix.Unix_error _ -> ());
7654 Unix.rename filename_new filename;
7655 Unix.chmod filename 0o444;
7656 printf "written %s\n%!" filename;
7665 if not (Sys.file_exists "configure.ac") then (
7667 You are probably running this from the wrong directory.
7668 Run it from the top source directory using the command
7674 let close = output_to "src/guestfs_protocol.x" in
7678 let close = output_to "src/guestfs-structs.h" in
7679 generate_structs_h ();
7682 let close = output_to "src/guestfs-actions.h" in
7683 generate_actions_h ();
7686 let close = output_to "src/guestfs-actions.c" in
7687 generate_client_actions ();
7690 let close = output_to "daemon/actions.h" in
7691 generate_daemon_actions_h ();
7694 let close = output_to "daemon/stubs.c" in
7695 generate_daemon_actions ();
7698 let close = output_to "capitests/tests.c" in
7702 let close = output_to "src/guestfs-bindtests.c" in
7703 generate_bindtests ();
7706 let close = output_to "fish/cmds.c" in
7707 generate_fish_cmds ();
7710 let close = output_to "fish/completion.c" in
7711 generate_fish_completion ();
7714 let close = output_to "guestfs-structs.pod" in
7715 generate_structs_pod ();
7718 let close = output_to "guestfs-actions.pod" in
7719 generate_actions_pod ();
7722 let close = output_to "guestfish-actions.pod" in
7723 generate_fish_actions_pod ();
7726 let close = output_to "ocaml/guestfs.mli" in
7727 generate_ocaml_mli ();
7730 let close = output_to "ocaml/guestfs.ml" in
7731 generate_ocaml_ml ();
7734 let close = output_to "ocaml/guestfs_c_actions.c" in
7735 generate_ocaml_c ();
7738 let close = output_to "ocaml/bindtests.ml" in
7739 generate_ocaml_bindtests ();
7742 let close = output_to "perl/Guestfs.xs" in
7743 generate_perl_xs ();
7746 let close = output_to "perl/lib/Sys/Guestfs.pm" in
7747 generate_perl_pm ();
7750 let close = output_to "perl/bindtests.pl" in
7751 generate_perl_bindtests ();
7754 let close = output_to "python/guestfs-py.c" in
7755 generate_python_c ();
7758 let close = output_to "python/guestfs.py" in
7759 generate_python_py ();
7762 let close = output_to "python/bindtests.py" in
7763 generate_python_bindtests ();
7766 let close = output_to "ruby/ext/guestfs/_guestfs.c" in
7770 let close = output_to "ruby/bindtests.rb" in
7771 generate_ruby_bindtests ();
7774 let close = output_to "java/com/redhat/et/libguestfs/GuestFS.java" in
7775 generate_java_java ();
7778 let close = output_to "java/com/redhat/et/libguestfs/PV.java" in
7779 generate_java_struct "PV" pv_cols;
7782 let close = output_to "java/com/redhat/et/libguestfs/VG.java" in
7783 generate_java_struct "VG" vg_cols;
7786 let close = output_to "java/com/redhat/et/libguestfs/LV.java" in
7787 generate_java_struct "LV" lv_cols;
7790 let close = output_to "java/com/redhat/et/libguestfs/Stat.java" in
7791 generate_java_struct "Stat" stat_cols;
7794 let close = output_to "java/com/redhat/et/libguestfs/StatVFS.java" in
7795 generate_java_struct "StatVFS" statvfs_cols;
7798 let close = output_to "java/com_redhat_et_libguestfs_GuestFS.c" in
7802 let close = output_to "java/Bindtests.java" in
7803 generate_java_bindtests ();
7806 let close = output_to "haskell/Guestfs.hs" in
7807 generate_haskell_hs ();
7810 let close = output_to "haskell/bindtests.hs" in
7811 generate_haskell_bindtests ();