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 all the
28 * output files. Note that if you are using a separate build directory you
29 * must run generator.ml from the _source_ directory.
31 * IMPORTANT: This script should NOT print any warnings. If it prints
32 * warnings, you should treat them as errors.
41 type style = ret * args
43 (* "RErr" as a return value means an int used as a simple error
44 * indication, ie. 0 or -1.
48 (* "RInt" as a return value means an int which is -1 for error
49 * or any value >= 0 on success. Only use this for smallish
50 * positive ints (0 <= i < 2^30).
54 (* "RInt64" is the same as RInt, but is guaranteed to be able
55 * to return a full 64 bit value, _except_ that -1 means error
56 * (so -1 cannot be a valid, non-error return value).
60 (* "RBool" is a bool return value which can be true/false or
65 (* "RConstString" is a string that refers to a constant value.
66 * The return value must NOT be NULL (since NULL indicates
69 * Try to avoid using this. In particular you cannot use this
70 * for values returned from the daemon, because there is no
71 * thread-safe way to return them in the C API.
73 | RConstString of string
75 (* "RConstOptString" is an even more broken version of
76 * "RConstString". The returned string may be NULL and there
77 * is no way to return an error indication. Avoid using this!
79 | RConstOptString of string
81 (* "RString" is a returned string. It must NOT be NULL, since
82 * a NULL return indicates an error. The caller frees this.
86 (* "RStringList" is a list of strings. No string in the list
87 * can be NULL. The caller frees the strings and the array.
89 | RStringList of string
91 (* "RStruct" is a function which returns a single named structure
92 * or an error indication (in C, a struct, and in other languages
93 * with varying representations, but usually very efficient). See
94 * after the function list below for the structures.
96 | RStruct of string * string (* name of retval, name of struct *)
98 (* "RStructList" is a function which returns either a list/array
99 * of structures (could be zero-length), or an error indication.
101 | RStructList of string * string (* name of retval, name of struct *)
103 (* Key-value pairs of untyped strings. Turns into a hashtable or
104 * dictionary in languages which support it. DON'T use this as a
105 * general "bucket" for results. Prefer a stronger typed return
106 * value if one is available, or write a custom struct. Don't use
107 * this if the list could potentially be very long, since it is
108 * inefficient. Keys should be unique. NULLs are not permitted.
110 | RHashtable of string
112 (* "RBufferOut" is handled almost exactly like RString, but
113 * it allows the string to contain arbitrary 8 bit data including
114 * ASCII NUL. In the C API this causes an implicit extra parameter
115 * to be added of type <size_t *size_r>. The extra parameter
116 * returns the actual size of the return buffer in bytes.
118 * Other programming languages support strings with arbitrary 8 bit
121 * At the RPC layer we have to use the opaque<> type instead of
122 * string<>. Returned data is still limited to the max message
125 | RBufferOut of string
127 and args = argt list (* Function parameters, guestfs handle is implicit. *)
129 (* Note in future we should allow a "variable args" parameter as
130 * the final parameter, to allow commands like
131 * chmod mode file [file(s)...]
132 * This is not implemented yet, but many commands (such as chmod)
133 * are currently defined with the argument order keeping this future
134 * possibility in mind.
137 | String of string (* const char *name, cannot be NULL *)
138 | Device of string (* /dev device name, cannot be NULL *)
139 | Pathname of string (* file name, cannot be NULL *)
140 | Dev_or_Path of string (* /dev device name or Pathname, cannot be NULL *)
141 | OptString of string (* const char *name, may be NULL *)
142 | StringList of string(* list of strings (each string cannot be NULL) *)
143 | DeviceList of string(* list of Device names (each cannot be NULL) *)
144 | Bool of string (* boolean *)
145 | Int of string (* int (smallish ints, signed, <= 31 bits) *)
146 | Int64 of string (* any 64 bit int *)
147 (* These are treated as filenames (simple string parameters) in
148 * the C API and bindings. But in the RPC protocol, we transfer
149 * the actual file content up to or down from the daemon.
150 * FileIn: local machine -> daemon (in request)
151 * FileOut: daemon -> local machine (in reply)
152 * In guestfish (only), the special name "-" means read from
153 * stdin or write to stdout.
158 (* Opaque buffer which can contain arbitrary 8 bit data.
159 * In the C API, this is expressed as <char *, int> pair.
160 * Most other languages have a string type which can contain
161 * ASCII NUL. We use whatever type is appropriate for each
163 * Buffers are limited by the total message size. To transfer
164 * large blocks of data, use FileIn/FileOut parameters instead.
165 * To return an arbitrary buffer, use RBufferOut.
171 | ProtocolLimitWarning (* display warning about protocol size limits *)
172 | DangerWillRobinson (* flags particularly dangerous commands *)
173 | FishAlias of string (* provide an alias for this cmd in guestfish *)
174 | FishAction of string (* call this function in guestfish *)
175 | NotInFish (* do not export via guestfish *)
176 | NotInDocs (* do not add this function to documentation *)
177 | DeprecatedBy of string (* function is deprecated, use .. instead *)
179 (* You can supply zero or as many tests as you want per API call.
181 * Note that the test environment has 3 block devices, of size 500MB,
182 * 50MB and 10MB (respectively /dev/sda, /dev/sdb, /dev/sdc), and
183 * a fourth ISO block device with some known files on it (/dev/sdd).
185 * Note for partitioning purposes, the 500MB device has 1015 cylinders.
186 * Number of cylinders was 63 for IDE emulated disks with precisely
187 * the same size. How exactly this is calculated is a mystery.
189 * The ISO block device (/dev/sdd) comes from images/test.iso.
191 * To be able to run the tests in a reasonable amount of time,
192 * the virtual machine and block devices are reused between tests.
193 * So don't try testing kill_subprocess :-x
195 * Between each test we blockdev-setrw, umount-all, lvm-remove-all.
197 * Don't assume anything about the previous contents of the block
198 * devices. Use 'Init*' to create some initial scenarios.
200 * You can add a prerequisite clause to any individual test. This
201 * is a run-time check, which, if it fails, causes the test to be
202 * skipped. Useful if testing a command which might not work on
203 * all variations of libguestfs builds. A test that has prerequisite
204 * of 'Always' is run unconditionally.
206 * In addition, packagers can skip individual tests by setting the
207 * environment variables: eg:
208 * SKIP_TEST_<CMD>_<NUM>=1 SKIP_TEST_COMMAND_3=1 (skips test #3 of command)
209 * SKIP_TEST_<CMD>=1 SKIP_TEST_ZEROFREE=1 (skips all zerofree tests)
211 type tests = (test_init * test_prereq * test) list
213 (* Run the command sequence and just expect nothing to fail. *)
216 (* Run the command sequence and expect the output of the final
217 * command to be the string.
219 | TestOutput of seq * string
221 (* Run the command sequence and expect the output of the final
222 * command to be the list of strings.
224 | TestOutputList of seq * string list
226 (* Run the command sequence and expect the output of the final
227 * command to be the list of block devices (could be either
228 * "/dev/sd.." or "/dev/hd.." form - we don't check the 5th
229 * character of each string).
231 | TestOutputListOfDevices of seq * string list
233 (* Run the command sequence and expect the output of the final
234 * command to be the integer.
236 | TestOutputInt of seq * int
238 (* Run the command sequence and expect the output of the final
239 * command to be <op> <int>, eg. ">=", "1".
241 | TestOutputIntOp of seq * string * int
243 (* Run the command sequence and expect the output of the final
244 * command to be a true value (!= 0 or != NULL).
246 | TestOutputTrue of seq
248 (* Run the command sequence and expect the output of the final
249 * command to be a false value (== 0 or == NULL, but not an error).
251 | TestOutputFalse of seq
253 (* Run the command sequence and expect the output of the final
254 * command to be a list of the given length (but don't care about
257 | TestOutputLength of seq * int
259 (* Run the command sequence and expect the output of the final
260 * command to be a buffer (RBufferOut), ie. string + size.
262 | TestOutputBuffer of seq * string
264 (* Run the command sequence and expect the output of the final
265 * command to be a structure.
267 | TestOutputStruct of seq * test_field_compare list
269 (* Run the command sequence and expect the final command (only)
272 | TestLastFail of seq
274 and test_field_compare =
275 | CompareWithInt of string * int
276 | CompareWithIntOp of string * string * int
277 | CompareWithString of string * string
278 | CompareFieldsIntEq of string * string
279 | CompareFieldsStrEq of string * string
281 (* Test prerequisites. *)
283 (* Test always runs. *)
286 (* Test is currently disabled - eg. it fails, or it tests some
287 * unimplemented feature.
291 (* 'string' is some C code (a function body) that should return
292 * true or false. The test will run if the code returns true.
296 (* As for 'If' but the test runs _unless_ the code returns true. *)
299 (* Some initial scenarios for testing. *)
301 (* Do nothing, block devices could contain random stuff including
302 * LVM PVs, and some filesystems might be mounted. This is usually
307 (* Block devices are empty and no filesystems are mounted. *)
310 (* /dev/sda contains a single partition /dev/sda1, with random
311 * content. /dev/sdb and /dev/sdc may have random content.
316 (* /dev/sda contains a single partition /dev/sda1, which is formatted
317 * as ext2, empty [except for lost+found] and mounted on /.
318 * /dev/sdb and /dev/sdc may have random content.
324 * /dev/sda1 (is a PV):
325 * /dev/VG/LV (size 8MB):
326 * formatted as ext2, empty [except for lost+found], mounted on /
327 * /dev/sdb and /dev/sdc may have random content.
331 (* /dev/sdd (the ISO, see images/ directory in source)
336 (* Sequence of commands for testing. *)
338 and cmd = string list
340 (* Note about long descriptions: When referring to another
341 * action, use the format C<guestfs_other> (ie. the full name of
342 * the C function). This will be replaced as appropriate in other
345 * Apart from that, long descriptions are just perldoc paragraphs.
348 (* Generate a random UUID (used in tests). *)
350 let chan = open_process_in "uuidgen" in
351 let uuid = input_line chan in
352 (match close_process_in chan with
355 failwith "uuidgen: process exited with non-zero status"
356 | WSIGNALED _ | WSTOPPED _ ->
357 failwith "uuidgen: process signalled or stopped by signal"
361 (* These test functions are used in the language binding tests. *)
363 let test_all_args = [
366 StringList "strlist";
374 let test_all_rets = [
375 (* except for RErr, which is tested thoroughly elsewhere *)
376 "test0rint", RInt "valout";
377 "test0rint64", RInt64 "valout";
378 "test0rbool", RBool "valout";
379 "test0rconststring", RConstString "valout";
380 "test0rconstoptstring", RConstOptString "valout";
381 "test0rstring", RString "valout";
382 "test0rstringlist", RStringList "valout";
383 "test0rstruct", RStruct ("valout", "lvm_pv");
384 "test0rstructlist", RStructList ("valout", "lvm_pv");
385 "test0rhashtable", RHashtable "valout";
388 let test_functions = [
389 ("test0", (RErr, test_all_args), -1, [NotInFish; NotInDocs],
391 "internal test function - do not use",
393 This is an internal test function which is used to test whether
394 the automatically generated bindings can handle every possible
395 parameter type correctly.
397 It echos the contents of each parameter to stdout.
399 You probably don't want to call this function.");
403 [(name, (ret, [String "val"]), -1, [NotInFish; NotInDocs],
405 "internal test function - do not use",
407 This is an internal test function which is used to test whether
408 the automatically generated bindings can handle every possible
409 return type correctly.
411 It converts string C<val> to the return type.
413 You probably don't want to call this function.");
414 (name ^ "err", (ret, []), -1, [NotInFish; NotInDocs],
416 "internal test function - do not use",
418 This is an internal test function which is used to test whether
419 the automatically generated bindings can handle every possible
420 return type correctly.
422 This function always returns an error.
424 You probably don't want to call this function.")]
428 (* non_daemon_functions are any functions which don't get processed
429 * in the daemon, eg. functions for setting and getting local
430 * configuration values.
433 let non_daemon_functions = test_functions @ [
434 ("launch", (RErr, []), -1, [FishAlias "run"; FishAction "launch"],
436 "launch the qemu subprocess",
438 Internally libguestfs is implemented by running a virtual machine
441 You should call this after configuring the handle
442 (eg. adding drives) but before performing any actions.");
444 ("wait_ready", (RErr, []), -1, [NotInFish],
446 "wait until the qemu subprocess launches (no op)",
448 This function is a no op.
450 In versions of the API E<lt> 1.0.71 you had to call this function
451 just after calling C<guestfs_launch> to wait for the launch
452 to complete. However this is no longer necessary because
453 C<guestfs_launch> now does the waiting.
455 If you see any calls to this function in code then you can just
456 remove them, unless you want to retain compatibility with older
457 versions of the API.");
459 ("kill_subprocess", (RErr, []), -1, [],
461 "kill the qemu subprocess",
463 This kills the qemu subprocess. You should never need to call this.");
465 ("add_drive", (RErr, [String "filename"]), -1, [FishAlias "add"],
467 "add an image to examine or modify",
469 This function adds a virtual machine disk image C<filename> to the
470 guest. The first time you call this function, the disk appears as IDE
471 disk 0 (C</dev/sda>) in the guest, the second time as C</dev/sdb>, and
474 You don't necessarily need to be root when using libguestfs. However
475 you obviously do need sufficient permissions to access the filename
476 for whatever operations you want to perform (ie. read access if you
477 just want to read the image or write access if you want to modify the
480 This is equivalent to the qemu parameter
481 C<-drive file=filename,cache=off,if=...>.
482 C<cache=off> is omitted in cases where it is not supported by
483 the underlying filesystem.
485 Note that this call checks for the existence of C<filename>. This
486 stops you from specifying other types of drive which are supported
487 by qemu such as C<nbd:> and C<http:> URLs. To specify those, use
488 the general C<guestfs_config> call instead.");
490 ("add_cdrom", (RErr, [String "filename"]), -1, [FishAlias "cdrom"],
492 "add a CD-ROM disk image to examine",
494 This function adds a virtual CD-ROM disk image to the guest.
496 This is equivalent to the qemu parameter C<-cdrom filename>.
498 Note that this call checks for the existence of C<filename>. This
499 stops you from specifying other types of drive which are supported
500 by qemu such as C<nbd:> and C<http:> URLs. To specify those, use
501 the general C<guestfs_config> call instead.");
503 ("add_drive_ro", (RErr, [String "filename"]), -1, [FishAlias "add-ro"],
505 "add a drive in snapshot mode (read-only)",
507 This adds a drive in snapshot mode, making it effectively
510 Note that writes to the device are allowed, and will be seen for
511 the duration of the guestfs handle, but they are written
512 to a temporary file which is discarded as soon as the guestfs
513 handle is closed. We don't currently have any method to enable
514 changes to be committed, although qemu can support this.
516 This is equivalent to the qemu parameter
517 C<-drive file=filename,snapshot=on,if=...>.
519 Note that this call checks for the existence of C<filename>. This
520 stops you from specifying other types of drive which are supported
521 by qemu such as C<nbd:> and C<http:> URLs. To specify those, use
522 the general C<guestfs_config> call instead.");
524 ("config", (RErr, [String "qemuparam"; OptString "qemuvalue"]), -1, [],
526 "add qemu parameters",
528 This can be used to add arbitrary qemu command line parameters
529 of the form C<-param value>. Actually it's not quite arbitrary - we
530 prevent you from setting some parameters which would interfere with
531 parameters that we use.
533 The first character of C<param> string must be a C<-> (dash).
535 C<value> can be NULL.");
537 ("set_qemu", (RErr, [String "qemu"]), -1, [FishAlias "qemu"],
539 "set the qemu binary",
541 Set the qemu binary that we will use.
543 The default is chosen when the library was compiled by the
546 You can also override this by setting the C<LIBGUESTFS_QEMU>
547 environment variable.
549 Setting C<qemu> to C<NULL> restores the default qemu binary.");
551 ("get_qemu", (RConstString "qemu", []), -1, [],
552 [InitNone, Always, TestRun (
554 "get the qemu binary",
556 Return the current qemu binary.
558 This is always non-NULL. If it wasn't set already, then this will
559 return the default qemu binary name.");
561 ("set_path", (RErr, [String "searchpath"]), -1, [FishAlias "path"],
563 "set the search path",
565 Set the path that libguestfs searches for kernel and initrd.img.
567 The default is C<$libdir/guestfs> unless overridden by setting
568 C<LIBGUESTFS_PATH> environment variable.
570 Setting C<path> to C<NULL> restores the default path.");
572 ("get_path", (RConstString "path", []), -1, [],
573 [InitNone, Always, TestRun (
575 "get the search path",
577 Return the current search path.
579 This is always non-NULL. If it wasn't set already, then this will
580 return the default path.");
582 ("set_append", (RErr, [OptString "append"]), -1, [FishAlias "append"],
584 "add options to kernel command line",
586 This function is used to add additional options to the
587 guest kernel command line.
589 The default is C<NULL> unless overridden by setting
590 C<LIBGUESTFS_APPEND> environment variable.
592 Setting C<append> to C<NULL> means I<no> additional options
593 are passed (libguestfs always adds a few of its own).");
595 ("get_append", (RConstOptString "append", []), -1, [],
596 (* This cannot be tested with the current framework. The
597 * function can return NULL in normal operations, which the
598 * test framework interprets as an error.
601 "get the additional kernel options",
603 Return the additional kernel options which are added to the
604 guest kernel command line.
606 If C<NULL> then no options are added.");
608 ("set_autosync", (RErr, [Bool "autosync"]), -1, [FishAlias "autosync"],
612 If C<autosync> is true, this enables autosync. Libguestfs will make a
613 best effort attempt to run C<guestfs_umount_all> followed by
614 C<guestfs_sync> when the handle is closed
615 (also if the program exits without closing handles).
617 This is disabled by default (except in guestfish where it is
618 enabled by default).");
620 ("get_autosync", (RBool "autosync", []), -1, [],
621 [InitNone, Always, TestRun (
622 [["get_autosync"]])],
625 Get the autosync flag.");
627 ("set_verbose", (RErr, [Bool "verbose"]), -1, [FishAlias "verbose"],
631 If C<verbose> is true, this turns on verbose messages (to C<stderr>).
633 Verbose messages are disabled unless the environment variable
634 C<LIBGUESTFS_DEBUG> is defined and set to C<1>.");
636 ("get_verbose", (RBool "verbose", []), -1, [],
640 This returns the verbose messages flag.");
642 ("is_ready", (RBool "ready", []), -1, [],
643 [InitNone, Always, TestOutputTrue (
645 "is ready to accept commands",
647 This returns true iff this handle is ready to accept commands
648 (in the C<READY> state).
650 For more information on states, see L<guestfs(3)>.");
652 ("is_config", (RBool "config", []), -1, [],
653 [InitNone, Always, TestOutputFalse (
655 "is in configuration state",
657 This returns true iff this handle is being configured
658 (in the C<CONFIG> state).
660 For more information on states, see L<guestfs(3)>.");
662 ("is_launching", (RBool "launching", []), -1, [],
663 [InitNone, Always, TestOutputFalse (
664 [["is_launching"]])],
665 "is launching subprocess",
667 This returns true iff this handle is launching the subprocess
668 (in the C<LAUNCHING> state).
670 For more information on states, see L<guestfs(3)>.");
672 ("is_busy", (RBool "busy", []), -1, [],
673 [InitNone, Always, TestOutputFalse (
675 "is busy processing a command",
677 This returns true iff this handle is busy processing a command
678 (in the C<BUSY> state).
680 For more information on states, see L<guestfs(3)>.");
682 ("get_state", (RInt "state", []), -1, [],
684 "get the current state",
686 This returns the current state as an opaque integer. This is
687 only useful for printing debug and internal error messages.
689 For more information on states, see L<guestfs(3)>.");
691 ("set_memsize", (RErr, [Int "memsize"]), -1, [FishAlias "memsize"],
692 [InitNone, Always, TestOutputInt (
693 [["set_memsize"; "500"];
694 ["get_memsize"]], 500)],
695 "set memory allocated to the qemu subprocess",
697 This sets the memory size in megabytes allocated to the
698 qemu subprocess. This only has any effect if called before
701 You can also change this by setting the environment
702 variable C<LIBGUESTFS_MEMSIZE> before the handle is
705 For more information on the architecture of libguestfs,
706 see L<guestfs(3)>.");
708 ("get_memsize", (RInt "memsize", []), -1, [],
709 [InitNone, Always, TestOutputIntOp (
710 [["get_memsize"]], ">=", 256)],
711 "get memory allocated to the qemu subprocess",
713 This gets the memory size in megabytes allocated to the
716 If C<guestfs_set_memsize> was not called
717 on this handle, and if C<LIBGUESTFS_MEMSIZE> was not set,
718 then this returns the compiled-in default value for memsize.
720 For more information on the architecture of libguestfs,
721 see L<guestfs(3)>.");
723 ("get_pid", (RInt "pid", []), -1, [FishAlias "pid"],
724 [InitNone, Always, TestOutputIntOp (
725 [["get_pid"]], ">=", 1)],
726 "get PID of qemu subprocess",
728 Return the process ID of the qemu subprocess. If there is no
729 qemu subprocess, then this will return an error.
731 This is an internal call used for debugging and testing.");
733 ("version", (RStruct ("version", "version"), []), -1, [],
734 [InitNone, Always, TestOutputStruct (
735 [["version"]], [CompareWithInt ("major", 1)])],
736 "get the library version number",
738 Return the libguestfs version number that the program is linked
741 Note that because of dynamic linking this is not necessarily
742 the version of libguestfs that you compiled against. You can
743 compile the program, and then at runtime dynamically link
744 against a completely different C<libguestfs.so> library.
746 This call was added in version C<1.0.58>. In previous
747 versions of libguestfs there was no way to get the version
748 number. From C code you can use ELF weak linking tricks to find out if
749 this symbol exists (if it doesn't, then it's an earlier version).
751 The call returns a structure with four elements. The first
752 three (C<major>, C<minor> and C<release>) are numbers and
753 correspond to the usual version triplet. The fourth element
754 (C<extra>) is a string and is normally empty, but may be
755 used for distro-specific information.
757 To construct the original version string:
758 C<$major.$minor.$release$extra>
760 I<Note:> Don't use this call to test for availability
761 of features. Distro backports makes this unreliable.");
763 ("set_selinux", (RErr, [Bool "selinux"]), -1, [FishAlias "selinux"],
764 [InitNone, Always, TestOutputTrue (
765 [["set_selinux"; "true"];
767 "set SELinux enabled or disabled at appliance boot",
769 This sets the selinux flag that is passed to the appliance
770 at boot time. The default is C<selinux=0> (disabled).
772 Note that if SELinux is enabled, it is always in
773 Permissive mode (C<enforcing=0>).
775 For more information on the architecture of libguestfs,
776 see L<guestfs(3)>.");
778 ("get_selinux", (RBool "selinux", []), -1, [],
780 "get SELinux enabled flag",
782 This returns the current setting of the selinux flag which
783 is passed to the appliance at boot time. See C<guestfs_set_selinux>.
785 For more information on the architecture of libguestfs,
786 see L<guestfs(3)>.");
788 ("set_trace", (RErr, [Bool "trace"]), -1, [FishAlias "trace"],
789 [InitNone, Always, TestOutputFalse (
790 [["set_trace"; "false"];
792 "enable or disable command traces",
794 If the command trace flag is set to 1, then commands are
795 printed on stdout before they are executed in a format
796 which is very similar to the one used by guestfish. In
797 other words, you can run a program with this enabled, and
798 you will get out a script which you can feed to guestfish
799 to perform the same set of actions.
801 If you want to trace C API calls into libguestfs (and
802 other libraries) then possibly a better way is to use
803 the external ltrace(1) command.
805 Command traces are disabled unless the environment variable
806 C<LIBGUESTFS_TRACE> is defined and set to C<1>.");
808 ("get_trace", (RBool "trace", []), -1, [],
810 "get command trace enabled flag",
812 Return the command trace flag.");
814 ("set_direct", (RErr, [Bool "direct"]), -1, [FishAlias "direct"],
815 [InitNone, Always, TestOutputFalse (
816 [["set_direct"; "false"];
818 "enable or disable direct appliance mode",
820 If the direct appliance mode flag is enabled, then stdin and
821 stdout are passed directly through to the appliance once it
824 One consequence of this is that log messages aren't caught
825 by the library and handled by C<guestfs_set_log_message_callback>,
826 but go straight to stdout.
828 You probably don't want to use this unless you know what you
831 The default is disabled.");
833 ("get_direct", (RBool "direct", []), -1, [],
835 "get direct appliance mode flag",
837 Return the direct appliance mode flag.");
839 ("set_recovery_proc", (RErr, [Bool "recoveryproc"]), -1, [FishAlias "recovery-proc"],
840 [InitNone, Always, TestOutputTrue (
841 [["set_recovery_proc"; "true"];
842 ["get_recovery_proc"]])],
843 "enable or disable the recovery process",
845 If this is called with the parameter C<false> then
846 C<guestfs_launch> does not create a recovery process. The
847 purpose of the recovery process is to stop runaway qemu
848 processes in the case where the main program aborts abruptly.
850 This only has any effect if called before C<guestfs_launch>,
851 and the default is true.
853 About the only time when you would want to disable this is
854 if the main process will fork itself into the background
855 (\"daemonize\" itself). In this case the recovery process
856 thinks that the main program has disappeared and so kills
857 qemu, which is not very helpful.");
859 ("get_recovery_proc", (RBool "recoveryproc", []), -1, [],
861 "get recovery process enabled flag",
863 Return the recovery process enabled flag.");
867 (* daemon_functions are any functions which cause some action
868 * to take place in the daemon.
871 let daemon_functions = [
872 ("mount", (RErr, [Device "device"; String "mountpoint"]), 1, [],
873 [InitEmpty, Always, TestOutput (
874 [["part_disk"; "/dev/sda"; "mbr"];
875 ["mkfs"; "ext2"; "/dev/sda1"];
876 ["mount"; "/dev/sda1"; "/"];
877 ["write_file"; "/new"; "new file contents"; "0"];
878 ["cat"; "/new"]], "new file contents")],
879 "mount a guest disk at a position in the filesystem",
881 Mount a guest disk at a position in the filesystem. Block devices
882 are named C</dev/sda>, C</dev/sdb> and so on, as they were added to
883 the guest. If those block devices contain partitions, they will have
884 the usual names (eg. C</dev/sda1>). Also LVM C</dev/VG/LV>-style
887 The rules are the same as for L<mount(2)>: A filesystem must
888 first be mounted on C</> before others can be mounted. Other
889 filesystems can only be mounted on directories which already
892 The mounted filesystem is writable, if we have sufficient permissions
893 on the underlying device.
895 The filesystem options C<sync> and C<noatime> are set with this
896 call, in order to improve reliability.");
898 ("sync", (RErr, []), 2, [],
899 [ InitEmpty, Always, TestRun [["sync"]]],
900 "sync disks, writes are flushed through to the disk image",
902 This syncs the disk, so that any writes are flushed through to the
903 underlying disk image.
905 You should always call this if you have modified a disk image, before
906 closing the handle.");
908 ("touch", (RErr, [Pathname "path"]), 3, [],
909 [InitBasicFS, Always, TestOutputTrue (
911 ["exists"; "/new"]])],
912 "update file timestamps or create a new file",
914 Touch acts like the L<touch(1)> command. It can be used to
915 update the timestamps on a file, or, if the file does not exist,
916 to create a new zero-length file.");
918 ("cat", (RString "content", [Pathname "path"]), 4, [ProtocolLimitWarning],
919 [InitISOFS, Always, TestOutput (
920 [["cat"; "/known-2"]], "abcdef\n")],
921 "list the contents of a file",
923 Return the contents of the file named C<path>.
925 Note that this function cannot correctly handle binary files
926 (specifically, files containing C<\\0> character which is treated
927 as end of string). For those you need to use the C<guestfs_read_file>
928 or C<guestfs_download> functions which have a more complex interface.");
930 ("ll", (RString "listing", [Pathname "directory"]), 5, [],
931 [], (* XXX Tricky to test because it depends on the exact format
932 * of the 'ls -l' command, which changes between F10 and F11.
934 "list the files in a directory (long format)",
936 List the files in C<directory> (relative to the root directory,
937 there is no cwd) in the format of 'ls -la'.
939 This command is mostly useful for interactive sessions. It
940 is I<not> intended that you try to parse the output string.");
942 ("ls", (RStringList "listing", [Pathname "directory"]), 6, [],
943 [InitBasicFS, Always, TestOutputList (
946 ["touch"; "/newest"];
947 ["ls"; "/"]], ["lost+found"; "new"; "newer"; "newest"])],
948 "list the files in a directory",
950 List the files in C<directory> (relative to the root directory,
951 there is no cwd). The '.' and '..' entries are not returned, but
952 hidden files are shown.
954 This command is mostly useful for interactive sessions. Programs
955 should probably use C<guestfs_readdir> instead.");
957 ("list_devices", (RStringList "devices", []), 7, [],
958 [InitEmpty, Always, TestOutputListOfDevices (
959 [["list_devices"]], ["/dev/sda"; "/dev/sdb"; "/dev/sdc"; "/dev/sdd"])],
960 "list the block devices",
962 List all the block devices.
964 The full block device names are returned, eg. C</dev/sda>");
966 ("list_partitions", (RStringList "partitions", []), 8, [],
967 [InitBasicFS, Always, TestOutputListOfDevices (
968 [["list_partitions"]], ["/dev/sda1"]);
969 InitEmpty, Always, TestOutputListOfDevices (
970 [["sfdiskM"; "/dev/sda"; ",100 ,200 ,"];
971 ["list_partitions"]], ["/dev/sda1"; "/dev/sda2"; "/dev/sda3"])],
972 "list the partitions",
974 List all the partitions detected on all block devices.
976 The full partition device names are returned, eg. C</dev/sda1>
978 This does not return logical volumes. For that you will need to
979 call C<guestfs_lvs>.");
981 ("pvs", (RStringList "physvols", []), 9, [],
982 [InitBasicFSonLVM, Always, TestOutputListOfDevices (
983 [["pvs"]], ["/dev/sda1"]);
984 InitEmpty, Always, TestOutputListOfDevices (
985 [["sfdiskM"; "/dev/sda"; ",100 ,200 ,"];
986 ["pvcreate"; "/dev/sda1"];
987 ["pvcreate"; "/dev/sda2"];
988 ["pvcreate"; "/dev/sda3"];
989 ["pvs"]], ["/dev/sda1"; "/dev/sda2"; "/dev/sda3"])],
990 "list the LVM physical volumes (PVs)",
992 List all the physical volumes detected. This is the equivalent
993 of the L<pvs(8)> command.
995 This returns a list of just the device names that contain
996 PVs (eg. C</dev/sda2>).
998 See also C<guestfs_pvs_full>.");
1000 ("vgs", (RStringList "volgroups", []), 10, [],
1001 [InitBasicFSonLVM, Always, TestOutputList (
1003 InitEmpty, Always, TestOutputList (
1004 [["sfdiskM"; "/dev/sda"; ",100 ,200 ,"];
1005 ["pvcreate"; "/dev/sda1"];
1006 ["pvcreate"; "/dev/sda2"];
1007 ["pvcreate"; "/dev/sda3"];
1008 ["vgcreate"; "VG1"; "/dev/sda1 /dev/sda2"];
1009 ["vgcreate"; "VG2"; "/dev/sda3"];
1010 ["vgs"]], ["VG1"; "VG2"])],
1011 "list the LVM volume groups (VGs)",
1013 List all the volumes groups detected. This is the equivalent
1014 of the L<vgs(8)> command.
1016 This returns a list of just the volume group names that were
1017 detected (eg. C<VolGroup00>).
1019 See also C<guestfs_vgs_full>.");
1021 ("lvs", (RStringList "logvols", []), 11, [],
1022 [InitBasicFSonLVM, Always, TestOutputList (
1023 [["lvs"]], ["/dev/VG/LV"]);
1024 InitEmpty, Always, TestOutputList (
1025 [["sfdiskM"; "/dev/sda"; ",100 ,200 ,"];
1026 ["pvcreate"; "/dev/sda1"];
1027 ["pvcreate"; "/dev/sda2"];
1028 ["pvcreate"; "/dev/sda3"];
1029 ["vgcreate"; "VG1"; "/dev/sda1 /dev/sda2"];
1030 ["vgcreate"; "VG2"; "/dev/sda3"];
1031 ["lvcreate"; "LV1"; "VG1"; "50"];
1032 ["lvcreate"; "LV2"; "VG1"; "50"];
1033 ["lvcreate"; "LV3"; "VG2"; "50"];
1034 ["lvs"]], ["/dev/VG1/LV1"; "/dev/VG1/LV2"; "/dev/VG2/LV3"])],
1035 "list the LVM logical volumes (LVs)",
1037 List all the logical volumes detected. This is the equivalent
1038 of the L<lvs(8)> command.
1040 This returns a list of the logical volume device names
1041 (eg. C</dev/VolGroup00/LogVol00>).
1043 See also C<guestfs_lvs_full>.");
1045 ("pvs_full", (RStructList ("physvols", "lvm_pv"), []), 12, [],
1046 [], (* XXX how to test? *)
1047 "list the LVM physical volumes (PVs)",
1049 List all the physical volumes detected. This is the equivalent
1050 of the L<pvs(8)> command. The \"full\" version includes all fields.");
1052 ("vgs_full", (RStructList ("volgroups", "lvm_vg"), []), 13, [],
1053 [], (* XXX how to test? *)
1054 "list the LVM volume groups (VGs)",
1056 List all the volumes groups detected. This is the equivalent
1057 of the L<vgs(8)> command. The \"full\" version includes all fields.");
1059 ("lvs_full", (RStructList ("logvols", "lvm_lv"), []), 14, [],
1060 [], (* XXX how to test? *)
1061 "list the LVM logical volumes (LVs)",
1063 List all the logical volumes detected. This is the equivalent
1064 of the L<lvs(8)> command. The \"full\" version includes all fields.");
1066 ("read_lines", (RStringList "lines", [Pathname "path"]), 15, [],
1067 [InitISOFS, Always, TestOutputList (
1068 [["read_lines"; "/known-4"]], ["abc"; "def"; "ghi"]);
1069 InitISOFS, Always, TestOutputList (
1070 [["read_lines"; "/empty"]], [])],
1071 "read file as lines",
1073 Return the contents of the file named C<path>.
1075 The file contents are returned as a list of lines. Trailing
1076 C<LF> and C<CRLF> character sequences are I<not> returned.
1078 Note that this function cannot correctly handle binary files
1079 (specifically, files containing C<\\0> character which is treated
1080 as end of line). For those you need to use the C<guestfs_read_file>
1081 function which has a more complex interface.");
1083 ("aug_init", (RErr, [Pathname "root"; Int "flags"]), 16, [],
1084 [], (* XXX Augeas code needs tests. *)
1085 "create a new Augeas handle",
1087 Create a new Augeas handle for editing configuration files.
1088 If there was any previous Augeas handle associated with this
1089 guestfs session, then it is closed.
1091 You must call this before using any other C<guestfs_aug_*>
1094 C<root> is the filesystem root. C<root> must not be NULL,
1097 The flags are the same as the flags defined in
1098 E<lt>augeas.hE<gt>, the logical I<or> of the following
1103 =item C<AUG_SAVE_BACKUP> = 1
1105 Keep the original file with a C<.augsave> extension.
1107 =item C<AUG_SAVE_NEWFILE> = 2
1109 Save changes into a file with extension C<.augnew>, and
1110 do not overwrite original. Overrides C<AUG_SAVE_BACKUP>.
1112 =item C<AUG_TYPE_CHECK> = 4
1114 Typecheck lenses (can be expensive).
1116 =item C<AUG_NO_STDINC> = 8
1118 Do not use standard load path for modules.
1120 =item C<AUG_SAVE_NOOP> = 16
1122 Make save a no-op, just record what would have been changed.
1124 =item C<AUG_NO_LOAD> = 32
1126 Do not load the tree in C<guestfs_aug_init>.
1130 To close the handle, you can call C<guestfs_aug_close>.
1132 To find out more about Augeas, see L<http://augeas.net/>.");
1134 ("aug_close", (RErr, []), 26, [],
1135 [], (* XXX Augeas code needs tests. *)
1136 "close the current Augeas handle",
1138 Close the current Augeas handle and free up any resources
1139 used by it. After calling this, you have to call
1140 C<guestfs_aug_init> again before you can use any other
1141 Augeas functions.");
1143 ("aug_defvar", (RInt "nrnodes", [String "name"; OptString "expr"]), 17, [],
1144 [], (* XXX Augeas code needs tests. *)
1145 "define an Augeas variable",
1147 Defines an Augeas variable C<name> whose value is the result
1148 of evaluating C<expr>. If C<expr> is NULL, then C<name> is
1151 On success this returns the number of nodes in C<expr>, or
1152 C<0> if C<expr> evaluates to something which is not a nodeset.");
1154 ("aug_defnode", (RStruct ("nrnodescreated", "int_bool"), [String "name"; String "expr"; String "val"]), 18, [],
1155 [], (* XXX Augeas code needs tests. *)
1156 "define an Augeas node",
1158 Defines a variable C<name> whose value is the result of
1161 If C<expr> evaluates to an empty nodeset, a node is created,
1162 equivalent to calling C<guestfs_aug_set> C<expr>, C<value>.
1163 C<name> will be the nodeset containing that single node.
1165 On success this returns a pair containing the
1166 number of nodes in the nodeset, and a boolean flag
1167 if a node was created.");
1169 ("aug_get", (RString "val", [String "augpath"]), 19, [],
1170 [], (* XXX Augeas code needs tests. *)
1171 "look up the value of an Augeas path",
1173 Look up the value associated with C<path>. If C<path>
1174 matches exactly one node, the C<value> is returned.");
1176 ("aug_set", (RErr, [String "augpath"; String "val"]), 20, [],
1177 [], (* XXX Augeas code needs tests. *)
1178 "set Augeas path to value",
1180 Set the value associated with C<path> to C<value>.");
1182 ("aug_insert", (RErr, [String "augpath"; String "label"; Bool "before"]), 21, [],
1183 [], (* XXX Augeas code needs tests. *)
1184 "insert a sibling Augeas node",
1186 Create a new sibling C<label> for C<path>, inserting it into
1187 the tree before or after C<path> (depending on the boolean
1190 C<path> must match exactly one existing node in the tree, and
1191 C<label> must be a label, ie. not contain C</>, C<*> or end
1192 with a bracketed index C<[N]>.");
1194 ("aug_rm", (RInt "nrnodes", [String "augpath"]), 22, [],
1195 [], (* XXX Augeas code needs tests. *)
1196 "remove an Augeas path",
1198 Remove C<path> and all of its children.
1200 On success this returns the number of entries which were removed.");
1202 ("aug_mv", (RErr, [String "src"; String "dest"]), 23, [],
1203 [], (* XXX Augeas code needs tests. *)
1206 Move the node C<src> to C<dest>. C<src> must match exactly
1207 one node. C<dest> is overwritten if it exists.");
1209 ("aug_match", (RStringList "matches", [String "augpath"]), 24, [],
1210 [], (* XXX Augeas code needs tests. *)
1211 "return Augeas nodes which match augpath",
1213 Returns a list of paths which match the path expression C<path>.
1214 The returned paths are sufficiently qualified so that they match
1215 exactly one node in the current tree.");
1217 ("aug_save", (RErr, []), 25, [],
1218 [], (* XXX Augeas code needs tests. *)
1219 "write all pending Augeas changes to disk",
1221 This writes all pending changes to disk.
1223 The flags which were passed to C<guestfs_aug_init> affect exactly
1224 how files are saved.");
1226 ("aug_load", (RErr, []), 27, [],
1227 [], (* XXX Augeas code needs tests. *)
1228 "load files into the tree",
1230 Load files into the tree.
1232 See C<aug_load> in the Augeas documentation for the full gory
1235 ("aug_ls", (RStringList "matches", [String "augpath"]), 28, [],
1236 [], (* XXX Augeas code needs tests. *)
1237 "list Augeas nodes under augpath",
1239 This is just a shortcut for listing C<guestfs_aug_match>
1240 C<path/*> and sorting the resulting nodes into alphabetical order.");
1242 ("rm", (RErr, [Pathname "path"]), 29, [],
1243 [InitBasicFS, Always, TestRun
1246 InitBasicFS, Always, TestLastFail
1248 InitBasicFS, Always, TestLastFail
1253 Remove the single file C<path>.");
1255 ("rmdir", (RErr, [Pathname "path"]), 30, [],
1256 [InitBasicFS, Always, TestRun
1259 InitBasicFS, Always, TestLastFail
1260 [["rmdir"; "/new"]];
1261 InitBasicFS, Always, TestLastFail
1263 ["rmdir"; "/new"]]],
1264 "remove a directory",
1266 Remove the single directory C<path>.");
1268 ("rm_rf", (RErr, [Pathname "path"]), 31, [],
1269 [InitBasicFS, Always, TestOutputFalse
1271 ["mkdir"; "/new/foo"];
1272 ["touch"; "/new/foo/bar"];
1274 ["exists"; "/new"]]],
1275 "remove a file or directory recursively",
1277 Remove the file or directory C<path>, recursively removing the
1278 contents if its a directory. This is like the C<rm -rf> shell
1281 ("mkdir", (RErr, [Pathname "path"]), 32, [],
1282 [InitBasicFS, Always, TestOutputTrue
1284 ["is_dir"; "/new"]];
1285 InitBasicFS, Always, TestLastFail
1286 [["mkdir"; "/new/foo/bar"]]],
1287 "create a directory",
1289 Create a directory named C<path>.");
1291 ("mkdir_p", (RErr, [Pathname "path"]), 33, [],
1292 [InitBasicFS, Always, TestOutputTrue
1293 [["mkdir_p"; "/new/foo/bar"];
1294 ["is_dir"; "/new/foo/bar"]];
1295 InitBasicFS, Always, TestOutputTrue
1296 [["mkdir_p"; "/new/foo/bar"];
1297 ["is_dir"; "/new/foo"]];
1298 InitBasicFS, Always, TestOutputTrue
1299 [["mkdir_p"; "/new/foo/bar"];
1300 ["is_dir"; "/new"]];
1301 (* Regression tests for RHBZ#503133: *)
1302 InitBasicFS, Always, TestRun
1304 ["mkdir_p"; "/new"]];
1305 InitBasicFS, Always, TestLastFail
1307 ["mkdir_p"; "/new"]]],
1308 "create a directory and parents",
1310 Create a directory named C<path>, creating any parent directories
1311 as necessary. This is like the C<mkdir -p> shell command.");
1313 ("chmod", (RErr, [Int "mode"; Pathname "path"]), 34, [],
1314 [], (* XXX Need stat command to test *)
1317 Change the mode (permissions) of C<path> to C<mode>. Only
1318 numeric modes are supported.");
1320 ("chown", (RErr, [Int "owner"; Int "group"; Pathname "path"]), 35, [],
1321 [], (* XXX Need stat command to test *)
1322 "change file owner and group",
1324 Change the file owner to C<owner> and group to C<group>.
1326 Only numeric uid and gid are supported. If you want to use
1327 names, you will need to locate and parse the password file
1328 yourself (Augeas support makes this relatively easy).");
1330 ("exists", (RBool "existsflag", [Pathname "path"]), 36, [],
1331 [InitISOFS, Always, TestOutputTrue (
1332 [["exists"; "/empty"]]);
1333 InitISOFS, Always, TestOutputTrue (
1334 [["exists"; "/directory"]])],
1335 "test if file or directory exists",
1337 This returns C<true> if and only if there is a file, directory
1338 (or anything) with the given C<path> name.
1340 See also C<guestfs_is_file>, C<guestfs_is_dir>, C<guestfs_stat>.");
1342 ("is_file", (RBool "fileflag", [Pathname "path"]), 37, [],
1343 [InitISOFS, Always, TestOutputTrue (
1344 [["is_file"; "/known-1"]]);
1345 InitISOFS, Always, TestOutputFalse (
1346 [["is_file"; "/directory"]])],
1347 "test if file exists",
1349 This returns C<true> if and only if there is a file
1350 with the given C<path> name. Note that it returns false for
1351 other objects like directories.
1353 See also C<guestfs_stat>.");
1355 ("is_dir", (RBool "dirflag", [Pathname "path"]), 38, [],
1356 [InitISOFS, Always, TestOutputFalse (
1357 [["is_dir"; "/known-3"]]);
1358 InitISOFS, Always, TestOutputTrue (
1359 [["is_dir"; "/directory"]])],
1360 "test if file exists",
1362 This returns C<true> if and only if there is a directory
1363 with the given C<path> name. Note that it returns false for
1364 other objects like files.
1366 See also C<guestfs_stat>.");
1368 ("pvcreate", (RErr, [Device "device"]), 39, [],
1369 [InitEmpty, Always, TestOutputListOfDevices (
1370 [["sfdiskM"; "/dev/sda"; ",100 ,200 ,"];
1371 ["pvcreate"; "/dev/sda1"];
1372 ["pvcreate"; "/dev/sda2"];
1373 ["pvcreate"; "/dev/sda3"];
1374 ["pvs"]], ["/dev/sda1"; "/dev/sda2"; "/dev/sda3"])],
1375 "create an LVM physical volume",
1377 This creates an LVM physical volume on the named C<device>,
1378 where C<device> should usually be a partition name such
1381 ("vgcreate", (RErr, [String "volgroup"; DeviceList "physvols"]), 40, [],
1382 [InitEmpty, Always, TestOutputList (
1383 [["sfdiskM"; "/dev/sda"; ",100 ,200 ,"];
1384 ["pvcreate"; "/dev/sda1"];
1385 ["pvcreate"; "/dev/sda2"];
1386 ["pvcreate"; "/dev/sda3"];
1387 ["vgcreate"; "VG1"; "/dev/sda1 /dev/sda2"];
1388 ["vgcreate"; "VG2"; "/dev/sda3"];
1389 ["vgs"]], ["VG1"; "VG2"])],
1390 "create an LVM volume group",
1392 This creates an LVM volume group called C<volgroup>
1393 from the non-empty list of physical volumes C<physvols>.");
1395 ("lvcreate", (RErr, [String "logvol"; String "volgroup"; Int "mbytes"]), 41, [],
1396 [InitEmpty, Always, TestOutputList (
1397 [["sfdiskM"; "/dev/sda"; ",100 ,200 ,"];
1398 ["pvcreate"; "/dev/sda1"];
1399 ["pvcreate"; "/dev/sda2"];
1400 ["pvcreate"; "/dev/sda3"];
1401 ["vgcreate"; "VG1"; "/dev/sda1 /dev/sda2"];
1402 ["vgcreate"; "VG2"; "/dev/sda3"];
1403 ["lvcreate"; "LV1"; "VG1"; "50"];
1404 ["lvcreate"; "LV2"; "VG1"; "50"];
1405 ["lvcreate"; "LV3"; "VG2"; "50"];
1406 ["lvcreate"; "LV4"; "VG2"; "50"];
1407 ["lvcreate"; "LV5"; "VG2"; "50"];
1409 ["/dev/VG1/LV1"; "/dev/VG1/LV2";
1410 "/dev/VG2/LV3"; "/dev/VG2/LV4"; "/dev/VG2/LV5"])],
1411 "create an LVM volume group",
1413 This creates an LVM volume group called C<logvol>
1414 on the volume group C<volgroup>, with C<size> megabytes.");
1416 ("mkfs", (RErr, [String "fstype"; Device "device"]), 42, [],
1417 [InitEmpty, Always, TestOutput (
1418 [["part_disk"; "/dev/sda"; "mbr"];
1419 ["mkfs"; "ext2"; "/dev/sda1"];
1420 ["mount"; "/dev/sda1"; "/"];
1421 ["write_file"; "/new"; "new file contents"; "0"];
1422 ["cat"; "/new"]], "new file contents")],
1423 "make a filesystem",
1425 This creates a filesystem on C<device> (usually a partition
1426 or LVM logical volume). The filesystem type is C<fstype>, for
1429 ("sfdisk", (RErr, [Device "device";
1430 Int "cyls"; Int "heads"; Int "sectors";
1431 StringList "lines"]), 43, [DangerWillRobinson],
1433 "create partitions on a block device",
1435 This is a direct interface to the L<sfdisk(8)> program for creating
1436 partitions on block devices.
1438 C<device> should be a block device, for example C</dev/sda>.
1440 C<cyls>, C<heads> and C<sectors> are the number of cylinders, heads
1441 and sectors on the device, which are passed directly to sfdisk as
1442 the I<-C>, I<-H> and I<-S> parameters. If you pass C<0> for any
1443 of these, then the corresponding parameter is omitted. Usually for
1444 'large' disks, you can just pass C<0> for these, but for small
1445 (floppy-sized) disks, sfdisk (or rather, the kernel) cannot work
1446 out the right geometry and you will need to tell it.
1448 C<lines> is a list of lines that we feed to C<sfdisk>. For more
1449 information refer to the L<sfdisk(8)> manpage.
1451 To create a single partition occupying the whole disk, you would
1452 pass C<lines> as a single element list, when the single element being
1453 the string C<,> (comma).
1455 See also: C<guestfs_sfdisk_l>, C<guestfs_sfdisk_N>,
1456 C<guestfs_part_init>");
1458 ("write_file", (RErr, [Pathname "path"; String "content"; Int "size"]), 44, [ProtocolLimitWarning],
1459 [InitBasicFS, Always, TestOutput (
1460 [["write_file"; "/new"; "new file contents"; "0"];
1461 ["cat"; "/new"]], "new file contents");
1462 InitBasicFS, Always, TestOutput (
1463 [["write_file"; "/new"; "\nnew file contents\n"; "0"];
1464 ["cat"; "/new"]], "\nnew file contents\n");
1465 InitBasicFS, Always, TestOutput (
1466 [["write_file"; "/new"; "\n\n"; "0"];
1467 ["cat"; "/new"]], "\n\n");
1468 InitBasicFS, Always, TestOutput (
1469 [["write_file"; "/new"; ""; "0"];
1470 ["cat"; "/new"]], "");
1471 InitBasicFS, Always, TestOutput (
1472 [["write_file"; "/new"; "\n\n\n"; "0"];
1473 ["cat"; "/new"]], "\n\n\n");
1474 InitBasicFS, Always, TestOutput (
1475 [["write_file"; "/new"; "\n"; "0"];
1476 ["cat"; "/new"]], "\n")],
1479 This call creates a file called C<path>. The contents of the
1480 file is the string C<content> (which can contain any 8 bit data),
1481 with length C<size>.
1483 As a special case, if C<size> is C<0>
1484 then the length is calculated using C<strlen> (so in this case
1485 the content cannot contain embedded ASCII NULs).
1487 I<NB.> Owing to a bug, writing content containing ASCII NUL
1488 characters does I<not> work, even if the length is specified.
1489 We hope to resolve this bug in a future version. In the meantime
1490 use C<guestfs_upload>.");
1492 ("umount", (RErr, [String "pathordevice"]), 45, [FishAlias "unmount"],
1493 [InitEmpty, Always, TestOutputListOfDevices (
1494 [["part_disk"; "/dev/sda"; "mbr"];
1495 ["mkfs"; "ext2"; "/dev/sda1"];
1496 ["mount"; "/dev/sda1"; "/"];
1497 ["mounts"]], ["/dev/sda1"]);
1498 InitEmpty, Always, TestOutputList (
1499 [["part_disk"; "/dev/sda"; "mbr"];
1500 ["mkfs"; "ext2"; "/dev/sda1"];
1501 ["mount"; "/dev/sda1"; "/"];
1504 "unmount a filesystem",
1506 This unmounts the given filesystem. The filesystem may be
1507 specified either by its mountpoint (path) or the device which
1508 contains the filesystem.");
1510 ("mounts", (RStringList "devices", []), 46, [],
1511 [InitBasicFS, Always, TestOutputListOfDevices (
1512 [["mounts"]], ["/dev/sda1"])],
1513 "show mounted filesystems",
1515 This returns the list of currently mounted filesystems. It returns
1516 the list of devices (eg. C</dev/sda1>, C</dev/VG/LV>).
1518 Some internal mounts are not shown.
1520 See also: C<guestfs_mountpoints>");
1522 ("umount_all", (RErr, []), 47, [FishAlias "unmount-all"],
1523 [InitBasicFS, Always, TestOutputList (
1526 (* check that umount_all can unmount nested mounts correctly: *)
1527 InitEmpty, Always, TestOutputList (
1528 [["sfdiskM"; "/dev/sda"; ",100 ,200 ,"];
1529 ["mkfs"; "ext2"; "/dev/sda1"];
1530 ["mkfs"; "ext2"; "/dev/sda2"];
1531 ["mkfs"; "ext2"; "/dev/sda3"];
1532 ["mount"; "/dev/sda1"; "/"];
1534 ["mount"; "/dev/sda2"; "/mp1"];
1535 ["mkdir"; "/mp1/mp2"];
1536 ["mount"; "/dev/sda3"; "/mp1/mp2"];
1537 ["mkdir"; "/mp1/mp2/mp3"];
1540 "unmount all filesystems",
1542 This unmounts all mounted filesystems.
1544 Some internal mounts are not unmounted by this call.");
1546 ("lvm_remove_all", (RErr, []), 48, [DangerWillRobinson],
1548 "remove all LVM LVs, VGs and PVs",
1550 This command removes all LVM logical volumes, volume groups
1551 and physical volumes.");
1553 ("file", (RString "description", [Dev_or_Path "path"]), 49, [],
1554 [InitISOFS, Always, TestOutput (
1555 [["file"; "/empty"]], "empty");
1556 InitISOFS, Always, TestOutput (
1557 [["file"; "/known-1"]], "ASCII text");
1558 InitISOFS, Always, TestLastFail (
1559 [["file"; "/notexists"]])],
1560 "determine file type",
1562 This call uses the standard L<file(1)> command to determine
1563 the type or contents of the file. This also works on devices,
1564 for example to find out whether a partition contains a filesystem.
1566 This call will also transparently look inside various types
1569 The exact command which runs is C<file -zbsL path>. Note in
1570 particular that the filename is not prepended to the output
1571 (the C<-b> option).");
1573 ("command", (RString "output", [StringList "arguments"]), 50, [ProtocolLimitWarning],
1574 [InitBasicFS, Always, TestOutput (
1575 [["upload"; "test-command"; "/test-command"];
1576 ["chmod"; "0o755"; "/test-command"];
1577 ["command"; "/test-command 1"]], "Result1");
1578 InitBasicFS, Always, TestOutput (
1579 [["upload"; "test-command"; "/test-command"];
1580 ["chmod"; "0o755"; "/test-command"];
1581 ["command"; "/test-command 2"]], "Result2\n");
1582 InitBasicFS, Always, TestOutput (
1583 [["upload"; "test-command"; "/test-command"];
1584 ["chmod"; "0o755"; "/test-command"];
1585 ["command"; "/test-command 3"]], "\nResult3");
1586 InitBasicFS, Always, TestOutput (
1587 [["upload"; "test-command"; "/test-command"];
1588 ["chmod"; "0o755"; "/test-command"];
1589 ["command"; "/test-command 4"]], "\nResult4\n");
1590 InitBasicFS, Always, TestOutput (
1591 [["upload"; "test-command"; "/test-command"];
1592 ["chmod"; "0o755"; "/test-command"];
1593 ["command"; "/test-command 5"]], "\nResult5\n\n");
1594 InitBasicFS, Always, TestOutput (
1595 [["upload"; "test-command"; "/test-command"];
1596 ["chmod"; "0o755"; "/test-command"];
1597 ["command"; "/test-command 6"]], "\n\nResult6\n\n");
1598 InitBasicFS, Always, TestOutput (
1599 [["upload"; "test-command"; "/test-command"];
1600 ["chmod"; "0o755"; "/test-command"];
1601 ["command"; "/test-command 7"]], "");
1602 InitBasicFS, Always, TestOutput (
1603 [["upload"; "test-command"; "/test-command"];
1604 ["chmod"; "0o755"; "/test-command"];
1605 ["command"; "/test-command 8"]], "\n");
1606 InitBasicFS, Always, TestOutput (
1607 [["upload"; "test-command"; "/test-command"];
1608 ["chmod"; "0o755"; "/test-command"];
1609 ["command"; "/test-command 9"]], "\n\n");
1610 InitBasicFS, Always, TestOutput (
1611 [["upload"; "test-command"; "/test-command"];
1612 ["chmod"; "0o755"; "/test-command"];
1613 ["command"; "/test-command 10"]], "Result10-1\nResult10-2\n");
1614 InitBasicFS, Always, TestOutput (
1615 [["upload"; "test-command"; "/test-command"];
1616 ["chmod"; "0o755"; "/test-command"];
1617 ["command"; "/test-command 11"]], "Result11-1\nResult11-2");
1618 InitBasicFS, Always, TestLastFail (
1619 [["upload"; "test-command"; "/test-command"];
1620 ["chmod"; "0o755"; "/test-command"];
1621 ["command"; "/test-command"]])],
1622 "run a command from the guest filesystem",
1624 This call runs a command from the guest filesystem. The
1625 filesystem must be mounted, and must contain a compatible
1626 operating system (ie. something Linux, with the same
1627 or compatible processor architecture).
1629 The single parameter is an argv-style list of arguments.
1630 The first element is the name of the program to run.
1631 Subsequent elements are parameters. The list must be
1632 non-empty (ie. must contain a program name). Note that
1633 the command runs directly, and is I<not> invoked via
1634 the shell (see C<guestfs_sh>).
1636 The return value is anything printed to I<stdout> by
1639 If the command returns a non-zero exit status, then
1640 this function returns an error message. The error message
1641 string is the content of I<stderr> from the command.
1643 The C<$PATH> environment variable will contain at least
1644 C</usr/bin> and C</bin>. If you require a program from
1645 another location, you should provide the full path in the
1648 Shared libraries and data files required by the program
1649 must be available on filesystems which are mounted in the
1650 correct places. It is the caller's responsibility to ensure
1651 all filesystems that are needed are mounted at the right
1654 ("command_lines", (RStringList "lines", [StringList "arguments"]), 51, [ProtocolLimitWarning],
1655 [InitBasicFS, Always, TestOutputList (
1656 [["upload"; "test-command"; "/test-command"];
1657 ["chmod"; "0o755"; "/test-command"];
1658 ["command_lines"; "/test-command 1"]], ["Result1"]);
1659 InitBasicFS, Always, TestOutputList (
1660 [["upload"; "test-command"; "/test-command"];
1661 ["chmod"; "0o755"; "/test-command"];
1662 ["command_lines"; "/test-command 2"]], ["Result2"]);
1663 InitBasicFS, Always, TestOutputList (
1664 [["upload"; "test-command"; "/test-command"];
1665 ["chmod"; "0o755"; "/test-command"];
1666 ["command_lines"; "/test-command 3"]], ["";"Result3"]);
1667 InitBasicFS, Always, TestOutputList (
1668 [["upload"; "test-command"; "/test-command"];
1669 ["chmod"; "0o755"; "/test-command"];
1670 ["command_lines"; "/test-command 4"]], ["";"Result4"]);
1671 InitBasicFS, Always, TestOutputList (
1672 [["upload"; "test-command"; "/test-command"];
1673 ["chmod"; "0o755"; "/test-command"];
1674 ["command_lines"; "/test-command 5"]], ["";"Result5";""]);
1675 InitBasicFS, Always, TestOutputList (
1676 [["upload"; "test-command"; "/test-command"];
1677 ["chmod"; "0o755"; "/test-command"];
1678 ["command_lines"; "/test-command 6"]], ["";"";"Result6";""]);
1679 InitBasicFS, Always, TestOutputList (
1680 [["upload"; "test-command"; "/test-command"];
1681 ["chmod"; "0o755"; "/test-command"];
1682 ["command_lines"; "/test-command 7"]], []);
1683 InitBasicFS, Always, TestOutputList (
1684 [["upload"; "test-command"; "/test-command"];
1685 ["chmod"; "0o755"; "/test-command"];
1686 ["command_lines"; "/test-command 8"]], [""]);
1687 InitBasicFS, Always, TestOutputList (
1688 [["upload"; "test-command"; "/test-command"];
1689 ["chmod"; "0o755"; "/test-command"];
1690 ["command_lines"; "/test-command 9"]], ["";""]);
1691 InitBasicFS, Always, TestOutputList (
1692 [["upload"; "test-command"; "/test-command"];
1693 ["chmod"; "0o755"; "/test-command"];
1694 ["command_lines"; "/test-command 10"]], ["Result10-1";"Result10-2"]);
1695 InitBasicFS, Always, TestOutputList (
1696 [["upload"; "test-command"; "/test-command"];
1697 ["chmod"; "0o755"; "/test-command"];
1698 ["command_lines"; "/test-command 11"]], ["Result11-1";"Result11-2"])],
1699 "run a command, returning lines",
1701 This is the same as C<guestfs_command>, but splits the
1702 result into a list of lines.
1704 See also: C<guestfs_sh_lines>");
1706 ("stat", (RStruct ("statbuf", "stat"), [Pathname "path"]), 52, [],
1707 [InitISOFS, Always, TestOutputStruct (
1708 [["stat"; "/empty"]], [CompareWithInt ("size", 0)])],
1709 "get file information",
1711 Returns file information for the given C<path>.
1713 This is the same as the C<stat(2)> system call.");
1715 ("lstat", (RStruct ("statbuf", "stat"), [Pathname "path"]), 53, [],
1716 [InitISOFS, Always, TestOutputStruct (
1717 [["lstat"; "/empty"]], [CompareWithInt ("size", 0)])],
1718 "get file information for a symbolic link",
1720 Returns file information for the given C<path>.
1722 This is the same as C<guestfs_stat> except that if C<path>
1723 is a symbolic link, then the link is stat-ed, not the file it
1726 This is the same as the C<lstat(2)> system call.");
1728 ("statvfs", (RStruct ("statbuf", "statvfs"), [Pathname "path"]), 54, [],
1729 [InitISOFS, Always, TestOutputStruct (
1730 [["statvfs"; "/"]], [CompareWithInt ("namemax", 255)])],
1731 "get file system statistics",
1733 Returns file system statistics for any mounted file system.
1734 C<path> should be a file or directory in the mounted file system
1735 (typically it is the mount point itself, but it doesn't need to be).
1737 This is the same as the C<statvfs(2)> system call.");
1739 ("tune2fs_l", (RHashtable "superblock", [Device "device"]), 55, [],
1741 "get ext2/ext3/ext4 superblock details",
1743 This returns the contents of the ext2, ext3 or ext4 filesystem
1744 superblock on C<device>.
1746 It is the same as running C<tune2fs -l device>. See L<tune2fs(8)>
1747 manpage for more details. The list of fields returned isn't
1748 clearly defined, and depends on both the version of C<tune2fs>
1749 that libguestfs was built against, and the filesystem itself.");
1751 ("blockdev_setro", (RErr, [Device "device"]), 56, [],
1752 [InitEmpty, Always, TestOutputTrue (
1753 [["blockdev_setro"; "/dev/sda"];
1754 ["blockdev_getro"; "/dev/sda"]])],
1755 "set block device to read-only",
1757 Sets the block device named C<device> to read-only.
1759 This uses the L<blockdev(8)> command.");
1761 ("blockdev_setrw", (RErr, [Device "device"]), 57, [],
1762 [InitEmpty, Always, TestOutputFalse (
1763 [["blockdev_setrw"; "/dev/sda"];
1764 ["blockdev_getro"; "/dev/sda"]])],
1765 "set block device to read-write",
1767 Sets the block device named C<device> to read-write.
1769 This uses the L<blockdev(8)> command.");
1771 ("blockdev_getro", (RBool "ro", [Device "device"]), 58, [],
1772 [InitEmpty, Always, TestOutputTrue (
1773 [["blockdev_setro"; "/dev/sda"];
1774 ["blockdev_getro"; "/dev/sda"]])],
1775 "is block device set to read-only",
1777 Returns a boolean indicating if the block device is read-only
1778 (true if read-only, false if not).
1780 This uses the L<blockdev(8)> command.");
1782 ("blockdev_getss", (RInt "sectorsize", [Device "device"]), 59, [],
1783 [InitEmpty, Always, TestOutputInt (
1784 [["blockdev_getss"; "/dev/sda"]], 512)],
1785 "get sectorsize of block device",
1787 This returns the size of sectors on a block device.
1788 Usually 512, but can be larger for modern devices.
1790 (Note, this is not the size in sectors, use C<guestfs_blockdev_getsz>
1793 This uses the L<blockdev(8)> command.");
1795 ("blockdev_getbsz", (RInt "blocksize", [Device "device"]), 60, [],
1796 [InitEmpty, Always, TestOutputInt (
1797 [["blockdev_getbsz"; "/dev/sda"]], 4096)],
1798 "get blocksize of block device",
1800 This returns the block size of a device.
1802 (Note this is different from both I<size in blocks> and
1803 I<filesystem block size>).
1805 This uses the L<blockdev(8)> command.");
1807 ("blockdev_setbsz", (RErr, [Device "device"; Int "blocksize"]), 61, [],
1809 "set blocksize of block device",
1811 This sets the block size of a device.
1813 (Note this is different from both I<size in blocks> and
1814 I<filesystem block size>).
1816 This uses the L<blockdev(8)> command.");
1818 ("blockdev_getsz", (RInt64 "sizeinsectors", [Device "device"]), 62, [],
1819 [InitEmpty, Always, TestOutputInt (
1820 [["blockdev_getsz"; "/dev/sda"]], 1024000)],
1821 "get total size of device in 512-byte sectors",
1823 This returns the size of the device in units of 512-byte sectors
1824 (even if the sectorsize isn't 512 bytes ... weird).
1826 See also C<guestfs_blockdev_getss> for the real sector size of
1827 the device, and C<guestfs_blockdev_getsize64> for the more
1828 useful I<size in bytes>.
1830 This uses the L<blockdev(8)> command.");
1832 ("blockdev_getsize64", (RInt64 "sizeinbytes", [Device "device"]), 63, [],
1833 [InitEmpty, Always, TestOutputInt (
1834 [["blockdev_getsize64"; "/dev/sda"]], 524288000)],
1835 "get total size of device in bytes",
1837 This returns the size of the device in bytes.
1839 See also C<guestfs_blockdev_getsz>.
1841 This uses the L<blockdev(8)> command.");
1843 ("blockdev_flushbufs", (RErr, [Device "device"]), 64, [],
1844 [InitEmpty, Always, TestRun
1845 [["blockdev_flushbufs"; "/dev/sda"]]],
1846 "flush device buffers",
1848 This tells the kernel to flush internal buffers associated
1851 This uses the L<blockdev(8)> command.");
1853 ("blockdev_rereadpt", (RErr, [Device "device"]), 65, [],
1854 [InitEmpty, Always, TestRun
1855 [["blockdev_rereadpt"; "/dev/sda"]]],
1856 "reread partition table",
1858 Reread the partition table on C<device>.
1860 This uses the L<blockdev(8)> command.");
1862 ("upload", (RErr, [FileIn "filename"; String "remotefilename"]), 66, [],
1863 [InitBasicFS, Always, TestOutput (
1864 (* Pick a file from cwd which isn't likely to change. *)
1865 [["upload"; "../COPYING.LIB"; "/COPYING.LIB"];
1866 ["checksum"; "md5"; "/COPYING.LIB"]],
1867 Digest.to_hex (Digest.file "COPYING.LIB"))],
1868 "upload a file from the local machine",
1870 Upload local file C<filename> to C<remotefilename> on the
1873 C<filename> can also be a named pipe.
1875 See also C<guestfs_download>.");
1877 ("download", (RErr, [Dev_or_Path "remotefilename"; FileOut "filename"]), 67, [],
1878 [InitBasicFS, Always, TestOutput (
1879 (* Pick a file from cwd which isn't likely to change. *)
1880 [["upload"; "../COPYING.LIB"; "/COPYING.LIB"];
1881 ["download"; "/COPYING.LIB"; "testdownload.tmp"];
1882 ["upload"; "testdownload.tmp"; "/upload"];
1883 ["checksum"; "md5"; "/upload"]],
1884 Digest.to_hex (Digest.file "COPYING.LIB"))],
1885 "download a file to the local machine",
1887 Download file C<remotefilename> and save it as C<filename>
1888 on the local machine.
1890 C<filename> can also be a named pipe.
1892 See also C<guestfs_upload>, C<guestfs_cat>.");
1894 ("checksum", (RString "checksum", [String "csumtype"; Pathname "path"]), 68, [],
1895 [InitISOFS, Always, TestOutput (
1896 [["checksum"; "crc"; "/known-3"]], "2891671662");
1897 InitISOFS, Always, TestLastFail (
1898 [["checksum"; "crc"; "/notexists"]]);
1899 InitISOFS, Always, TestOutput (
1900 [["checksum"; "md5"; "/known-3"]], "46d6ca27ee07cdc6fa99c2e138cc522c");
1901 InitISOFS, Always, TestOutput (
1902 [["checksum"; "sha1"; "/known-3"]], "b7ebccc3ee418311091c3eda0a45b83c0a770f15");
1903 InitISOFS, Always, TestOutput (
1904 [["checksum"; "sha224"; "/known-3"]], "d2cd1774b28f3659c14116be0a6dc2bb5c4b350ce9cd5defac707741");
1905 InitISOFS, Always, TestOutput (
1906 [["checksum"; "sha256"; "/known-3"]], "75bb71b90cd20cb13f86d2bea8dad63ac7194e7517c3b52b8d06ff52d3487d30");
1907 InitISOFS, Always, TestOutput (
1908 [["checksum"; "sha384"; "/known-3"]], "5fa7883430f357b5d7b7271d3a1d2872b51d73cba72731de6863d3dea55f30646af2799bef44d5ea776a5ec7941ac640");
1909 InitISOFS, Always, TestOutput (
1910 [["checksum"; "sha512"; "/known-3"]], "2794062c328c6b216dca90443b7f7134c5f40e56bd0ed7853123275a09982a6f992e6ca682f9d2fba34a4c5e870d8fe077694ff831e3032a004ee077e00603f6")],
1911 "compute MD5, SHAx or CRC checksum of file",
1913 This call computes the MD5, SHAx or CRC checksum of the
1916 The type of checksum to compute is given by the C<csumtype>
1917 parameter which must have one of the following values:
1923 Compute the cyclic redundancy check (CRC) specified by POSIX
1924 for the C<cksum> command.
1928 Compute the MD5 hash (using the C<md5sum> program).
1932 Compute the SHA1 hash (using the C<sha1sum> program).
1936 Compute the SHA224 hash (using the C<sha224sum> program).
1940 Compute the SHA256 hash (using the C<sha256sum> program).
1944 Compute the SHA384 hash (using the C<sha384sum> program).
1948 Compute the SHA512 hash (using the C<sha512sum> program).
1952 The checksum is returned as a printable string.");
1954 ("tar_in", (RErr, [FileIn "tarfile"; String "directory"]), 69, [],
1955 [InitBasicFS, Always, TestOutput (
1956 [["tar_in"; "../images/helloworld.tar"; "/"];
1957 ["cat"; "/hello"]], "hello\n")],
1958 "unpack tarfile to directory",
1960 This command uploads and unpacks local file C<tarfile> (an
1961 I<uncompressed> tar file) into C<directory>.
1963 To upload a compressed tarball, use C<guestfs_tgz_in>.");
1965 ("tar_out", (RErr, [String "directory"; FileOut "tarfile"]), 70, [],
1967 "pack directory into tarfile",
1969 This command packs the contents of C<directory> and downloads
1970 it to local file C<tarfile>.
1972 To download a compressed tarball, use C<guestfs_tgz_out>.");
1974 ("tgz_in", (RErr, [FileIn "tarball"; String "directory"]), 71, [],
1975 [InitBasicFS, Always, TestOutput (
1976 [["tgz_in"; "../images/helloworld.tar.gz"; "/"];
1977 ["cat"; "/hello"]], "hello\n")],
1978 "unpack compressed tarball to directory",
1980 This command uploads and unpacks local file C<tarball> (a
1981 I<gzip compressed> tar file) into C<directory>.
1983 To upload an uncompressed tarball, use C<guestfs_tar_in>.");
1985 ("tgz_out", (RErr, [Pathname "directory"; FileOut "tarball"]), 72, [],
1987 "pack directory into compressed tarball",
1989 This command packs the contents of C<directory> and downloads
1990 it to local file C<tarball>.
1992 To download an uncompressed tarball, use C<guestfs_tar_out>.");
1994 ("mount_ro", (RErr, [Device "device"; String "mountpoint"]), 73, [],
1995 [InitBasicFS, Always, TestLastFail (
1997 ["mount_ro"; "/dev/sda1"; "/"];
1998 ["touch"; "/new"]]);
1999 InitBasicFS, Always, TestOutput (
2000 [["write_file"; "/new"; "data"; "0"];
2002 ["mount_ro"; "/dev/sda1"; "/"];
2003 ["cat"; "/new"]], "data")],
2004 "mount a guest disk, read-only",
2006 This is the same as the C<guestfs_mount> command, but it
2007 mounts the filesystem with the read-only (I<-o ro>) flag.");
2009 ("mount_options", (RErr, [String "options"; Device "device"; String "mountpoint"]), 74, [],
2011 "mount a guest disk with mount options",
2013 This is the same as the C<guestfs_mount> command, but it
2014 allows you to set the mount options as for the
2015 L<mount(8)> I<-o> flag.");
2017 ("mount_vfs", (RErr, [String "options"; String "vfstype"; Device "device"; String "mountpoint"]), 75, [],
2019 "mount a guest disk with mount options and vfstype",
2021 This is the same as the C<guestfs_mount> command, but it
2022 allows you to set both the mount options and the vfstype
2023 as for the L<mount(8)> I<-o> and I<-t> flags.");
2025 ("debug", (RString "result", [String "subcmd"; StringList "extraargs"]), 76, [],
2027 "debugging and internals",
2029 The C<guestfs_debug> command exposes some internals of
2030 C<guestfsd> (the guestfs daemon) that runs inside the
2033 There is no comprehensive help for this command. You have
2034 to look at the file C<daemon/debug.c> in the libguestfs source
2035 to find out what you can do.");
2037 ("lvremove", (RErr, [Device "device"]), 77, [],
2038 [InitEmpty, Always, TestOutputList (
2039 [["part_disk"; "/dev/sda"; "mbr"];
2040 ["pvcreate"; "/dev/sda1"];
2041 ["vgcreate"; "VG"; "/dev/sda1"];
2042 ["lvcreate"; "LV1"; "VG"; "50"];
2043 ["lvcreate"; "LV2"; "VG"; "50"];
2044 ["lvremove"; "/dev/VG/LV1"];
2045 ["lvs"]], ["/dev/VG/LV2"]);
2046 InitEmpty, Always, TestOutputList (
2047 [["part_disk"; "/dev/sda"; "mbr"];
2048 ["pvcreate"; "/dev/sda1"];
2049 ["vgcreate"; "VG"; "/dev/sda1"];
2050 ["lvcreate"; "LV1"; "VG"; "50"];
2051 ["lvcreate"; "LV2"; "VG"; "50"];
2052 ["lvremove"; "/dev/VG"];
2054 InitEmpty, Always, TestOutputList (
2055 [["part_disk"; "/dev/sda"; "mbr"];
2056 ["pvcreate"; "/dev/sda1"];
2057 ["vgcreate"; "VG"; "/dev/sda1"];
2058 ["lvcreate"; "LV1"; "VG"; "50"];
2059 ["lvcreate"; "LV2"; "VG"; "50"];
2060 ["lvremove"; "/dev/VG"];
2062 "remove an LVM logical volume",
2064 Remove an LVM logical volume C<device>, where C<device> is
2065 the path to the LV, such as C</dev/VG/LV>.
2067 You can also remove all LVs in a volume group by specifying
2068 the VG name, C</dev/VG>.");
2070 ("vgremove", (RErr, [String "vgname"]), 78, [],
2071 [InitEmpty, Always, TestOutputList (
2072 [["part_disk"; "/dev/sda"; "mbr"];
2073 ["pvcreate"; "/dev/sda1"];
2074 ["vgcreate"; "VG"; "/dev/sda1"];
2075 ["lvcreate"; "LV1"; "VG"; "50"];
2076 ["lvcreate"; "LV2"; "VG"; "50"];
2079 InitEmpty, Always, TestOutputList (
2080 [["part_disk"; "/dev/sda"; "mbr"];
2081 ["pvcreate"; "/dev/sda1"];
2082 ["vgcreate"; "VG"; "/dev/sda1"];
2083 ["lvcreate"; "LV1"; "VG"; "50"];
2084 ["lvcreate"; "LV2"; "VG"; "50"];
2087 "remove an LVM volume group",
2089 Remove an LVM volume group C<vgname>, (for example C<VG>).
2091 This also forcibly removes all logical volumes in the volume
2094 ("pvremove", (RErr, [Device "device"]), 79, [],
2095 [InitEmpty, Always, TestOutputListOfDevices (
2096 [["part_disk"; "/dev/sda"; "mbr"];
2097 ["pvcreate"; "/dev/sda1"];
2098 ["vgcreate"; "VG"; "/dev/sda1"];
2099 ["lvcreate"; "LV1"; "VG"; "50"];
2100 ["lvcreate"; "LV2"; "VG"; "50"];
2102 ["pvremove"; "/dev/sda1"];
2104 InitEmpty, Always, TestOutputListOfDevices (
2105 [["part_disk"; "/dev/sda"; "mbr"];
2106 ["pvcreate"; "/dev/sda1"];
2107 ["vgcreate"; "VG"; "/dev/sda1"];
2108 ["lvcreate"; "LV1"; "VG"; "50"];
2109 ["lvcreate"; "LV2"; "VG"; "50"];
2111 ["pvremove"; "/dev/sda1"];
2113 InitEmpty, Always, TestOutputListOfDevices (
2114 [["part_disk"; "/dev/sda"; "mbr"];
2115 ["pvcreate"; "/dev/sda1"];
2116 ["vgcreate"; "VG"; "/dev/sda1"];
2117 ["lvcreate"; "LV1"; "VG"; "50"];
2118 ["lvcreate"; "LV2"; "VG"; "50"];
2120 ["pvremove"; "/dev/sda1"];
2122 "remove an LVM physical volume",
2124 This wipes a physical volume C<device> so that LVM will no longer
2127 The implementation uses the C<pvremove> command which refuses to
2128 wipe physical volumes that contain any volume groups, so you have
2129 to remove those first.");
2131 ("set_e2label", (RErr, [Device "device"; String "label"]), 80, [],
2132 [InitBasicFS, Always, TestOutput (
2133 [["set_e2label"; "/dev/sda1"; "testlabel"];
2134 ["get_e2label"; "/dev/sda1"]], "testlabel")],
2135 "set the ext2/3/4 filesystem label",
2137 This sets the ext2/3/4 filesystem label of the filesystem on
2138 C<device> to C<label>. Filesystem labels are limited to
2141 You can use either C<guestfs_tune2fs_l> or C<guestfs_get_e2label>
2142 to return the existing label on a filesystem.");
2144 ("get_e2label", (RString "label", [Device "device"]), 81, [],
2146 "get the ext2/3/4 filesystem label",
2148 This returns the ext2/3/4 filesystem label of the filesystem on
2151 ("set_e2uuid", (RErr, [Device "device"; String "uuid"]), 82, [],
2152 (let uuid = uuidgen () in
2153 [InitBasicFS, Always, TestOutput (
2154 [["set_e2uuid"; "/dev/sda1"; uuid];
2155 ["get_e2uuid"; "/dev/sda1"]], uuid);
2156 InitBasicFS, Always, TestOutput (
2157 [["set_e2uuid"; "/dev/sda1"; "clear"];
2158 ["get_e2uuid"; "/dev/sda1"]], "");
2159 (* We can't predict what UUIDs will be, so just check the commands run. *)
2160 InitBasicFS, Always, TestRun (
2161 [["set_e2uuid"; "/dev/sda1"; "random"]]);
2162 InitBasicFS, Always, TestRun (
2163 [["set_e2uuid"; "/dev/sda1"; "time"]])]),
2164 "set the ext2/3/4 filesystem UUID",
2166 This sets the ext2/3/4 filesystem UUID of the filesystem on
2167 C<device> to C<uuid>. The format of the UUID and alternatives
2168 such as C<clear>, C<random> and C<time> are described in the
2169 L<tune2fs(8)> manpage.
2171 You can use either C<guestfs_tune2fs_l> or C<guestfs_get_e2uuid>
2172 to return the existing UUID of a filesystem.");
2174 ("get_e2uuid", (RString "uuid", [Device "device"]), 83, [],
2176 "get the ext2/3/4 filesystem UUID",
2178 This returns the ext2/3/4 filesystem UUID of the filesystem on
2181 ("fsck", (RInt "status", [String "fstype"; Device "device"]), 84, [],
2182 [InitBasicFS, Always, TestOutputInt (
2183 [["umount"; "/dev/sda1"];
2184 ["fsck"; "ext2"; "/dev/sda1"]], 0);
2185 InitBasicFS, Always, TestOutputInt (
2186 [["umount"; "/dev/sda1"];
2187 ["zero"; "/dev/sda1"];
2188 ["fsck"; "ext2"; "/dev/sda1"]], 8)],
2189 "run the filesystem checker",
2191 This runs the filesystem checker (fsck) on C<device> which
2192 should have filesystem type C<fstype>.
2194 The returned integer is the status. See L<fsck(8)> for the
2195 list of status codes from C<fsck>.
2203 Multiple status codes can be summed together.
2207 A non-zero return code can mean \"success\", for example if
2208 errors have been corrected on the filesystem.
2212 Checking or repairing NTFS volumes is not supported
2217 This command is entirely equivalent to running C<fsck -a -t fstype device>.");
2219 ("zero", (RErr, [Device "device"]), 85, [],
2220 [InitBasicFS, Always, TestOutput (
2221 [["umount"; "/dev/sda1"];
2222 ["zero"; "/dev/sda1"];
2223 ["file"; "/dev/sda1"]], "data")],
2224 "write zeroes to the device",
2226 This command writes zeroes over the first few blocks of C<device>.
2228 How many blocks are zeroed isn't specified (but it's I<not> enough
2229 to securely wipe the device). It should be sufficient to remove
2230 any partition tables, filesystem superblocks and so on.
2232 See also: C<guestfs_scrub_device>.");
2234 ("grub_install", (RErr, [Pathname "root"; Device "device"]), 86, [],
2235 (* Test disabled because grub-install incompatible with virtio-blk driver.
2236 * See also: https://bugzilla.redhat.com/show_bug.cgi?id=479760
2238 [InitBasicFS, Disabled, TestOutputTrue (
2239 [["grub_install"; "/"; "/dev/sda1"];
2240 ["is_dir"; "/boot"]])],
2243 This command installs GRUB (the Grand Unified Bootloader) on
2244 C<device>, with the root directory being C<root>.");
2246 ("cp", (RErr, [Pathname "src"; Pathname "dest"]), 87, [],
2247 [InitBasicFS, Always, TestOutput (
2248 [["write_file"; "/old"; "file content"; "0"];
2249 ["cp"; "/old"; "/new"];
2250 ["cat"; "/new"]], "file content");
2251 InitBasicFS, Always, TestOutputTrue (
2252 [["write_file"; "/old"; "file content"; "0"];
2253 ["cp"; "/old"; "/new"];
2254 ["is_file"; "/old"]]);
2255 InitBasicFS, Always, TestOutput (
2256 [["write_file"; "/old"; "file content"; "0"];
2258 ["cp"; "/old"; "/dir/new"];
2259 ["cat"; "/dir/new"]], "file content")],
2262 This copies a file from C<src> to C<dest> where C<dest> is
2263 either a destination filename or destination directory.");
2265 ("cp_a", (RErr, [Pathname "src"; Pathname "dest"]), 88, [],
2266 [InitBasicFS, Always, TestOutput (
2267 [["mkdir"; "/olddir"];
2268 ["mkdir"; "/newdir"];
2269 ["write_file"; "/olddir/file"; "file content"; "0"];
2270 ["cp_a"; "/olddir"; "/newdir"];
2271 ["cat"; "/newdir/olddir/file"]], "file content")],
2272 "copy a file or directory recursively",
2274 This copies a file or directory from C<src> to C<dest>
2275 recursively using the C<cp -a> command.");
2277 ("mv", (RErr, [Pathname "src"; Pathname "dest"]), 89, [],
2278 [InitBasicFS, Always, TestOutput (
2279 [["write_file"; "/old"; "file content"; "0"];
2280 ["mv"; "/old"; "/new"];
2281 ["cat"; "/new"]], "file content");
2282 InitBasicFS, Always, TestOutputFalse (
2283 [["write_file"; "/old"; "file content"; "0"];
2284 ["mv"; "/old"; "/new"];
2285 ["is_file"; "/old"]])],
2288 This moves a file from C<src> to C<dest> where C<dest> is
2289 either a destination filename or destination directory.");
2291 ("drop_caches", (RErr, [Int "whattodrop"]), 90, [],
2292 [InitEmpty, Always, TestRun (
2293 [["drop_caches"; "3"]])],
2294 "drop kernel page cache, dentries and inodes",
2296 This instructs the guest kernel to drop its page cache,
2297 and/or dentries and inode caches. The parameter C<whattodrop>
2298 tells the kernel what precisely to drop, see
2299 L<http://linux-mm.org/Drop_Caches>
2301 Setting C<whattodrop> to 3 should drop everything.
2303 This automatically calls L<sync(2)> before the operation,
2304 so that the maximum guest memory is freed.");
2306 ("dmesg", (RString "kmsgs", []), 91, [],
2307 [InitEmpty, Always, TestRun (
2309 "return kernel messages",
2311 This returns the kernel messages (C<dmesg> output) from
2312 the guest kernel. This is sometimes useful for extended
2313 debugging of problems.
2315 Another way to get the same information is to enable
2316 verbose messages with C<guestfs_set_verbose> or by setting
2317 the environment variable C<LIBGUESTFS_DEBUG=1> before
2318 running the program.");
2320 ("ping_daemon", (RErr, []), 92, [],
2321 [InitEmpty, Always, TestRun (
2322 [["ping_daemon"]])],
2323 "ping the guest daemon",
2325 This is a test probe into the guestfs daemon running inside
2326 the qemu subprocess. Calling this function checks that the
2327 daemon responds to the ping message, without affecting the daemon
2328 or attached block device(s) in any other way.");
2330 ("equal", (RBool "equality", [Pathname "file1"; Pathname "file2"]), 93, [],
2331 [InitBasicFS, Always, TestOutputTrue (
2332 [["write_file"; "/file1"; "contents of a file"; "0"];
2333 ["cp"; "/file1"; "/file2"];
2334 ["equal"; "/file1"; "/file2"]]);
2335 InitBasicFS, Always, TestOutputFalse (
2336 [["write_file"; "/file1"; "contents of a file"; "0"];
2337 ["write_file"; "/file2"; "contents of another file"; "0"];
2338 ["equal"; "/file1"; "/file2"]]);
2339 InitBasicFS, Always, TestLastFail (
2340 [["equal"; "/file1"; "/file2"]])],
2341 "test if two files have equal contents",
2343 This compares the two files C<file1> and C<file2> and returns
2344 true if their content is exactly equal, or false otherwise.
2346 The external L<cmp(1)> program is used for the comparison.");
2348 ("strings", (RStringList "stringsout", [Pathname "path"]), 94, [ProtocolLimitWarning],
2349 [InitISOFS, Always, TestOutputList (
2350 [["strings"; "/known-5"]], ["abcdefghi"; "jklmnopqr"]);
2351 InitISOFS, Always, TestOutputList (
2352 [["strings"; "/empty"]], [])],
2353 "print the printable strings in a file",
2355 This runs the L<strings(1)> command on a file and returns
2356 the list of printable strings found.");
2358 ("strings_e", (RStringList "stringsout", [String "encoding"; Pathname "path"]), 95, [ProtocolLimitWarning],
2359 [InitISOFS, Always, TestOutputList (
2360 [["strings_e"; "b"; "/known-5"]], []);
2361 InitBasicFS, Disabled, TestOutputList (
2362 [["write_file"; "/new"; "\000h\000e\000l\000l\000o\000\n\000w\000o\000r\000l\000d\000\n"; "24"];
2363 ["strings_e"; "b"; "/new"]], ["hello"; "world"])],
2364 "print the printable strings in a file",
2366 This is like the C<guestfs_strings> command, but allows you to
2367 specify the encoding.
2369 See the L<strings(1)> manpage for the full list of encodings.
2371 Commonly useful encodings are C<l> (lower case L) which will
2372 show strings inside Windows/x86 files.
2374 The returned strings are transcoded to UTF-8.");
2376 ("hexdump", (RString "dump", [Pathname "path"]), 96, [ProtocolLimitWarning],
2377 [InitISOFS, Always, TestOutput (
2378 [["hexdump"; "/known-4"]], "00000000 61 62 63 0a 64 65 66 0a 67 68 69 |abc.def.ghi|\n0000000b\n");
2379 (* Test for RHBZ#501888c2 regression which caused large hexdump
2380 * commands to segfault.
2382 InitISOFS, Always, TestRun (
2383 [["hexdump"; "/100krandom"]])],
2384 "dump a file in hexadecimal",
2386 This runs C<hexdump -C> on the given C<path>. The result is
2387 the human-readable, canonical hex dump of the file.");
2389 ("zerofree", (RErr, [Device "device"]), 97, [],
2390 [InitNone, Always, TestOutput (
2391 [["part_disk"; "/dev/sda"; "mbr"];
2392 ["mkfs"; "ext3"; "/dev/sda1"];
2393 ["mount"; "/dev/sda1"; "/"];
2394 ["write_file"; "/new"; "test file"; "0"];
2395 ["umount"; "/dev/sda1"];
2396 ["zerofree"; "/dev/sda1"];
2397 ["mount"; "/dev/sda1"; "/"];
2398 ["cat"; "/new"]], "test file")],
2399 "zero unused inodes and disk blocks on ext2/3 filesystem",
2401 This runs the I<zerofree> program on C<device>. This program
2402 claims to zero unused inodes and disk blocks on an ext2/3
2403 filesystem, thus making it possible to compress the filesystem
2406 You should B<not> run this program if the filesystem is
2409 It is possible that using this program can damage the filesystem
2410 or data on the filesystem.");
2412 ("pvresize", (RErr, [Device "device"]), 98, [],
2414 "resize an LVM physical volume",
2416 This resizes (expands or shrinks) an existing LVM physical
2417 volume to match the new size of the underlying device.");
2419 ("sfdisk_N", (RErr, [Device "device"; Int "partnum";
2420 Int "cyls"; Int "heads"; Int "sectors";
2421 String "line"]), 99, [DangerWillRobinson],
2423 "modify a single partition on a block device",
2425 This runs L<sfdisk(8)> option to modify just the single
2426 partition C<n> (note: C<n> counts from 1).
2428 For other parameters, see C<guestfs_sfdisk>. You should usually
2429 pass C<0> for the cyls/heads/sectors parameters.
2431 See also: C<guestfs_part_add>");
2433 ("sfdisk_l", (RString "partitions", [Device "device"]), 100, [],
2435 "display the partition table",
2437 This displays the partition table on C<device>, in the
2438 human-readable output of the L<sfdisk(8)> command. It is
2439 not intended to be parsed.
2441 See also: C<guestfs_part_list>");
2443 ("sfdisk_kernel_geometry", (RString "partitions", [Device "device"]), 101, [],
2445 "display the kernel geometry",
2447 This displays the kernel's idea of the geometry of C<device>.
2449 The result is in human-readable format, and not designed to
2452 ("sfdisk_disk_geometry", (RString "partitions", [Device "device"]), 102, [],
2454 "display the disk geometry from the partition table",
2456 This displays the disk geometry of C<device> read from the
2457 partition table. Especially in the case where the underlying
2458 block device has been resized, this can be different from the
2459 kernel's idea of the geometry (see C<guestfs_sfdisk_kernel_geometry>).
2461 The result is in human-readable format, and not designed to
2464 ("vg_activate_all", (RErr, [Bool "activate"]), 103, [],
2466 "activate or deactivate all volume groups",
2468 This command activates or (if C<activate> is false) deactivates
2469 all logical volumes in all volume groups.
2470 If activated, then they are made known to the
2471 kernel, ie. they appear as C</dev/mapper> devices. If deactivated,
2472 then those devices disappear.
2474 This command is the same as running C<vgchange -a y|n>");
2476 ("vg_activate", (RErr, [Bool "activate"; StringList "volgroups"]), 104, [],
2478 "activate or deactivate some volume groups",
2480 This command activates or (if C<activate> is false) deactivates
2481 all logical volumes in the listed volume groups C<volgroups>.
2482 If activated, then they are made known to the
2483 kernel, ie. they appear as C</dev/mapper> devices. If deactivated,
2484 then those devices disappear.
2486 This command is the same as running C<vgchange -a y|n volgroups...>
2488 Note that if C<volgroups> is an empty list then B<all> volume groups
2489 are activated or deactivated.");
2491 ("lvresize", (RErr, [Device "device"; Int "mbytes"]), 105, [],
2492 [InitNone, Always, TestOutput (
2493 [["part_disk"; "/dev/sda"; "mbr"];
2494 ["pvcreate"; "/dev/sda1"];
2495 ["vgcreate"; "VG"; "/dev/sda1"];
2496 ["lvcreate"; "LV"; "VG"; "10"];
2497 ["mkfs"; "ext2"; "/dev/VG/LV"];
2498 ["mount"; "/dev/VG/LV"; "/"];
2499 ["write_file"; "/new"; "test content"; "0"];
2501 ["lvresize"; "/dev/VG/LV"; "20"];
2502 ["e2fsck_f"; "/dev/VG/LV"];
2503 ["resize2fs"; "/dev/VG/LV"];
2504 ["mount"; "/dev/VG/LV"; "/"];
2505 ["cat"; "/new"]], "test content")],
2506 "resize an LVM logical volume",
2508 This resizes (expands or shrinks) an existing LVM logical
2509 volume to C<mbytes>. When reducing, data in the reduced part
2512 ("resize2fs", (RErr, [Device "device"]), 106, [],
2513 [], (* lvresize tests this *)
2514 "resize an ext2/ext3 filesystem",
2516 This resizes an ext2 or ext3 filesystem to match the size of
2517 the underlying device.
2519 I<Note:> It is sometimes required that you run C<guestfs_e2fsck_f>
2520 on the C<device> before calling this command. For unknown reasons
2521 C<resize2fs> sometimes gives an error about this and sometimes not.
2522 In any case, it is always safe to call C<guestfs_e2fsck_f> before
2523 calling this function.");
2525 ("find", (RStringList "names", [Pathname "directory"]), 107, [ProtocolLimitWarning],
2526 [InitBasicFS, Always, TestOutputList (
2527 [["find"; "/"]], ["lost+found"]);
2528 InitBasicFS, Always, TestOutputList (
2532 ["find"; "/"]], ["a"; "b"; "b/c"; "lost+found"]);
2533 InitBasicFS, Always, TestOutputList (
2534 [["mkdir_p"; "/a/b/c"];
2535 ["touch"; "/a/b/c/d"];
2536 ["find"; "/a/b/"]], ["c"; "c/d"])],
2537 "find all files and directories",
2539 This command lists out all files and directories, recursively,
2540 starting at C<directory>. It is essentially equivalent to
2541 running the shell command C<find directory -print> but some
2542 post-processing happens on the output, described below.
2544 This returns a list of strings I<without any prefix>. Thus
2545 if the directory structure was:
2551 then the returned list from C<guestfs_find> C</tmp> would be
2559 If C<directory> is not a directory, then this command returns
2562 The returned list is sorted.
2564 See also C<guestfs_find0>.");
2566 ("e2fsck_f", (RErr, [Device "device"]), 108, [],
2567 [], (* lvresize tests this *)
2568 "check an ext2/ext3 filesystem",
2570 This runs C<e2fsck -p -f device>, ie. runs the ext2/ext3
2571 filesystem checker on C<device>, noninteractively (C<-p>),
2572 even if the filesystem appears to be clean (C<-f>).
2574 This command is only needed because of C<guestfs_resize2fs>
2575 (q.v.). Normally you should use C<guestfs_fsck>.");
2577 ("sleep", (RErr, [Int "secs"]), 109, [],
2578 [InitNone, Always, TestRun (
2580 "sleep for some seconds",
2582 Sleep for C<secs> seconds.");
2584 ("ntfs_3g_probe", (RInt "status", [Bool "rw"; Device "device"]), 110, [],
2585 [InitNone, Always, TestOutputInt (
2586 [["part_disk"; "/dev/sda"; "mbr"];
2587 ["mkfs"; "ntfs"; "/dev/sda1"];
2588 ["ntfs_3g_probe"; "true"; "/dev/sda1"]], 0);
2589 InitNone, Always, TestOutputInt (
2590 [["part_disk"; "/dev/sda"; "mbr"];
2591 ["mkfs"; "ext2"; "/dev/sda1"];
2592 ["ntfs_3g_probe"; "true"; "/dev/sda1"]], 12)],
2593 "probe NTFS volume",
2595 This command runs the L<ntfs-3g.probe(8)> command which probes
2596 an NTFS C<device> for mountability. (Not all NTFS volumes can
2597 be mounted read-write, and some cannot be mounted at all).
2599 C<rw> is a boolean flag. Set it to true if you want to test
2600 if the volume can be mounted read-write. Set it to false if
2601 you want to test if the volume can be mounted read-only.
2603 The return value is an integer which C<0> if the operation
2604 would succeed, or some non-zero value documented in the
2605 L<ntfs-3g.probe(8)> manual page.");
2607 ("sh", (RString "output", [String "command"]), 111, [],
2608 [], (* XXX needs tests *)
2609 "run a command via the shell",
2611 This call runs a command from the guest filesystem via the
2614 This is like C<guestfs_command>, but passes the command to:
2616 /bin/sh -c \"command\"
2618 Depending on the guest's shell, this usually results in
2619 wildcards being expanded, shell expressions being interpolated
2622 All the provisos about C<guestfs_command> apply to this call.");
2624 ("sh_lines", (RStringList "lines", [String "command"]), 112, [],
2625 [], (* XXX needs tests *)
2626 "run a command via the shell returning lines",
2628 This is the same as C<guestfs_sh>, but splits the result
2629 into a list of lines.
2631 See also: C<guestfs_command_lines>");
2633 ("glob_expand", (RStringList "paths", [Pathname "pattern"]), 113, [],
2634 (* Use Pathname here, and hence ABS_PATH (pattern,... in generated
2635 * code in stubs.c, since all valid glob patterns must start with "/".
2636 * There is no concept of "cwd" in libguestfs, hence no "."-relative names.
2638 [InitBasicFS, Always, TestOutputList (
2639 [["mkdir_p"; "/a/b/c"];
2640 ["touch"; "/a/b/c/d"];
2641 ["touch"; "/a/b/c/e"];
2642 ["glob_expand"; "/a/b/c/*"]], ["/a/b/c/d"; "/a/b/c/e"]);
2643 InitBasicFS, Always, TestOutputList (
2644 [["mkdir_p"; "/a/b/c"];
2645 ["touch"; "/a/b/c/d"];
2646 ["touch"; "/a/b/c/e"];
2647 ["glob_expand"; "/a/*/c/*"]], ["/a/b/c/d"; "/a/b/c/e"]);
2648 InitBasicFS, Always, TestOutputList (
2649 [["mkdir_p"; "/a/b/c"];
2650 ["touch"; "/a/b/c/d"];
2651 ["touch"; "/a/b/c/e"];
2652 ["glob_expand"; "/a/*/x/*"]], [])],
2653 "expand a wildcard path",
2655 This command searches for all the pathnames matching
2656 C<pattern> according to the wildcard expansion rules
2659 If no paths match, then this returns an empty list
2660 (note: not an error).
2662 It is just a wrapper around the C L<glob(3)> function
2663 with flags C<GLOB_MARK|GLOB_BRACE>.
2664 See that manual page for more details.");
2666 ("scrub_device", (RErr, [Device "device"]), 114, [DangerWillRobinson],
2667 [InitNone, Always, TestRun ( (* use /dev/sdc because it's smaller *)
2668 [["scrub_device"; "/dev/sdc"]])],
2669 "scrub (securely wipe) a device",
2671 This command writes patterns over C<device> to make data retrieval
2674 It is an interface to the L<scrub(1)> program. See that
2675 manual page for more details.");
2677 ("scrub_file", (RErr, [Pathname "file"]), 115, [],
2678 [InitBasicFS, Always, TestRun (
2679 [["write_file"; "/file"; "content"; "0"];
2680 ["scrub_file"; "/file"]])],
2681 "scrub (securely wipe) a file",
2683 This command writes patterns over a file to make data retrieval
2686 The file is I<removed> after scrubbing.
2688 It is an interface to the L<scrub(1)> program. See that
2689 manual page for more details.");
2691 ("scrub_freespace", (RErr, [Pathname "dir"]), 116, [],
2692 [], (* XXX needs testing *)
2693 "scrub (securely wipe) free space",
2695 This command creates the directory C<dir> and then fills it
2696 with files until the filesystem is full, and scrubs the files
2697 as for C<guestfs_scrub_file>, and deletes them.
2698 The intention is to scrub any free space on the partition
2701 It is an interface to the L<scrub(1)> program. See that
2702 manual page for more details.");
2704 ("mkdtemp", (RString "dir", [Pathname "template"]), 117, [],
2705 [InitBasicFS, Always, TestRun (
2707 ["mkdtemp"; "/tmp/tmpXXXXXX"]])],
2708 "create a temporary directory",
2710 This command creates a temporary directory. The
2711 C<template> parameter should be a full pathname for the
2712 temporary directory name with the final six characters being
2715 For example: \"/tmp/myprogXXXXXX\" or \"/Temp/myprogXXXXXX\",
2716 the second one being suitable for Windows filesystems.
2718 The name of the temporary directory that was created
2721 The temporary directory is created with mode 0700
2722 and is owned by root.
2724 The caller is responsible for deleting the temporary
2725 directory and its contents after use.
2727 See also: L<mkdtemp(3)>");
2729 ("wc_l", (RInt "lines", [Pathname "path"]), 118, [],
2730 [InitISOFS, Always, TestOutputInt (
2731 [["wc_l"; "/10klines"]], 10000)],
2732 "count lines in a file",
2734 This command counts the lines in a file, using the
2735 C<wc -l> external command.");
2737 ("wc_w", (RInt "words", [Pathname "path"]), 119, [],
2738 [InitISOFS, Always, TestOutputInt (
2739 [["wc_w"; "/10klines"]], 10000)],
2740 "count words in a file",
2742 This command counts the words in a file, using the
2743 C<wc -w> external command.");
2745 ("wc_c", (RInt "chars", [Pathname "path"]), 120, [],
2746 [InitISOFS, Always, TestOutputInt (
2747 [["wc_c"; "/100kallspaces"]], 102400)],
2748 "count characters in a file",
2750 This command counts the characters in a file, using the
2751 C<wc -c> external command.");
2753 ("head", (RStringList "lines", [Pathname "path"]), 121, [ProtocolLimitWarning],
2754 [InitISOFS, Always, TestOutputList (
2755 [["head"; "/10klines"]], ["0abcdefghijklmnopqrstuvwxyz";"1abcdefghijklmnopqrstuvwxyz";"2abcdefghijklmnopqrstuvwxyz";"3abcdefghijklmnopqrstuvwxyz";"4abcdefghijklmnopqrstuvwxyz";"5abcdefghijklmnopqrstuvwxyz";"6abcdefghijklmnopqrstuvwxyz";"7abcdefghijklmnopqrstuvwxyz";"8abcdefghijklmnopqrstuvwxyz";"9abcdefghijklmnopqrstuvwxyz"])],
2756 "return first 10 lines of a file",
2758 This command returns up to the first 10 lines of a file as
2759 a list of strings.");
2761 ("head_n", (RStringList "lines", [Int "nrlines"; Pathname "path"]), 122, [ProtocolLimitWarning],
2762 [InitISOFS, Always, TestOutputList (
2763 [["head_n"; "3"; "/10klines"]], ["0abcdefghijklmnopqrstuvwxyz";"1abcdefghijklmnopqrstuvwxyz";"2abcdefghijklmnopqrstuvwxyz"]);
2764 InitISOFS, Always, TestOutputList (
2765 [["head_n"; "-9997"; "/10klines"]], ["0abcdefghijklmnopqrstuvwxyz";"1abcdefghijklmnopqrstuvwxyz";"2abcdefghijklmnopqrstuvwxyz"]);
2766 InitISOFS, Always, TestOutputList (
2767 [["head_n"; "0"; "/10klines"]], [])],
2768 "return first N lines of a file",
2770 If the parameter C<nrlines> is a positive number, this returns the first
2771 C<nrlines> lines of the file C<path>.
2773 If the parameter C<nrlines> is a negative number, this returns lines
2774 from the file C<path>, excluding the last C<nrlines> lines.
2776 If the parameter C<nrlines> is zero, this returns an empty list.");
2778 ("tail", (RStringList "lines", [Pathname "path"]), 123, [ProtocolLimitWarning],
2779 [InitISOFS, Always, TestOutputList (
2780 [["tail"; "/10klines"]], ["9990abcdefghijklmnopqrstuvwxyz";"9991abcdefghijklmnopqrstuvwxyz";"9992abcdefghijklmnopqrstuvwxyz";"9993abcdefghijklmnopqrstuvwxyz";"9994abcdefghijklmnopqrstuvwxyz";"9995abcdefghijklmnopqrstuvwxyz";"9996abcdefghijklmnopqrstuvwxyz";"9997abcdefghijklmnopqrstuvwxyz";"9998abcdefghijklmnopqrstuvwxyz";"9999abcdefghijklmnopqrstuvwxyz"])],
2781 "return last 10 lines of a file",
2783 This command returns up to the last 10 lines of a file as
2784 a list of strings.");
2786 ("tail_n", (RStringList "lines", [Int "nrlines"; Pathname "path"]), 124, [ProtocolLimitWarning],
2787 [InitISOFS, Always, TestOutputList (
2788 [["tail_n"; "3"; "/10klines"]], ["9997abcdefghijklmnopqrstuvwxyz";"9998abcdefghijklmnopqrstuvwxyz";"9999abcdefghijklmnopqrstuvwxyz"]);
2789 InitISOFS, Always, TestOutputList (
2790 [["tail_n"; "-9998"; "/10klines"]], ["9997abcdefghijklmnopqrstuvwxyz";"9998abcdefghijklmnopqrstuvwxyz";"9999abcdefghijklmnopqrstuvwxyz"]);
2791 InitISOFS, Always, TestOutputList (
2792 [["tail_n"; "0"; "/10klines"]], [])],
2793 "return last N lines of a file",
2795 If the parameter C<nrlines> is a positive number, this returns the last
2796 C<nrlines> lines of the file C<path>.
2798 If the parameter C<nrlines> is a negative number, this returns lines
2799 from the file C<path>, starting with the C<-nrlines>th line.
2801 If the parameter C<nrlines> is zero, this returns an empty list.");
2803 ("df", (RString "output", []), 125, [],
2804 [], (* XXX Tricky to test because it depends on the exact format
2805 * of the 'df' command and other imponderables.
2807 "report file system disk space usage",
2809 This command runs the C<df> command to report disk space used.
2811 This command is mostly useful for interactive sessions. It
2812 is I<not> intended that you try to parse the output string.
2813 Use C<statvfs> from programs.");
2815 ("df_h", (RString "output", []), 126, [],
2816 [], (* XXX Tricky to test because it depends on the exact format
2817 * of the 'df' command and other imponderables.
2819 "report file system disk space usage (human readable)",
2821 This command runs the C<df -h> command to report disk space used
2822 in human-readable format.
2824 This command is mostly useful for interactive sessions. It
2825 is I<not> intended that you try to parse the output string.
2826 Use C<statvfs> from programs.");
2828 ("du", (RInt64 "sizekb", [Pathname "path"]), 127, [],
2829 [InitISOFS, Always, TestOutputInt (
2830 [["du"; "/directory"]], 2 (* ISO fs blocksize is 2K *))],
2831 "estimate file space usage",
2833 This command runs the C<du -s> command to estimate file space
2836 C<path> can be a file or a directory. If C<path> is a directory
2837 then the estimate includes the contents of the directory and all
2838 subdirectories (recursively).
2840 The result is the estimated size in I<kilobytes>
2841 (ie. units of 1024 bytes).");
2843 ("initrd_list", (RStringList "filenames", [Pathname "path"]), 128, [],
2844 [InitISOFS, Always, TestOutputList (
2845 [["initrd_list"; "/initrd"]], ["empty";"known-1";"known-2";"known-3";"known-4"; "known-5"])],
2846 "list files in an initrd",
2848 This command lists out files contained in an initrd.
2850 The files are listed without any initial C</> character. The
2851 files are listed in the order they appear (not necessarily
2852 alphabetical). Directory names are listed as separate items.
2854 Old Linux kernels (2.4 and earlier) used a compressed ext2
2855 filesystem as initrd. We I<only> support the newer initramfs
2856 format (compressed cpio files).");
2858 ("mount_loop", (RErr, [Pathname "file"; Pathname "mountpoint"]), 129, [],
2860 "mount a file using the loop device",
2862 This command lets you mount C<file> (a filesystem image
2863 in a file) on a mount point. It is entirely equivalent to
2864 the command C<mount -o loop file mountpoint>.");
2866 ("mkswap", (RErr, [Device "device"]), 130, [],
2867 [InitEmpty, Always, TestRun (
2868 [["part_disk"; "/dev/sda"; "mbr"];
2869 ["mkswap"; "/dev/sda1"]])],
2870 "create a swap partition",
2872 Create a swap partition on C<device>.");
2874 ("mkswap_L", (RErr, [String "label"; Device "device"]), 131, [],
2875 [InitEmpty, Always, TestRun (
2876 [["part_disk"; "/dev/sda"; "mbr"];
2877 ["mkswap_L"; "hello"; "/dev/sda1"]])],
2878 "create a swap partition with a label",
2880 Create a swap partition on C<device> with label C<label>.
2882 Note that you cannot attach a swap label to a block device
2883 (eg. C</dev/sda>), just to a partition. This appears to be
2884 a limitation of the kernel or swap tools.");
2886 ("mkswap_U", (RErr, [String "uuid"; Device "device"]), 132, [],
2887 (let uuid = uuidgen () in
2888 [InitEmpty, Always, TestRun (
2889 [["part_disk"; "/dev/sda"; "mbr"];
2890 ["mkswap_U"; uuid; "/dev/sda1"]])]),
2891 "create a swap partition with an explicit UUID",
2893 Create a swap partition on C<device> with UUID C<uuid>.");
2895 ("mknod", (RErr, [Int "mode"; Int "devmajor"; Int "devminor"; Pathname "path"]), 133, [],
2896 [InitBasicFS, Always, TestOutputStruct (
2897 [["mknod"; "0o10777"; "0"; "0"; "/node"];
2898 (* NB: default umask 022 means 0777 -> 0755 in these tests *)
2899 ["stat"; "/node"]], [CompareWithInt ("mode", 0o10755)]);
2900 InitBasicFS, Always, TestOutputStruct (
2901 [["mknod"; "0o60777"; "66"; "99"; "/node"];
2902 ["stat"; "/node"]], [CompareWithInt ("mode", 0o60755)])],
2903 "make block, character or FIFO devices",
2905 This call creates block or character special devices, or
2906 named pipes (FIFOs).
2908 The C<mode> parameter should be the mode, using the standard
2909 constants. C<devmajor> and C<devminor> are the
2910 device major and minor numbers, only used when creating block
2911 and character special devices.");
2913 ("mkfifo", (RErr, [Int "mode"; Pathname "path"]), 134, [],
2914 [InitBasicFS, Always, TestOutputStruct (
2915 [["mkfifo"; "0o777"; "/node"];
2916 ["stat"; "/node"]], [CompareWithInt ("mode", 0o10755)])],
2917 "make FIFO (named pipe)",
2919 This call creates a FIFO (named pipe) called C<path> with
2920 mode C<mode>. It is just a convenient wrapper around
2921 C<guestfs_mknod>.");
2923 ("mknod_b", (RErr, [Int "mode"; Int "devmajor"; Int "devminor"; Pathname "path"]), 135, [],
2924 [InitBasicFS, Always, TestOutputStruct (
2925 [["mknod_b"; "0o777"; "99"; "66"; "/node"];
2926 ["stat"; "/node"]], [CompareWithInt ("mode", 0o60755)])],
2927 "make block device node",
2929 This call creates a block device node called C<path> with
2930 mode C<mode> and device major/minor C<devmajor> and C<devminor>.
2931 It is just a convenient wrapper around C<guestfs_mknod>.");
2933 ("mknod_c", (RErr, [Int "mode"; Int "devmajor"; Int "devminor"; Pathname "path"]), 136, [],
2934 [InitBasicFS, Always, TestOutputStruct (
2935 [["mknod_c"; "0o777"; "99"; "66"; "/node"];
2936 ["stat"; "/node"]], [CompareWithInt ("mode", 0o20755)])],
2937 "make char device node",
2939 This call creates a char device node called C<path> with
2940 mode C<mode> and device major/minor C<devmajor> and C<devminor>.
2941 It is just a convenient wrapper around C<guestfs_mknod>.");
2943 ("umask", (RInt "oldmask", [Int "mask"]), 137, [],
2944 [], (* XXX umask is one of those stateful things that we should
2945 * reset between each test.
2947 "set file mode creation mask (umask)",
2949 This function sets the mask used for creating new files and
2950 device nodes to C<mask & 0777>.
2952 Typical umask values would be C<022> which creates new files
2953 with permissions like \"-rw-r--r--\" or \"-rwxr-xr-x\", and
2954 C<002> which creates new files with permissions like
2955 \"-rw-rw-r--\" or \"-rwxrwxr-x\".
2957 The default umask is C<022>. This is important because it
2958 means that directories and device nodes will be created with
2959 C<0644> or C<0755> mode even if you specify C<0777>.
2961 See also L<umask(2)>, C<guestfs_mknod>, C<guestfs_mkdir>.
2963 This call returns the previous umask.");
2965 ("readdir", (RStructList ("entries", "dirent"), [Pathname "dir"]), 138, [],
2967 "read directories entries",
2969 This returns the list of directory entries in directory C<dir>.
2971 All entries in the directory are returned, including C<.> and
2972 C<..>. The entries are I<not> sorted, but returned in the same
2973 order as the underlying filesystem.
2975 Also this call returns basic file type information about each
2976 file. The C<ftyp> field will contain one of the following characters:
3014 The L<readdir(3)> returned a C<d_type> field with an
3019 This function is primarily intended for use by programs. To
3020 get a simple list of names, use C<guestfs_ls>. To get a printable
3021 directory for human consumption, use C<guestfs_ll>.");
3023 ("sfdiskM", (RErr, [Device "device"; StringList "lines"]), 139, [DangerWillRobinson],
3025 "create partitions on a block device",
3027 This is a simplified interface to the C<guestfs_sfdisk>
3028 command, where partition sizes are specified in megabytes
3029 only (rounded to the nearest cylinder) and you don't need
3030 to specify the cyls, heads and sectors parameters which
3031 were rarely if ever used anyway.
3033 See also: C<guestfs_sfdisk>, the L<sfdisk(8)> manpage
3034 and C<guestfs_part_disk>");
3036 ("zfile", (RString "description", [String "meth"; Pathname "path"]), 140, [DeprecatedBy "file"],
3038 "determine file type inside a compressed file",
3040 This command runs C<file> after first decompressing C<path>
3043 C<method> must be one of C<gzip>, C<compress> or C<bzip2>.
3045 Since 1.0.63, use C<guestfs_file> instead which can now
3046 process compressed files.");
3048 ("getxattrs", (RStructList ("xattrs", "xattr"), [Pathname "path"]), 141, [],
3050 "list extended attributes of a file or directory",
3052 This call lists the extended attributes of the file or directory
3055 At the system call level, this is a combination of the
3056 L<listxattr(2)> and L<getxattr(2)> calls.
3058 See also: C<guestfs_lgetxattrs>, L<attr(5)>.");
3060 ("lgetxattrs", (RStructList ("xattrs", "xattr"), [Pathname "path"]), 142, [],
3062 "list extended attributes of a file or directory",
3064 This is the same as C<guestfs_getxattrs>, but if C<path>
3065 is a symbolic link, then it returns the extended attributes
3066 of the link itself.");
3068 ("setxattr", (RErr, [String "xattr";
3069 String "val"; Int "vallen"; (* will be BufferIn *)
3070 Pathname "path"]), 143, [],
3072 "set extended attribute of a file or directory",
3074 This call sets the extended attribute named C<xattr>
3075 of the file C<path> to the value C<val> (of length C<vallen>).
3076 The value is arbitrary 8 bit data.
3078 See also: C<guestfs_lsetxattr>, L<attr(5)>.");
3080 ("lsetxattr", (RErr, [String "xattr";
3081 String "val"; Int "vallen"; (* will be BufferIn *)
3082 Pathname "path"]), 144, [],
3084 "set extended attribute of a file or directory",
3086 This is the same as C<guestfs_setxattr>, but if C<path>
3087 is a symbolic link, then it sets an extended attribute
3088 of the link itself.");
3090 ("removexattr", (RErr, [String "xattr"; Pathname "path"]), 145, [],
3092 "remove extended attribute of a file or directory",
3094 This call removes the extended attribute named C<xattr>
3095 of the file C<path>.
3097 See also: C<guestfs_lremovexattr>, L<attr(5)>.");
3099 ("lremovexattr", (RErr, [String "xattr"; Pathname "path"]), 146, [],
3101 "remove extended attribute of a file or directory",
3103 This is the same as C<guestfs_removexattr>, but if C<path>
3104 is a symbolic link, then it removes an extended attribute
3105 of the link itself.");
3107 ("mountpoints", (RHashtable "mps", []), 147, [],
3111 This call is similar to C<guestfs_mounts>. That call returns
3112 a list of devices. This one returns a hash table (map) of
3113 device name to directory where the device is mounted.");
3115 ("mkmountpoint", (RErr, [String "exemptpath"]), 148, [],
3116 (* This is a special case: while you would expect a parameter
3117 * of type "Pathname", that doesn't work, because it implies
3118 * NEED_ROOT in the generated calling code in stubs.c, and
3119 * this function cannot use NEED_ROOT.
3122 "create a mountpoint",
3124 C<guestfs_mkmountpoint> and C<guestfs_rmmountpoint> are
3125 specialized calls that can be used to create extra mountpoints
3126 before mounting the first filesystem.
3128 These calls are I<only> necessary in some very limited circumstances,
3129 mainly the case where you want to mount a mix of unrelated and/or
3130 read-only filesystems together.
3132 For example, live CDs often contain a \"Russian doll\" nest of
3133 filesystems, an ISO outer layer, with a squashfs image inside, with
3134 an ext2/3 image inside that. You can unpack this as follows
3137 add-ro Fedora-11-i686-Live.iso
3140 mkmountpoint /squash
3143 mount-loop /cd/LiveOS/squashfs.img /squash
3144 mount-loop /squash/LiveOS/ext3fs.img /ext3
3146 The inner filesystem is now unpacked under the /ext3 mountpoint.");
3148 ("rmmountpoint", (RErr, [String "exemptpath"]), 149, [],
3150 "remove a mountpoint",
3152 This calls removes a mountpoint that was previously created
3153 with C<guestfs_mkmountpoint>. See C<guestfs_mkmountpoint>
3154 for full details.");
3156 ("read_file", (RBufferOut "content", [Pathname "path"]), 150, [ProtocolLimitWarning],
3157 [InitISOFS, Always, TestOutputBuffer (
3158 [["read_file"; "/known-4"]], "abc\ndef\nghi")],
3161 This calls returns the contents of the file C<path> as a
3164 Unlike C<guestfs_cat>, this function can correctly
3165 handle files that contain embedded ASCII NUL characters.
3166 However unlike C<guestfs_download>, this function is limited
3167 in the total size of file that can be handled.");
3169 ("grep", (RStringList "lines", [String "regex"; Pathname "path"]), 151, [ProtocolLimitWarning],
3170 [InitISOFS, Always, TestOutputList (
3171 [["grep"; "abc"; "/test-grep.txt"]], ["abc"; "abc123"]);
3172 InitISOFS, Always, TestOutputList (
3173 [["grep"; "nomatch"; "/test-grep.txt"]], [])],
3174 "return lines matching a pattern",
3176 This calls the external C<grep> program and returns the
3179 ("egrep", (RStringList "lines", [String "regex"; Pathname "path"]), 152, [ProtocolLimitWarning],
3180 [InitISOFS, Always, TestOutputList (
3181 [["egrep"; "abc"; "/test-grep.txt"]], ["abc"; "abc123"])],
3182 "return lines matching a pattern",
3184 This calls the external C<egrep> program and returns the
3187 ("fgrep", (RStringList "lines", [String "pattern"; Pathname "path"]), 153, [ProtocolLimitWarning],
3188 [InitISOFS, Always, TestOutputList (
3189 [["fgrep"; "abc"; "/test-grep.txt"]], ["abc"; "abc123"])],
3190 "return lines matching a pattern",
3192 This calls the external C<fgrep> program and returns the
3195 ("grepi", (RStringList "lines", [String "regex"; Pathname "path"]), 154, [ProtocolLimitWarning],
3196 [InitISOFS, Always, TestOutputList (
3197 [["grepi"; "abc"; "/test-grep.txt"]], ["abc"; "abc123"; "ABC"])],
3198 "return lines matching a pattern",
3200 This calls the external C<grep -i> program and returns the
3203 ("egrepi", (RStringList "lines", [String "regex"; Pathname "path"]), 155, [ProtocolLimitWarning],
3204 [InitISOFS, Always, TestOutputList (
3205 [["egrepi"; "abc"; "/test-grep.txt"]], ["abc"; "abc123"; "ABC"])],
3206 "return lines matching a pattern",
3208 This calls the external C<egrep -i> program and returns the
3211 ("fgrepi", (RStringList "lines", [String "pattern"; Pathname "path"]), 156, [ProtocolLimitWarning],
3212 [InitISOFS, Always, TestOutputList (
3213 [["fgrepi"; "abc"; "/test-grep.txt"]], ["abc"; "abc123"; "ABC"])],
3214 "return lines matching a pattern",
3216 This calls the external C<fgrep -i> program and returns the
3219 ("zgrep", (RStringList "lines", [String "regex"; Pathname "path"]), 157, [ProtocolLimitWarning],
3220 [InitISOFS, Always, TestOutputList (
3221 [["zgrep"; "abc"; "/test-grep.txt.gz"]], ["abc"; "abc123"])],
3222 "return lines matching a pattern",
3224 This calls the external C<zgrep> program and returns the
3227 ("zegrep", (RStringList "lines", [String "regex"; Pathname "path"]), 158, [ProtocolLimitWarning],
3228 [InitISOFS, Always, TestOutputList (
3229 [["zegrep"; "abc"; "/test-grep.txt.gz"]], ["abc"; "abc123"])],
3230 "return lines matching a pattern",
3232 This calls the external C<zegrep> program and returns the
3235 ("zfgrep", (RStringList "lines", [String "pattern"; Pathname "path"]), 159, [ProtocolLimitWarning],
3236 [InitISOFS, Always, TestOutputList (
3237 [["zfgrep"; "abc"; "/test-grep.txt.gz"]], ["abc"; "abc123"])],
3238 "return lines matching a pattern",
3240 This calls the external C<zfgrep> program and returns the
3243 ("zgrepi", (RStringList "lines", [String "regex"; Pathname "path"]), 160, [ProtocolLimitWarning],
3244 [InitISOFS, Always, TestOutputList (
3245 [["zgrepi"; "abc"; "/test-grep.txt.gz"]], ["abc"; "abc123"; "ABC"])],
3246 "return lines matching a pattern",
3248 This calls the external C<zgrep -i> program and returns the
3251 ("zegrepi", (RStringList "lines", [String "regex"; Pathname "path"]), 161, [ProtocolLimitWarning],
3252 [InitISOFS, Always, TestOutputList (
3253 [["zegrepi"; "abc"; "/test-grep.txt.gz"]], ["abc"; "abc123"; "ABC"])],
3254 "return lines matching a pattern",
3256 This calls the external C<zegrep -i> program and returns the
3259 ("zfgrepi", (RStringList "lines", [String "pattern"; Pathname "path"]), 162, [ProtocolLimitWarning],
3260 [InitISOFS, Always, TestOutputList (
3261 [["zfgrepi"; "abc"; "/test-grep.txt.gz"]], ["abc"; "abc123"; "ABC"])],
3262 "return lines matching a pattern",
3264 This calls the external C<zfgrep -i> program and returns the
3267 ("realpath", (RString "rpath", [Pathname "path"]), 163, [],
3268 [InitISOFS, Always, TestOutput (
3269 [["realpath"; "/../directory"]], "/directory")],
3270 "canonicalized absolute pathname",
3272 Return the canonicalized absolute pathname of C<path>. The
3273 returned path has no C<.>, C<..> or symbolic link path elements.");
3275 ("ln", (RErr, [String "target"; Pathname "linkname"]), 164, [],
3276 [InitBasicFS, Always, TestOutputStruct (
3279 ["stat"; "/b"]], [CompareWithInt ("nlink", 2)])],
3280 "create a hard link",
3282 This command creates a hard link using the C<ln> command.");
3284 ("ln_f", (RErr, [String "target"; Pathname "linkname"]), 165, [],
3285 [InitBasicFS, Always, TestOutputStruct (
3288 ["ln_f"; "/a"; "/b"];
3289 ["stat"; "/b"]], [CompareWithInt ("nlink", 2)])],
3290 "create a hard link",
3292 This command creates a hard link using the C<ln -f> command.
3293 The C<-f> option removes the link (C<linkname>) if it exists already.");
3295 ("ln_s", (RErr, [String "target"; Pathname "linkname"]), 166, [],
3296 [InitBasicFS, Always, TestOutputStruct (
3298 ["ln_s"; "a"; "/b"];
3299 ["lstat"; "/b"]], [CompareWithInt ("mode", 0o120777)])],
3300 "create a symbolic link",
3302 This command creates a symbolic link using the C<ln -s> command.");
3304 ("ln_sf", (RErr, [String "target"; Pathname "linkname"]), 167, [],
3305 [InitBasicFS, Always, TestOutput (
3306 [["mkdir_p"; "/a/b"];
3307 ["touch"; "/a/b/c"];
3308 ["ln_sf"; "../d"; "/a/b/c"];
3309 ["readlink"; "/a/b/c"]], "../d")],
3310 "create a symbolic link",
3312 This command creates a symbolic link using the C<ln -sf> command,
3313 The C<-f> option removes the link (C<linkname>) if it exists already.");
3315 ("readlink", (RString "link", [Pathname "path"]), 168, [],
3316 [] (* XXX tested above *),
3317 "read the target of a symbolic link",
3319 This command reads the target of a symbolic link.");
3321 ("fallocate", (RErr, [Pathname "path"; Int "len"]), 169, [],
3322 [InitBasicFS, Always, TestOutputStruct (
3323 [["fallocate"; "/a"; "1000000"];
3324 ["stat"; "/a"]], [CompareWithInt ("size", 1_000_000)])],
3325 "preallocate a file in the guest filesystem",
3327 This command preallocates a file (containing zero bytes) named
3328 C<path> of size C<len> bytes. If the file exists already, it
3331 Do not confuse this with the guestfish-specific
3332 C<alloc> command which allocates a file in the host and
3333 attaches it as a device.");
3335 ("swapon_device", (RErr, [Device "device"]), 170, [],
3336 [InitPartition, Always, TestRun (
3337 [["mkswap"; "/dev/sda1"];
3338 ["swapon_device"; "/dev/sda1"];
3339 ["swapoff_device"; "/dev/sda1"]])],
3340 "enable swap on device",
3342 This command enables the libguestfs appliance to use the
3343 swap device or partition named C<device>. The increased
3344 memory is made available for all commands, for example
3345 those run using C<guestfs_command> or C<guestfs_sh>.
3347 Note that you should not swap to existing guest swap
3348 partitions unless you know what you are doing. They may
3349 contain hibernation information, or other information that
3350 the guest doesn't want you to trash. You also risk leaking
3351 information about the host to the guest this way. Instead,
3352 attach a new host device to the guest and swap on that.");
3354 ("swapoff_device", (RErr, [Device "device"]), 171, [],
3355 [], (* XXX tested by swapon_device *)
3356 "disable swap on device",
3358 This command disables the libguestfs appliance swap
3359 device or partition named C<device>.
3360 See C<guestfs_swapon_device>.");
3362 ("swapon_file", (RErr, [Pathname "file"]), 172, [],
3363 [InitBasicFS, Always, TestRun (
3364 [["fallocate"; "/swap"; "8388608"];
3365 ["mkswap_file"; "/swap"];
3366 ["swapon_file"; "/swap"];
3367 ["swapoff_file"; "/swap"]])],
3368 "enable swap on file",
3370 This command enables swap to a file.
3371 See C<guestfs_swapon_device> for other notes.");
3373 ("swapoff_file", (RErr, [Pathname "file"]), 173, [],
3374 [], (* XXX tested by swapon_file *)
3375 "disable swap on file",
3377 This command disables the libguestfs appliance swap on file.");
3379 ("swapon_label", (RErr, [String "label"]), 174, [],
3380 [InitEmpty, Always, TestRun (
3381 [["part_disk"; "/dev/sdb"; "mbr"];
3382 ["mkswap_L"; "swapit"; "/dev/sdb1"];
3383 ["swapon_label"; "swapit"];
3384 ["swapoff_label"; "swapit"];
3385 ["zero"; "/dev/sdb"];
3386 ["blockdev_rereadpt"; "/dev/sdb"]])],
3387 "enable swap on labeled swap partition",
3389 This command enables swap to a labeled swap partition.
3390 See C<guestfs_swapon_device> for other notes.");
3392 ("swapoff_label", (RErr, [String "label"]), 175, [],
3393 [], (* XXX tested by swapon_label *)
3394 "disable swap on labeled swap partition",
3396 This command disables the libguestfs appliance swap on
3397 labeled swap partition.");
3399 ("swapon_uuid", (RErr, [String "uuid"]), 176, [],
3400 (let uuid = uuidgen () in
3401 [InitEmpty, Always, TestRun (
3402 [["mkswap_U"; uuid; "/dev/sdb"];
3403 ["swapon_uuid"; uuid];
3404 ["swapoff_uuid"; uuid]])]),
3405 "enable swap on swap partition by UUID",
3407 This command enables swap to a swap partition with the given UUID.
3408 See C<guestfs_swapon_device> for other notes.");
3410 ("swapoff_uuid", (RErr, [String "uuid"]), 177, [],
3411 [], (* XXX tested by swapon_uuid *)
3412 "disable swap on swap partition by UUID",
3414 This command disables the libguestfs appliance swap partition
3415 with the given UUID.");
3417 ("mkswap_file", (RErr, [Pathname "path"]), 178, [],
3418 [InitBasicFS, Always, TestRun (
3419 [["fallocate"; "/swap"; "8388608"];
3420 ["mkswap_file"; "/swap"]])],
3421 "create a swap file",
3425 This command just writes a swap file signature to an existing
3426 file. To create the file itself, use something like C<guestfs_fallocate>.");
3428 ("inotify_init", (RErr, [Int "maxevents"]), 179, [],
3429 [InitISOFS, Always, TestRun (
3430 [["inotify_init"; "0"]])],
3431 "create an inotify handle",
3433 This command creates a new inotify handle.
3434 The inotify subsystem can be used to notify events which happen to
3435 objects in the guest filesystem.
3437 C<maxevents> is the maximum number of events which will be
3438 queued up between calls to C<guestfs_inotify_read> or
3439 C<guestfs_inotify_files>.
3440 If this is passed as C<0>, then the kernel (or previously set)
3441 default is used. For Linux 2.6.29 the default was 16384 events.
3442 Beyond this limit, the kernel throws away events, but records
3443 the fact that it threw them away by setting a flag
3444 C<IN_Q_OVERFLOW> in the returned structure list (see
3445 C<guestfs_inotify_read>).
3447 Before any events are generated, you have to add some
3448 watches to the internal watch list. See:
3449 C<guestfs_inotify_add_watch>,
3450 C<guestfs_inotify_rm_watch> and
3451 C<guestfs_inotify_watch_all>.
3453 Queued up events should be read periodically by calling
3454 C<guestfs_inotify_read>
3455 (or C<guestfs_inotify_files> which is just a helpful
3456 wrapper around C<guestfs_inotify_read>). If you don't
3457 read the events out often enough then you risk the internal
3460 The handle should be closed after use by calling
3461 C<guestfs_inotify_close>. This also removes any
3462 watches automatically.
3464 See also L<inotify(7)> for an overview of the inotify interface
3465 as exposed by the Linux kernel, which is roughly what we expose
3466 via libguestfs. Note that there is one global inotify handle
3467 per libguestfs instance.");
3469 ("inotify_add_watch", (RInt64 "wd", [Pathname "path"; Int "mask"]), 180, [],
3470 [InitBasicFS, Always, TestOutputList (
3471 [["inotify_init"; "0"];
3472 ["inotify_add_watch"; "/"; "1073741823"];
3475 ["inotify_files"]], ["a"; "b"])],
3476 "add an inotify watch",
3478 Watch C<path> for the events listed in C<mask>.
3480 Note that if C<path> is a directory then events within that
3481 directory are watched, but this does I<not> happen recursively
3482 (in subdirectories).
3484 Note for non-C or non-Linux callers: the inotify events are
3485 defined by the Linux kernel ABI and are listed in
3486 C</usr/include/sys/inotify.h>.");
3488 ("inotify_rm_watch", (RErr, [Int(*XXX64*) "wd"]), 181, [],
3490 "remove an inotify watch",
3492 Remove a previously defined inotify watch.
3493 See C<guestfs_inotify_add_watch>.");
3495 ("inotify_read", (RStructList ("events", "inotify_event"), []), 182, [],
3497 "return list of inotify events",
3499 Return the complete queue of events that have happened
3500 since the previous read call.
3502 If no events have happened, this returns an empty list.
3504 I<Note>: In order to make sure that all events have been
3505 read, you must call this function repeatedly until it
3506 returns an empty list. The reason is that the call will
3507 read events up to the maximum appliance-to-host message
3508 size and leave remaining events in the queue.");
3510 ("inotify_files", (RStringList "paths", []), 183, [],
3512 "return list of watched files that had events",
3514 This function is a helpful wrapper around C<guestfs_inotify_read>
3515 which just returns a list of pathnames of objects that were
3516 touched. The returned pathnames are sorted and deduplicated.");
3518 ("inotify_close", (RErr, []), 184, [],
3520 "close the inotify handle",
3522 This closes the inotify handle which was previously
3523 opened by inotify_init. It removes all watches, throws
3524 away any pending events, and deallocates all resources.");
3526 ("setcon", (RErr, [String "context"]), 185, [],
3528 "set SELinux security context",
3530 This sets the SELinux security context of the daemon
3531 to the string C<context>.
3533 See the documentation about SELINUX in L<guestfs(3)>.");
3535 ("getcon", (RString "context", []), 186, [],
3537 "get SELinux security context",
3539 This gets the SELinux security context of the daemon.
3541 See the documentation about SELINUX in L<guestfs(3)>,
3542 and C<guestfs_setcon>");
3544 ("mkfs_b", (RErr, [String "fstype"; Int "blocksize"; Device "device"]), 187, [],
3545 [InitEmpty, Always, TestOutput (
3546 [["part_disk"; "/dev/sda"; "mbr"];
3547 ["mkfs_b"; "ext2"; "4096"; "/dev/sda1"];
3548 ["mount"; "/dev/sda1"; "/"];
3549 ["write_file"; "/new"; "new file contents"; "0"];
3550 ["cat"; "/new"]], "new file contents")],
3551 "make a filesystem with block size",
3553 This call is similar to C<guestfs_mkfs>, but it allows you to
3554 control the block size of the resulting filesystem. Supported
3555 block sizes depend on the filesystem type, but typically they
3556 are C<1024>, C<2048> or C<4096> only.");
3558 ("mke2journal", (RErr, [Int "blocksize"; Device "device"]), 188, [],
3559 [InitEmpty, Always, TestOutput (
3560 [["sfdiskM"; "/dev/sda"; ",100 ,"];
3561 ["mke2journal"; "4096"; "/dev/sda1"];
3562 ["mke2fs_J"; "ext2"; "4096"; "/dev/sda2"; "/dev/sda1"];
3563 ["mount"; "/dev/sda2"; "/"];
3564 ["write_file"; "/new"; "new file contents"; "0"];
3565 ["cat"; "/new"]], "new file contents")],
3566 "make ext2/3/4 external journal",
3568 This creates an ext2 external journal on C<device>. It is equivalent
3571 mke2fs -O journal_dev -b blocksize device");
3573 ("mke2journal_L", (RErr, [Int "blocksize"; String "label"; Device "device"]), 189, [],
3574 [InitEmpty, Always, TestOutput (
3575 [["sfdiskM"; "/dev/sda"; ",100 ,"];
3576 ["mke2journal_L"; "4096"; "JOURNAL"; "/dev/sda1"];
3577 ["mke2fs_JL"; "ext2"; "4096"; "/dev/sda2"; "JOURNAL"];
3578 ["mount"; "/dev/sda2"; "/"];
3579 ["write_file"; "/new"; "new file contents"; "0"];
3580 ["cat"; "/new"]], "new file contents")],
3581 "make ext2/3/4 external journal with label",
3583 This creates an ext2 external journal on C<device> with label C<label>.");
3585 ("mke2journal_U", (RErr, [Int "blocksize"; String "uuid"; Device "device"]), 190, [],
3586 (let uuid = uuidgen () in
3587 [InitEmpty, Always, TestOutput (
3588 [["sfdiskM"; "/dev/sda"; ",100 ,"];
3589 ["mke2journal_U"; "4096"; uuid; "/dev/sda1"];
3590 ["mke2fs_JU"; "ext2"; "4096"; "/dev/sda2"; uuid];
3591 ["mount"; "/dev/sda2"; "/"];
3592 ["write_file"; "/new"; "new file contents"; "0"];
3593 ["cat"; "/new"]], "new file contents")]),
3594 "make ext2/3/4 external journal with UUID",
3596 This creates an ext2 external journal on C<device> with UUID C<uuid>.");
3598 ("mke2fs_J", (RErr, [String "fstype"; Int "blocksize"; Device "device"; Device "journal"]), 191, [],
3600 "make ext2/3/4 filesystem with external journal",
3602 This creates an ext2/3/4 filesystem on C<device> with
3603 an external journal on C<journal>. It is equivalent
3606 mke2fs -t fstype -b blocksize -J device=<journal> <device>
3608 See also C<guestfs_mke2journal>.");
3610 ("mke2fs_JL", (RErr, [String "fstype"; Int "blocksize"; Device "device"; String "label"]), 192, [],
3612 "make ext2/3/4 filesystem with external journal",
3614 This creates an ext2/3/4 filesystem on C<device> with
3615 an external journal on the journal labeled C<label>.
3617 See also C<guestfs_mke2journal_L>.");
3619 ("mke2fs_JU", (RErr, [String "fstype"; Int "blocksize"; Device "device"; String "uuid"]), 193, [],
3621 "make ext2/3/4 filesystem with external journal",
3623 This creates an ext2/3/4 filesystem on C<device> with
3624 an external journal on the journal with UUID C<uuid>.
3626 See also C<guestfs_mke2journal_U>.");
3628 ("modprobe", (RErr, [String "modulename"]), 194, [],
3629 [InitNone, Always, TestRun [["modprobe"; "fat"]]],
3630 "load a kernel module",
3632 This loads a kernel module in the appliance.
3634 The kernel module must have been whitelisted when libguestfs
3635 was built (see C<appliance/kmod.whitelist.in> in the source).");
3637 ("echo_daemon", (RString "output", [StringList "words"]), 195, [],
3638 [InitNone, Always, TestOutput (
3639 [["echo_daemon"; "This is a test"]], "This is a test"
3641 "echo arguments back to the client",
3643 This command concatenate the list of C<words> passed with single spaces between
3644 them and returns the resulting string.
3646 You can use this command to test the connection through to the daemon.
3648 See also C<guestfs_ping_daemon>.");
3650 ("find0", (RErr, [Pathname "directory"; FileOut "files"]), 196, [],
3651 [], (* There is a regression test for this. *)
3652 "find all files and directories, returning NUL-separated list",
3654 This command lists out all files and directories, recursively,
3655 starting at C<directory>, placing the resulting list in the
3656 external file called C<files>.
3658 This command works the same way as C<guestfs_find> with the
3659 following exceptions:
3665 The resulting list is written to an external file.
3669 Items (filenames) in the result are separated
3670 by C<\\0> characters. See L<find(1)> option I<-print0>.
3674 This command is not limited in the number of names that it
3679 The result list is not sorted.
3683 ("case_sensitive_path", (RString "rpath", [Pathname "path"]), 197, [],
3684 [InitISOFS, Always, TestOutput (
3685 [["case_sensitive_path"; "/DIRECTORY"]], "/directory");
3686 InitISOFS, Always, TestOutput (
3687 [["case_sensitive_path"; "/DIRECTORY/"]], "/directory");
3688 InitISOFS, Always, TestOutput (
3689 [["case_sensitive_path"; "/Known-1"]], "/known-1");
3690 InitISOFS, Always, TestLastFail (
3691 [["case_sensitive_path"; "/Known-1/"]]);
3692 InitBasicFS, Always, TestOutput (
3694 ["mkdir"; "/a/bbb"];
3695 ["touch"; "/a/bbb/c"];
3696 ["case_sensitive_path"; "/A/bbB/C"]], "/a/bbb/c");
3697 InitBasicFS, Always, TestOutput (
3699 ["mkdir"; "/a/bbb"];
3700 ["touch"; "/a/bbb/c"];
3701 ["case_sensitive_path"; "/A////bbB/C"]], "/a/bbb/c");
3702 InitBasicFS, Always, TestLastFail (
3704 ["mkdir"; "/a/bbb"];
3705 ["touch"; "/a/bbb/c"];
3706 ["case_sensitive_path"; "/A/bbb/../bbb/C"]])],
3707 "return true path on case-insensitive filesystem",
3709 This can be used to resolve case insensitive paths on
3710 a filesystem which is case sensitive. The use case is
3711 to resolve paths which you have read from Windows configuration
3712 files or the Windows Registry, to the true path.
3714 The command handles a peculiarity of the Linux ntfs-3g
3715 filesystem driver (and probably others), which is that although
3716 the underlying filesystem is case-insensitive, the driver
3717 exports the filesystem to Linux as case-sensitive.
3719 One consequence of this is that special directories such
3720 as C<c:\\windows> may appear as C</WINDOWS> or C</windows>
3721 (or other things) depending on the precise details of how
3722 they were created. In Windows itself this would not be
3725 Bug or feature? You decide:
3726 L<http://www.tuxera.com/community/ntfs-3g-faq/#posixfilenames1>
3728 This function resolves the true case of each element in the
3729 path and returns the case-sensitive path.
3731 Thus C<guestfs_case_sensitive_path> (\"/Windows/System32\")
3732 might return C<\"/WINDOWS/system32\"> (the exact return value
3733 would depend on details of how the directories were originally
3734 created under Windows).
3737 This function does not handle drive names, backslashes etc.
3739 See also C<guestfs_realpath>.");
3741 ("vfs_type", (RString "fstype", [Device "device"]), 198, [],
3742 [InitBasicFS, Always, TestOutput (
3743 [["vfs_type"; "/dev/sda1"]], "ext2")],
3744 "get the Linux VFS type corresponding to a mounted device",
3746 This command gets the block device type corresponding to
3747 a mounted device called C<device>.
3749 Usually the result is the name of the Linux VFS module that
3750 is used to mount this device (probably determined automatically
3751 if you used the C<guestfs_mount> call).");
3753 ("truncate", (RErr, [Pathname "path"]), 199, [],
3754 [InitBasicFS, Always, TestOutputStruct (
3755 [["write_file"; "/test"; "some stuff so size is not zero"; "0"];
3756 ["truncate"; "/test"];
3757 ["stat"; "/test"]], [CompareWithInt ("size", 0)])],
3758 "truncate a file to zero size",
3760 This command truncates C<path> to a zero-length file. The
3761 file must exist already.");
3763 ("truncate_size", (RErr, [Pathname "path"; Int64 "size"]), 200, [],
3764 [InitBasicFS, Always, TestOutputStruct (
3765 [["touch"; "/test"];
3766 ["truncate_size"; "/test"; "1000"];
3767 ["stat"; "/test"]], [CompareWithInt ("size", 1000)])],
3768 "truncate a file to a particular size",
3770 This command truncates C<path> to size C<size> bytes. The file
3771 must exist already. If the file is smaller than C<size> then
3772 the file is extended to the required size with null bytes.");
3774 ("utimens", (RErr, [Pathname "path"; Int64 "atsecs"; Int64 "atnsecs"; Int64 "mtsecs"; Int64 "mtnsecs"]), 201, [],
3775 [InitBasicFS, Always, TestOutputStruct (
3776 [["touch"; "/test"];
3777 ["utimens"; "/test"; "12345"; "67890"; "9876"; "5432"];
3778 ["stat"; "/test"]], [CompareWithInt ("mtime", 9876)])],
3779 "set timestamp of a file with nanosecond precision",
3781 This command sets the timestamps of a file with nanosecond
3784 C<atsecs, atnsecs> are the last access time (atime) in secs and
3785 nanoseconds from the epoch.
3787 C<mtsecs, mtnsecs> are the last modification time (mtime) in
3788 secs and nanoseconds from the epoch.
3790 If the C<*nsecs> field contains the special value C<-1> then
3791 the corresponding timestamp is set to the current time. (The
3792 C<*secs> field is ignored in this case).
3794 If the C<*nsecs> field contains the special value C<-2> then
3795 the corresponding timestamp is left unchanged. (The
3796 C<*secs> field is ignored in this case).");
3798 ("mkdir_mode", (RErr, [Pathname "path"; Int "mode"]), 202, [],
3799 [InitBasicFS, Always, TestOutputStruct (
3800 [["mkdir_mode"; "/test"; "0o111"];
3801 ["stat"; "/test"]], [CompareWithInt ("mode", 0o40111)])],
3802 "create a directory with a particular mode",
3804 This command creates a directory, setting the initial permissions
3805 of the directory to C<mode>. See also C<guestfs_mkdir>.");
3807 ("lchown", (RErr, [Int "owner"; Int "group"; Pathname "path"]), 203, [],
3809 "change file owner and group",
3811 Change the file owner to C<owner> and group to C<group>.
3812 This is like C<guestfs_chown> but if C<path> is a symlink then
3813 the link itself is changed, not the target.
3815 Only numeric uid and gid are supported. If you want to use
3816 names, you will need to locate and parse the password file
3817 yourself (Augeas support makes this relatively easy).");
3819 ("lstatlist", (RStructList ("statbufs", "stat"), [Pathname "path"; StringList "names"]), 204, [],
3821 "lstat on multiple files",
3823 This call allows you to perform the C<guestfs_lstat> operation
3824 on multiple files, where all files are in the directory C<path>.
3825 C<names> is the list of files from this directory.
3827 On return you get a list of stat structs, with a one-to-one
3828 correspondence to the C<names> list. If any name did not exist
3829 or could not be lstat'd, then the C<ino> field of that structure
3832 This call is intended for programs that want to efficiently
3833 list a directory contents without making many round-trips.
3834 See also C<guestfs_lxattrlist> for a similarly efficient call
3835 for getting extended attributes. Very long directory listings
3836 might cause the protocol message size to be exceeded, causing
3837 this call to fail. The caller must split up such requests
3838 into smaller groups of names.");
3840 ("lxattrlist", (RStructList ("xattrs", "xattr"), [Pathname "path"; StringList "names"]), 205, [],
3842 "lgetxattr on multiple files",
3844 This call allows you to get the extended attributes
3845 of multiple files, where all files are in the directory C<path>.
3846 C<names> is the list of files from this directory.
3848 On return you get a flat list of xattr structs which must be
3849 interpreted sequentially. The first xattr struct always has a zero-length
3850 C<attrname>. C<attrval> in this struct is zero-length
3851 to indicate there was an error doing C<lgetxattr> for this
3852 file, I<or> is a C string which is a decimal number
3853 (the number of following attributes for this file, which could
3854 be C<\"0\">). Then after the first xattr struct are the
3855 zero or more attributes for the first named file.
3856 This repeats for the second and subsequent files.
3858 This call is intended for programs that want to efficiently
3859 list a directory contents without making many round-trips.
3860 See also C<guestfs_lstatlist> for a similarly efficient call
3861 for getting standard stats. Very long directory listings
3862 might cause the protocol message size to be exceeded, causing
3863 this call to fail. The caller must split up such requests
3864 into smaller groups of names.");
3866 ("readlinklist", (RStringList "links", [Pathname "path"; StringList "names"]), 206, [],
3868 "readlink on multiple files",
3870 This call allows you to do a C<readlink> operation
3871 on multiple files, where all files are in the directory C<path>.
3872 C<names> is the list of files from this directory.
3874 On return you get a list of strings, with a one-to-one
3875 correspondence to the C<names> list. Each string is the
3876 value of the symbol link.
3878 If the C<readlink(2)> operation fails on any name, then
3879 the corresponding result string is the empty string C<\"\">.
3880 However the whole operation is completed even if there
3881 were C<readlink(2)> errors, and so you can call this
3882 function with names where you don't know if they are
3883 symbolic links already (albeit slightly less efficient).
3885 This call is intended for programs that want to efficiently
3886 list a directory contents without making many round-trips.
3887 Very long directory listings might cause the protocol
3888 message size to be exceeded, causing
3889 this call to fail. The caller must split up such requests
3890 into smaller groups of names.");
3892 ("pread", (RBufferOut "content", [Pathname "path"; Int "count"; Int64 "offset"]), 207, [ProtocolLimitWarning],
3893 [InitISOFS, Always, TestOutputBuffer (
3894 [["pread"; "/known-4"; "1"; "3"]], "\n");
3895 InitISOFS, Always, TestOutputBuffer (
3896 [["pread"; "/empty"; "0"; "100"]], "")],
3897 "read part of a file",
3899 This command lets you read part of a file. It reads C<count>
3900 bytes of the file, starting at C<offset>, from file C<path>.
3902 This may read fewer bytes than requested. For further details
3903 see the L<pread(2)> system call.");
3905 ("part_init", (RErr, [Device "device"; String "parttype"]), 208, [],
3906 [InitEmpty, Always, TestRun (
3907 [["part_init"; "/dev/sda"; "gpt"]])],
3908 "create an empty partition table",
3910 This creates an empty partition table on C<device> of one of the
3911 partition types listed below. Usually C<parttype> should be
3912 either C<msdos> or C<gpt> (for large disks).
3914 Initially there are no partitions. Following this, you should
3915 call C<guestfs_part_add> for each partition required.
3917 Possible values for C<parttype> are:
3921 =item B<efi> | B<gpt>
3923 Intel EFI / GPT partition table.
3925 This is recommended for >= 2 TB partitions that will be accessed
3926 from Linux and Intel-based Mac OS X. It also has limited backwards
3927 compatibility with the C<mbr> format.
3929 =item B<mbr> | B<msdos>
3931 The standard PC \"Master Boot Record\" (MBR) format used
3932 by MS-DOS and Windows. This partition type will B<only> work
3933 for device sizes up to 2 TB. For large disks we recommend
3938 Other partition table types that may work but are not
3947 =item B<amiga> | B<rdb>
3949 Amiga \"Rigid Disk Block\" format.
3957 DASD, used on IBM mainframes.
3965 Old Mac partition format. Modern Macs use C<gpt>.
3969 NEC PC-98 format, common in Japan apparently.
3977 ("part_add", (RErr, [Device "device"; String "prlogex"; Int64 "startsect"; Int64 "endsect"]), 209, [],
3978 [InitEmpty, Always, TestRun (
3979 [["part_init"; "/dev/sda"; "mbr"];
3980 ["part_add"; "/dev/sda"; "primary"; "1"; "-1"]]);
3981 InitEmpty, Always, TestRun (
3982 [["part_init"; "/dev/sda"; "gpt"];
3983 ["part_add"; "/dev/sda"; "primary"; "34"; "127"];
3984 ["part_add"; "/dev/sda"; "primary"; "128"; "-34"]]);
3985 InitEmpty, Always, TestRun (
3986 [["part_init"; "/dev/sda"; "mbr"];
3987 ["part_add"; "/dev/sda"; "primary"; "32"; "127"];
3988 ["part_add"; "/dev/sda"; "primary"; "128"; "255"];
3989 ["part_add"; "/dev/sda"; "primary"; "256"; "511"];
3990 ["part_add"; "/dev/sda"; "primary"; "512"; "-1"]])],
3991 "add a partition to the device",
3993 This command adds a partition to C<device>. If there is no partition
3994 table on the device, call C<guestfs_part_init> first.
3996 The C<prlogex> parameter is the type of partition. Normally you
3997 should pass C<p> or C<primary> here, but MBR partition tables also
3998 support C<l> (or C<logical>) and C<e> (or C<extended>) partition
4001 C<startsect> and C<endsect> are the start and end of the partition
4002 in I<sectors>. C<endsect> may be negative, which means it counts
4003 backwards from the end of the disk (C<-1> is the last sector).
4005 Creating a partition which covers the whole disk is not so easy.
4006 Use C<guestfs_part_disk> to do that.");
4008 ("part_disk", (RErr, [Device "device"; String "parttype"]), 210, [DangerWillRobinson],
4009 [InitEmpty, Always, TestRun (
4010 [["part_disk"; "/dev/sda"; "mbr"]]);
4011 InitEmpty, Always, TestRun (
4012 [["part_disk"; "/dev/sda"; "gpt"]])],
4013 "partition whole disk with a single primary partition",
4015 This command is simply a combination of C<guestfs_part_init>
4016 followed by C<guestfs_part_add> to create a single primary partition
4017 covering the whole disk.
4019 C<parttype> is the partition table type, usually C<mbr> or C<gpt>,
4020 but other possible values are described in C<guestfs_part_init>.");
4022 ("part_set_bootable", (RErr, [Device "device"; Int "partnum"; Bool "bootable"]), 211, [],
4023 [InitEmpty, Always, TestRun (
4024 [["part_disk"; "/dev/sda"; "mbr"];
4025 ["part_set_bootable"; "/dev/sda"; "1"; "true"]])],
4026 "make a partition bootable",
4028 This sets the bootable flag on partition numbered C<partnum> on
4029 device C<device>. Note that partitions are numbered from 1.
4031 The bootable flag is used by some PC BIOSes to determine which
4032 partition to boot from. It is by no means universally recognized,
4033 and in any case if your operating system installed a boot
4034 sector on the device itself, then that takes precedence.");
4036 ("part_set_name", (RErr, [Device "device"; Int "partnum"; String "name"]), 212, [],
4037 [InitEmpty, Always, TestRun (
4038 [["part_disk"; "/dev/sda"; "gpt"];
4039 ["part_set_name"; "/dev/sda"; "1"; "thepartname"]])],
4040 "set partition name",
4042 This sets the partition name on partition numbered C<partnum> on
4043 device C<device>. Note that partitions are numbered from 1.
4045 The partition name can only be set on certain types of partition
4046 table. This works on C<gpt> but not on C<mbr> partitions.");
4048 ("part_list", (RStructList ("partitions", "partition"), [Device "device"]), 213, [],
4049 [], (* XXX Add a regression test for this. *)
4050 "list partitions on a device",
4052 This command parses the partition table on C<device> and
4053 returns the list of partitions found.
4055 The fields in the returned structure are:
4061 Partition number, counting from 1.
4065 Start of the partition I<in bytes>. To get sectors you have to
4066 divide by the device's sector size, see C<guestfs_blockdev_getss>.
4070 End of the partition in bytes.
4074 Size of the partition in bytes.
4078 ("part_get_parttype", (RString "parttype", [Device "device"]), 214, [],
4079 [InitEmpty, Always, TestOutput (
4080 [["part_disk"; "/dev/sda"; "gpt"];
4081 ["part_get_parttype"; "/dev/sda"]], "gpt")],
4082 "get the partition table type",
4084 This command examines the partition table on C<device> and
4085 returns the partition table type (format) being used.
4087 Common return values include: C<msdos> (a DOS/Windows style MBR
4088 partition table), C<gpt> (a GPT/EFI-style partition table). Other
4089 values are possible, although unusual. See C<guestfs_part_init>
4092 ("fill", (RErr, [Int "c"; Int "len"; Pathname "path"]), 215, [],
4093 [InitBasicFS, Always, TestOutputBuffer (
4094 [["fill"; "0x63"; "10"; "/test"];
4095 ["read_file"; "/test"]], "cccccccccc")],
4096 "fill a file with octets",
4098 This command creates a new file called C<path>. The initial
4099 content of the file is C<len> octets of C<c>, where C<c>
4100 must be a number in the range C<[0..255]>.
4102 To fill a file with zero bytes (sparsely), it is
4103 much more efficient to use C<guestfs_truncate_size>.");
4107 let all_functions = non_daemon_functions @ daemon_functions
4109 (* In some places we want the functions to be displayed sorted
4110 * alphabetically, so this is useful:
4112 let all_functions_sorted =
4113 List.sort (fun (n1,_,_,_,_,_,_) (n2,_,_,_,_,_,_) ->
4114 compare n1 n2) all_functions
4116 (* Field types for structures. *)
4118 | FChar (* C 'char' (really, a 7 bit byte). *)
4119 | FString (* nul-terminated ASCII string, NOT NULL. *)
4120 | FBuffer (* opaque buffer of bytes, (char *, int) pair *)
4125 | FBytes (* Any int measure that counts bytes. *)
4126 | FUUID (* 32 bytes long, NOT nul-terminated. *)
4127 | FOptPercent (* [0..100], or -1 meaning "not present". *)
4129 (* Because we generate extra parsing code for LVM command line tools,
4130 * we have to pull out the LVM columns separately here.
4140 "pv_attr", FString (* XXX *);
4141 "pv_pe_count", FInt64;
4142 "pv_pe_alloc_count", FInt64;
4145 "pv_mda_count", FInt64;
4146 "pv_mda_free", FBytes;
4147 (* Not in Fedora 10:
4148 "pv_mda_size", FBytes;
4155 "vg_attr", FString (* XXX *);
4158 "vg_sysid", FString;
4159 "vg_extent_size", FBytes;
4160 "vg_extent_count", FInt64;
4161 "vg_free_count", FInt64;
4166 "snap_count", FInt64;
4169 "vg_mda_count", FInt64;
4170 "vg_mda_free", FBytes;
4171 (* Not in Fedora 10:
4172 "vg_mda_size", FBytes;
4178 "lv_attr", FString (* XXX *);
4181 "lv_kernel_major", FInt64;
4182 "lv_kernel_minor", FInt64;
4184 "seg_count", FInt64;
4186 "snap_percent", FOptPercent;
4187 "copy_percent", FOptPercent;
4190 "mirror_log", FString;
4194 (* Names and fields in all structures (in RStruct and RStructList)
4198 (* The old RIntBool return type, only ever used for aug_defnode. Do
4199 * not use this struct in any new code.
4202 "i", FInt32; (* for historical compatibility *)
4203 "b", FInt32; (* for historical compatibility *)
4206 (* LVM PVs, VGs, LVs. *)
4207 "lvm_pv", lvm_pv_cols;
4208 "lvm_vg", lvm_vg_cols;
4209 "lvm_lv", lvm_lv_cols;
4211 (* Column names and types from stat structures.
4212 * NB. Can't use things like 'st_atime' because glibc header files
4213 * define some of these as macros. Ugh.
4244 (* Column names in dirent structure. *)
4247 (* 'b' 'c' 'd' 'f' (FIFO) 'l' 'r' (regular file) 's' 'u' '?' *)
4252 (* Version numbers. *)
4260 (* Extended attribute. *)
4262 "attrname", FString;
4266 (* Inotify events. *)
4270 "in_cookie", FUInt32;
4274 (* Partition table entry. *)
4277 "part_start", FBytes;
4279 "part_size", FBytes;
4281 ] (* end of structs *)
4283 (* Ugh, Java has to be different ..
4284 * These names are also used by the Haskell bindings.
4286 let java_structs = [
4287 "int_bool", "IntBool";
4292 "statvfs", "StatVFS";
4294 "version", "Version";
4296 "inotify_event", "INotifyEvent";
4297 "partition", "Partition";
4300 (* What structs are actually returned. *)
4301 type rstructs_used_t = RStructOnly | RStructListOnly | RStructAndList
4303 (* Returns a list of RStruct/RStructList structs that are returned
4304 * by any function. Each element of returned list is a pair:
4306 * (structname, RStructOnly)
4307 * == there exists function which returns RStruct (_, structname)
4308 * (structname, RStructListOnly)
4309 * == there exists function which returns RStructList (_, structname)
4310 * (structname, RStructAndList)
4311 * == there are functions returning both RStruct (_, structname)
4312 * and RStructList (_, structname)
4314 let rstructs_used_by functions =
4315 (* ||| is a "logical OR" for rstructs_used_t *)
4319 | _, RStructAndList -> RStructAndList
4320 | RStructOnly, RStructListOnly
4321 | RStructListOnly, RStructOnly -> RStructAndList
4322 | RStructOnly, RStructOnly -> RStructOnly
4323 | RStructListOnly, RStructListOnly -> RStructListOnly
4326 let h = Hashtbl.create 13 in
4328 (* if elem->oldv exists, update entry using ||| operator,
4329 * else just add elem->newv to the hash
4331 let update elem newv =
4332 try let oldv = Hashtbl.find h elem in
4333 Hashtbl.replace h elem (newv ||| oldv)
4334 with Not_found -> Hashtbl.add h elem newv
4338 fun (_, style, _, _, _, _, _) ->
4339 match fst style with
4340 | RStruct (_, structname) -> update structname RStructOnly
4341 | RStructList (_, structname) -> update structname RStructListOnly
4345 (* return key->values as a list of (key,value) *)
4346 Hashtbl.fold (fun key value xs -> (key, value) :: xs) h []
4348 (* Used for testing language bindings. *)
4350 | CallString of string
4351 | CallOptString of string option
4352 | CallStringList of string list
4354 | CallInt64 of int64
4357 (* Used to memoize the result of pod2text. *)
4358 let pod2text_memo_filename = "src/.pod2text.data"
4359 let pod2text_memo : ((int * string * string), string list) Hashtbl.t =
4361 let chan = open_in pod2text_memo_filename in
4362 let v = input_value chan in
4366 _ -> Hashtbl.create 13
4367 let pod2text_memo_updated () =
4368 let chan = open_out pod2text_memo_filename in
4369 output_value chan pod2text_memo;
4372 (* Useful functions.
4373 * Note we don't want to use any external OCaml libraries which
4374 * makes this a bit harder than it should be.
4376 let failwithf fs = ksprintf failwith fs
4378 let replace_char s c1 c2 =
4379 let s2 = String.copy s in
4380 let r = ref false in
4381 for i = 0 to String.length s2 - 1 do
4382 if String.unsafe_get s2 i = c1 then (
4383 String.unsafe_set s2 i c2;
4387 if not !r then s else s2
4391 (* || c = '\f' *) || c = '\n' || c = '\r' || c = '\t' (* || c = '\v' *)
4393 let triml ?(test = isspace) str =
4395 let n = ref (String.length str) in
4396 while !n > 0 && test str.[!i]; do
4401 else String.sub str !i !n
4403 let trimr ?(test = isspace) str =
4404 let n = ref (String.length str) in
4405 while !n > 0 && test str.[!n-1]; do
4408 if !n = String.length str then str
4409 else String.sub str 0 !n
4411 let trim ?(test = isspace) str =
4412 trimr ~test (triml ~test str)
4414 let rec find s sub =
4415 let len = String.length s in
4416 let sublen = String.length sub in
4418 if i <= len-sublen then (
4420 if j < sublen then (
4421 if s.[i+j] = sub.[j] then loop2 (j+1)
4427 if r = -1 then loop (i+1) else r
4433 let rec replace_str s s1 s2 =
4434 let len = String.length s in
4435 let sublen = String.length s1 in
4436 let i = find s s1 in
4439 let s' = String.sub s 0 i in
4440 let s'' = String.sub s (i+sublen) (len-i-sublen) in
4441 s' ^ s2 ^ replace_str s'' s1 s2
4444 let rec string_split sep str =
4445 let len = String.length str in
4446 let seplen = String.length sep in
4447 let i = find str sep in
4448 if i = -1 then [str]
4450 let s' = String.sub str 0 i in
4451 let s'' = String.sub str (i+seplen) (len-i-seplen) in
4452 s' :: string_split sep s''
4455 let files_equal n1 n2 =
4456 let cmd = sprintf "cmp -s %s %s" (Filename.quote n1) (Filename.quote n2) in
4457 match Sys.command cmd with
4460 | i -> failwithf "%s: failed with error code %d" cmd i
4462 let rec filter_map f = function
4466 | Some y -> y :: filter_map f xs
4467 | None -> filter_map f xs
4469 let rec find_map f = function
4470 | [] -> raise Not_found
4474 | None -> find_map f xs
4477 let rec loop i = function
4479 | x :: xs -> f i x; loop (i+1) xs
4484 let rec loop i = function
4486 | x :: xs -> let r = f i x in r :: loop (i+1) xs
4490 let name_of_argt = function
4491 | Pathname n | Device n | Dev_or_Path n | String n | OptString n
4492 | StringList n | DeviceList n | Bool n | Int n | Int64 n
4493 | FileIn n | FileOut n -> n
4495 let java_name_of_struct typ =
4496 try List.assoc typ java_structs
4499 "java_name_of_struct: no java_structs entry corresponding to %s" typ
4501 let cols_of_struct typ =
4502 try List.assoc typ structs
4504 failwithf "cols_of_struct: unknown struct %s" typ
4506 let seq_of_test = function
4507 | TestRun s | TestOutput (s, _) | TestOutputList (s, _)
4508 | TestOutputListOfDevices (s, _)
4509 | TestOutputInt (s, _) | TestOutputIntOp (s, _, _)
4510 | TestOutputTrue s | TestOutputFalse s
4511 | TestOutputLength (s, _) | TestOutputBuffer (s, _)
4512 | TestOutputStruct (s, _)
4513 | TestLastFail s -> s
4515 (* Handling for function flags. *)
4516 let protocol_limit_warning =
4517 "Because of the message protocol, there is a transfer limit
4518 of somewhere between 2MB and 4MB. To transfer large files you should use
4521 let danger_will_robinson =
4522 "B<This command is dangerous. Without careful use you
4523 can easily destroy all your data>."
4525 let deprecation_notice flags =
4528 find_map (function DeprecatedBy str -> Some str | _ -> None) flags in
4530 sprintf "This function is deprecated.
4531 In new code, use the C<%s> call instead.
4533 Deprecated functions will not be removed from the API, but the
4534 fact that they are deprecated indicates that there are problems
4535 with correct use of these functions." alt in
4540 (* Check function names etc. for consistency. *)
4541 let check_functions () =
4542 let contains_uppercase str =
4543 let len = String.length str in
4545 if i >= len then false
4548 if c >= 'A' && c <= 'Z' then true
4555 (* Check function names. *)
4557 fun (name, _, _, _, _, _, _) ->
4558 if String.length name >= 7 && String.sub name 0 7 = "guestfs" then
4559 failwithf "function name %s does not need 'guestfs' prefix" name;
4561 failwithf "function name is empty";
4562 if name.[0] < 'a' || name.[0] > 'z' then
4563 failwithf "function name %s must start with lowercase a-z" name;
4564 if String.contains name '-' then
4565 failwithf "function name %s should not contain '-', use '_' instead."
4569 (* Check function parameter/return names. *)
4571 fun (name, style, _, _, _, _, _) ->
4572 let check_arg_ret_name n =
4573 if contains_uppercase n then
4574 failwithf "%s param/ret %s should not contain uppercase chars"
4576 if String.contains n '-' || String.contains n '_' then
4577 failwithf "%s param/ret %s should not contain '-' or '_'"
4580 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;
4581 if n = "int" || n = "char" || n = "short" || n = "long" then
4582 failwithf "%s has a param/ret which conflicts with a C type (eg. 'int', 'char' etc.)" name;
4583 if n = "i" || n = "n" then
4584 failwithf "%s has a param/ret called 'i' or 'n', which will cause some conflicts in the generated code" name;
4585 if n = "argv" || n = "args" then
4586 failwithf "%s has a param/ret called 'argv' or 'args', which will cause some conflicts in the generated code" name;
4588 (* List Haskell, OCaml and C keywords here.
4589 * http://www.haskell.org/haskellwiki/Keywords
4590 * http://caml.inria.fr/pub/docs/manual-ocaml/lex.html#operator-char
4591 * http://en.wikipedia.org/wiki/C_syntax#Reserved_keywords
4592 * Formatted via: cat c haskell ocaml|sort -u|grep -vE '_|^val$' \
4593 * |perl -pe 's/(.+)/"$1";/'|fmt -70
4594 * Omitting _-containing words, since they're handled above.
4595 * Omitting the OCaml reserved word, "val", is ok,
4596 * and saves us from renaming several parameters.
4599 "and"; "as"; "asr"; "assert"; "auto"; "begin"; "break"; "case";
4600 "char"; "class"; "const"; "constraint"; "continue"; "data";
4601 "default"; "deriving"; "do"; "done"; "double"; "downto"; "else";
4602 "end"; "enum"; "exception"; "extern"; "external"; "false"; "float";
4603 "for"; "forall"; "foreign"; "fun"; "function"; "functor"; "goto";
4604 "hiding"; "if"; "import"; "in"; "include"; "infix"; "infixl";
4605 "infixr"; "inherit"; "initializer"; "inline"; "instance"; "int";
4606 "land"; "lazy"; "let"; "long"; "lor"; "lsl"; "lsr"; "lxor";
4607 "match"; "mdo"; "method"; "mod"; "module"; "mutable"; "new";
4608 "newtype"; "object"; "of"; "open"; "or"; "private"; "qualified";
4609 "rec"; "register"; "restrict"; "return"; "short"; "sig"; "signed";
4610 "sizeof"; "static"; "struct"; "switch"; "then"; "to"; "true"; "try";
4611 "type"; "typedef"; "union"; "unsigned"; "virtual"; "void";
4612 "volatile"; "when"; "where"; "while";
4614 if List.mem n reserved then
4615 failwithf "%s has param/ret using reserved word %s" name n;
4618 (match fst style with
4620 | RInt n | RInt64 n | RBool n
4621 | RConstString n | RConstOptString n | RString n
4622 | RStringList n | RStruct (n, _) | RStructList (n, _)
4623 | RHashtable n | RBufferOut n ->
4624 check_arg_ret_name n
4626 List.iter (fun arg -> check_arg_ret_name (name_of_argt arg)) (snd style)
4629 (* Check short descriptions. *)
4631 fun (name, _, _, _, _, shortdesc, _) ->
4632 if shortdesc.[0] <> Char.lowercase shortdesc.[0] then
4633 failwithf "short description of %s should begin with lowercase." name;
4634 let c = shortdesc.[String.length shortdesc-1] in
4635 if c = '\n' || c = '.' then
4636 failwithf "short description of %s should not end with . or \\n." name
4639 (* Check long dscriptions. *)
4641 fun (name, _, _, _, _, _, longdesc) ->
4642 if longdesc.[String.length longdesc-1] = '\n' then
4643 failwithf "long description of %s should not end with \\n." name
4646 (* Check proc_nrs. *)
4648 fun (name, _, proc_nr, _, _, _, _) ->
4649 if proc_nr <= 0 then
4650 failwithf "daemon function %s should have proc_nr > 0" name
4654 fun (name, _, proc_nr, _, _, _, _) ->
4655 if proc_nr <> -1 then
4656 failwithf "non-daemon function %s should have proc_nr -1" name
4657 ) non_daemon_functions;
4660 List.map (fun (name, _, proc_nr, _, _, _, _) -> name, proc_nr)
4663 List.sort (fun (_,nr1) (_,nr2) -> compare nr1 nr2) proc_nrs in
4664 let rec loop = function
4667 | (name1,nr1) :: ((name2,nr2) :: _ as rest) when nr1 < nr2 ->
4669 | (name1,nr1) :: (name2,nr2) :: _ ->
4670 failwithf "%s and %s have conflicting procedure numbers (%d, %d)"
4678 (* Ignore functions that have no tests. We generate a
4679 * warning when the user does 'make check' instead.
4681 | name, _, _, _, [], _, _ -> ()
4682 | name, _, _, _, tests, _, _ ->
4686 match seq_of_test test with
4688 failwithf "%s has a test containing an empty sequence" name
4689 | cmds -> List.map List.hd cmds
4691 let funcs = List.flatten funcs in
4693 let tested = List.mem name funcs in
4696 failwithf "function %s has tests but does not test itself" name
4699 (* 'pr' prints to the current output file. *)
4700 let chan = ref Pervasives.stdout
4701 let pr fs = ksprintf (output_string !chan) fs
4703 (* Generate a header block in a number of standard styles. *)
4704 type comment_style = CStyle | HashStyle | OCamlStyle | HaskellStyle
4705 type license = GPLv2 | LGPLv2
4707 let generate_header comment license =
4708 let c = match comment with
4709 | CStyle -> pr "/* "; " *"
4710 | HashStyle -> pr "# "; "#"
4711 | OCamlStyle -> pr "(* "; " *"
4712 | HaskellStyle -> pr "{- "; " " in
4713 pr "libguestfs generated file\n";
4714 pr "%s WARNING: THIS FILE IS GENERATED BY 'src/generator.ml'.\n" c;
4715 pr "%s ANY CHANGES YOU MAKE TO THIS FILE WILL BE LOST.\n" c;
4717 pr "%s Copyright (C) 2009 Red Hat Inc.\n" c;
4721 pr "%s This program is free software; you can redistribute it and/or modify\n" c;
4722 pr "%s it under the terms of the GNU General Public License as published by\n" c;
4723 pr "%s the Free Software Foundation; either version 2 of the License, or\n" c;
4724 pr "%s (at your option) any later version.\n" c;
4726 pr "%s This program is distributed in the hope that it will be useful,\n" c;
4727 pr "%s but WITHOUT ANY WARRANTY; without even the implied warranty of\n" c;
4728 pr "%s MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" c;
4729 pr "%s GNU General Public License for more details.\n" c;
4731 pr "%s You should have received a copy of the GNU General Public License along\n" c;
4732 pr "%s with this program; if not, write to the Free Software Foundation, Inc.,\n" c;
4733 pr "%s 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\n" c;
4736 pr "%s This library is free software; you can redistribute it and/or\n" c;
4737 pr "%s modify it under the terms of the GNU Lesser General Public\n" c;
4738 pr "%s License as published by the Free Software Foundation; either\n" c;
4739 pr "%s version 2 of the License, or (at your option) any later version.\n" c;
4741 pr "%s This library is distributed in the hope that it will be useful,\n" c;
4742 pr "%s but WITHOUT ANY WARRANTY; without even the implied warranty of\n" c;
4743 pr "%s MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n" c;
4744 pr "%s Lesser General Public License for more details.\n" c;
4746 pr "%s You should have received a copy of the GNU Lesser General Public\n" c;
4747 pr "%s License along with this library; if not, write to the Free Software\n" c;
4748 pr "%s Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n" c;
4751 | CStyle -> pr " */\n"
4753 | OCamlStyle -> pr " *)\n"
4754 | HaskellStyle -> pr "-}\n"
4758 (* Start of main code generation functions below this line. *)
4760 (* Generate the pod documentation for the C API. *)
4761 let rec generate_actions_pod () =
4763 fun (shortname, style, _, flags, _, _, longdesc) ->
4764 if not (List.mem NotInDocs flags) then (
4765 let name = "guestfs_" ^ shortname in
4766 pr "=head2 %s\n\n" name;
4768 generate_prototype ~extern:false ~handle:"handle" name style;
4770 pr "%s\n\n" longdesc;
4771 (match fst style with
4773 pr "This function returns 0 on success or -1 on error.\n\n"
4775 pr "On error this function returns -1.\n\n"
4777 pr "On error this function returns -1.\n\n"
4779 pr "This function returns a C truth value on success or -1 on error.\n\n"
4781 pr "This function returns a string, or NULL on error.
4782 The string is owned by the guest handle and must I<not> be freed.\n\n"
4783 | RConstOptString _ ->
4784 pr "This function returns a string which may be NULL.
4785 There is way to return an error from this function.
4786 The string is owned by the guest handle and must I<not> be freed.\n\n"
4788 pr "This function returns a string, or NULL on error.
4789 I<The caller must free the returned string after use>.\n\n"
4791 pr "This function returns a NULL-terminated array of strings
4792 (like L<environ(3)>), or NULL if there was an error.
4793 I<The caller must free the strings and the array after use>.\n\n"
4794 | RStruct (_, typ) ->
4795 pr "This function returns a C<struct guestfs_%s *>,
4796 or NULL if there was an error.
4797 I<The caller must call C<guestfs_free_%s> after use>.\n\n" typ typ
4798 | RStructList (_, typ) ->
4799 pr "This function returns a C<struct guestfs_%s_list *>
4800 (see E<lt>guestfs-structs.hE<gt>),
4801 or NULL if there was an error.
4802 I<The caller must call C<guestfs_free_%s_list> after use>.\n\n" typ typ
4804 pr "This function returns a NULL-terminated array of
4805 strings, or NULL if there was an error.
4806 The array of strings will always have length C<2n+1>, where
4807 C<n> keys and values alternate, followed by the trailing NULL entry.
4808 I<The caller must free the strings and the array after use>.\n\n"
4810 pr "This function returns a buffer, or NULL on error.
4811 The size of the returned buffer is written to C<*size_r>.
4812 I<The caller must free the returned buffer after use>.\n\n"
4814 if List.mem ProtocolLimitWarning flags then
4815 pr "%s\n\n" protocol_limit_warning;
4816 if List.mem DangerWillRobinson flags then
4817 pr "%s\n\n" danger_will_robinson;
4818 match deprecation_notice flags with
4820 | Some txt -> pr "%s\n\n" txt
4822 ) all_functions_sorted
4824 and generate_structs_pod () =
4825 (* Structs documentation. *)
4828 pr "=head2 guestfs_%s\n" typ;
4830 pr " struct guestfs_%s {\n" typ;
4833 | name, FChar -> pr " char %s;\n" name
4834 | name, FUInt32 -> pr " uint32_t %s;\n" name
4835 | name, FInt32 -> pr " int32_t %s;\n" name
4836 | name, (FUInt64|FBytes) -> pr " uint64_t %s;\n" name
4837 | name, FInt64 -> pr " int64_t %s;\n" name
4838 | name, FString -> pr " char *%s;\n" name
4840 pr " /* The next two fields describe a byte array. */\n";
4841 pr " uint32_t %s_len;\n" name;
4842 pr " char *%s;\n" name
4844 pr " /* The next field is NOT nul-terminated, be careful when printing it: */\n";
4845 pr " char %s[32];\n" name
4846 | name, FOptPercent ->
4847 pr " /* The next field is [0..100] or -1 meaning 'not present': */\n";
4848 pr " float %s;\n" name
4852 pr " struct guestfs_%s_list {\n" typ;
4853 pr " uint32_t len; /* Number of elements in list. */\n";
4854 pr " struct guestfs_%s *val; /* Elements. */\n" typ;
4857 pr " void guestfs_free_%s (struct guestfs_free_%s *);\n" typ typ;
4858 pr " void guestfs_free_%s_list (struct guestfs_free_%s_list *);\n"
4863 (* Generate the protocol (XDR) file, 'guestfs_protocol.x' and
4864 * indirectly 'guestfs_protocol.h' and 'guestfs_protocol.c'.
4866 * We have to use an underscore instead of a dash because otherwise
4867 * rpcgen generates incorrect code.
4869 * This header is NOT exported to clients, but see also generate_structs_h.
4871 and generate_xdr () =
4872 generate_header CStyle LGPLv2;
4874 (* This has to be defined to get around a limitation in Sun's rpcgen. *)
4875 pr "typedef string str<>;\n";
4878 (* Internal structures. *)
4882 pr "struct guestfs_int_%s {\n" typ;
4884 | name, FChar -> pr " char %s;\n" name
4885 | name, FString -> pr " string %s<>;\n" name
4886 | name, FBuffer -> pr " opaque %s<>;\n" name
4887 | name, FUUID -> pr " opaque %s[32];\n" name
4888 | name, (FInt32|FUInt32) -> pr " int %s;\n" name
4889 | name, (FInt64|FUInt64|FBytes) -> pr " hyper %s;\n" name
4890 | name, FOptPercent -> pr " float %s;\n" name
4894 pr "typedef struct guestfs_int_%s guestfs_int_%s_list<>;\n" typ typ;
4899 fun (shortname, style, _, _, _, _, _) ->
4900 let name = "guestfs_" ^ shortname in
4902 (match snd style with
4905 pr "struct %s_args {\n" name;
4908 | Pathname n | Device n | Dev_or_Path n | String n ->
4909 pr " string %s<>;\n" n
4910 | OptString n -> pr " str *%s;\n" n
4911 | StringList n | DeviceList n -> pr " str %s<>;\n" n
4912 | Bool n -> pr " bool %s;\n" n
4913 | Int n -> pr " int %s;\n" n
4914 | Int64 n -> pr " hyper %s;\n" n
4915 | FileIn _ | FileOut _ -> ()
4919 (match fst style with
4922 pr "struct %s_ret {\n" name;
4926 pr "struct %s_ret {\n" name;
4927 pr " hyper %s;\n" n;
4930 pr "struct %s_ret {\n" name;
4933 | RConstString _ | RConstOptString _ ->
4934 failwithf "RConstString|RConstOptString cannot be used by daemon functions"
4936 pr "struct %s_ret {\n" name;
4937 pr " string %s<>;\n" n;
4940 pr "struct %s_ret {\n" name;
4941 pr " str %s<>;\n" n;
4943 | RStruct (n, typ) ->
4944 pr "struct %s_ret {\n" name;
4945 pr " guestfs_int_%s %s;\n" typ n;
4947 | RStructList (n, typ) ->
4948 pr "struct %s_ret {\n" name;
4949 pr " guestfs_int_%s_list %s;\n" typ n;
4952 pr "struct %s_ret {\n" name;
4953 pr " str %s<>;\n" n;
4956 pr "struct %s_ret {\n" name;
4957 pr " opaque %s<>;\n" n;
4962 (* Table of procedure numbers. *)
4963 pr "enum guestfs_procedure {\n";
4965 fun (shortname, _, proc_nr, _, _, _, _) ->
4966 pr " GUESTFS_PROC_%s = %d,\n" (String.uppercase shortname) proc_nr
4968 pr " GUESTFS_PROC_NR_PROCS\n";
4972 (* Having to choose a maximum message size is annoying for several
4973 * reasons (it limits what we can do in the API), but it (a) makes
4974 * the protocol a lot simpler, and (b) provides a bound on the size
4975 * of the daemon which operates in limited memory space. For large
4976 * file transfers you should use FTP.
4978 pr "const GUESTFS_MESSAGE_MAX = %d;\n" (4 * 1024 * 1024);
4981 (* Message header, etc. *)
4983 /* The communication protocol is now documented in the guestfs(3)
4987 const GUESTFS_PROGRAM = 0x2000F5F5;
4988 const GUESTFS_PROTOCOL_VERSION = 1;
4990 /* These constants must be larger than any possible message length. */
4991 const GUESTFS_LAUNCH_FLAG = 0xf5f55ff5;
4992 const GUESTFS_CANCEL_FLAG = 0xffffeeee;
4994 enum guestfs_message_direction {
4995 GUESTFS_DIRECTION_CALL = 0, /* client -> daemon */
4996 GUESTFS_DIRECTION_REPLY = 1 /* daemon -> client */
4999 enum guestfs_message_status {
5000 GUESTFS_STATUS_OK = 0,
5001 GUESTFS_STATUS_ERROR = 1
5004 const GUESTFS_ERROR_LEN = 256;
5006 struct guestfs_message_error {
5007 string error_message<GUESTFS_ERROR_LEN>;
5010 struct guestfs_message_header {
5011 unsigned prog; /* GUESTFS_PROGRAM */
5012 unsigned vers; /* GUESTFS_PROTOCOL_VERSION */
5013 guestfs_procedure proc; /* GUESTFS_PROC_x */
5014 guestfs_message_direction direction;
5015 unsigned serial; /* message serial number */
5016 guestfs_message_status status;
5019 const GUESTFS_MAX_CHUNK_SIZE = 8192;
5021 struct guestfs_chunk {
5022 int cancel; /* if non-zero, transfer is cancelled */
5023 /* data size is 0 bytes if the transfer has finished successfully */
5024 opaque data<GUESTFS_MAX_CHUNK_SIZE>;
5028 (* Generate the guestfs-structs.h file. *)
5029 and generate_structs_h () =
5030 generate_header CStyle LGPLv2;
5032 (* This is a public exported header file containing various
5033 * structures. The structures are carefully written to have
5034 * exactly the same in-memory format as the XDR structures that
5035 * we use on the wire to the daemon. The reason for creating
5036 * copies of these structures here is just so we don't have to
5037 * export the whole of guestfs_protocol.h (which includes much
5038 * unrelated and XDR-dependent stuff that we don't want to be
5039 * public, or required by clients).
5041 * To reiterate, we will pass these structures to and from the
5042 * client with a simple assignment or memcpy, so the format
5043 * must be identical to what rpcgen / the RFC defines.
5046 (* Public structures. *)
5049 pr "struct guestfs_%s {\n" typ;
5052 | name, FChar -> pr " char %s;\n" name
5053 | name, FString -> pr " char *%s;\n" name
5055 pr " uint32_t %s_len;\n" name;
5056 pr " char *%s;\n" name
5057 | name, FUUID -> pr " char %s[32]; /* this is NOT nul-terminated, be careful when printing */\n" name
5058 | name, FUInt32 -> pr " uint32_t %s;\n" name
5059 | name, FInt32 -> pr " int32_t %s;\n" name
5060 | name, (FUInt64|FBytes) -> pr " uint64_t %s;\n" name
5061 | name, FInt64 -> pr " int64_t %s;\n" name
5062 | name, FOptPercent -> pr " float %s; /* [0..100] or -1 */\n" name
5066 pr "struct guestfs_%s_list {\n" typ;
5067 pr " uint32_t len;\n";
5068 pr " struct guestfs_%s *val;\n" typ;
5071 pr "extern void guestfs_free_%s (struct guestfs_%s *);\n" typ typ;
5072 pr "extern void guestfs_free_%s_list (struct guestfs_%s_list *);\n" typ typ;
5076 (* Generate the guestfs-actions.h file. *)
5077 and generate_actions_h () =
5078 generate_header CStyle LGPLv2;
5080 fun (shortname, style, _, _, _, _, _) ->
5081 let name = "guestfs_" ^ shortname in
5082 generate_prototype ~single_line:true ~newline:true ~handle:"handle"
5086 (* Generate the guestfs-internal-actions.h file. *)
5087 and generate_internal_actions_h () =
5088 generate_header CStyle LGPLv2;
5090 fun (shortname, style, _, _, _, _, _) ->
5091 let name = "guestfs__" ^ shortname in
5092 generate_prototype ~single_line:true ~newline:true ~handle:"handle"
5094 ) non_daemon_functions
5096 (* Generate the client-side dispatch stubs. *)
5097 and generate_client_actions () =
5098 generate_header CStyle LGPLv2;
5104 #include <inttypes.h>
5106 #include \"guestfs.h\"
5107 #include \"guestfs-internal.h\"
5108 #include \"guestfs-internal-actions.h\"
5109 #include \"guestfs_protocol.h\"
5111 #define error guestfs_error
5112 //#define perrorf guestfs_perrorf
5113 #define safe_malloc guestfs_safe_malloc
5114 #define safe_realloc guestfs_safe_realloc
5115 //#define safe_strdup guestfs_safe_strdup
5116 #define safe_memdup guestfs_safe_memdup
5118 /* Check the return message from a call for validity. */
5120 check_reply_header (guestfs_h *g,
5121 const struct guestfs_message_header *hdr,
5122 unsigned int proc_nr, unsigned int serial)
5124 if (hdr->prog != GUESTFS_PROGRAM) {
5125 error (g, \"wrong program (%%d/%%d)\", hdr->prog, GUESTFS_PROGRAM);
5128 if (hdr->vers != GUESTFS_PROTOCOL_VERSION) {
5129 error (g, \"wrong protocol version (%%d/%%d)\",
5130 hdr->vers, GUESTFS_PROTOCOL_VERSION);
5133 if (hdr->direction != GUESTFS_DIRECTION_REPLY) {
5134 error (g, \"unexpected message direction (%%d/%%d)\",
5135 hdr->direction, GUESTFS_DIRECTION_REPLY);
5138 if (hdr->proc != proc_nr) {
5139 error (g, \"unexpected procedure number (%%d/%%d)\", hdr->proc, proc_nr);
5142 if (hdr->serial != serial) {
5143 error (g, \"unexpected serial (%%d/%%d)\", hdr->serial, serial);
5150 /* Check we are in the right state to run a high-level action. */
5152 check_state (guestfs_h *g, const char *caller)
5154 if (!guestfs__is_ready (g)) {
5155 if (guestfs__is_config (g) || guestfs__is_launching (g))
5156 error (g, \"%%s: call launch before using this function\\n(in guestfish, don't forget to use the 'run' command)\",
5159 error (g, \"%%s called from the wrong state, %%d != READY\",
5160 caller, guestfs__get_state (g));
5168 (* Generate code to generate guestfish call traces. *)
5169 let trace_call shortname style =
5170 pr " if (guestfs__get_trace (g)) {\n";
5173 List.exists (function
5174 | StringList _ | DeviceList _ -> true
5175 | _ -> false) (snd style) in
5181 pr " printf (\"%s\");\n" shortname;
5184 | String n (* strings *)
5190 (* guestfish doesn't support string escaping, so neither do we *)
5191 pr " printf (\" \\\"%%s\\\"\", %s);\n" n
5192 | OptString n -> (* string option *)
5193 pr " if (%s) printf (\" \\\"%%s\\\"\", %s);\n" n n;
5194 pr " else printf (\" null\");\n"
5196 | DeviceList n -> (* string list *)
5197 pr " putchar (' ');\n";
5198 pr " putchar ('\"');\n";
5199 pr " for (i = 0; %s[i]; ++i) {\n" n;
5200 pr " if (i > 0) putchar (' ');\n";
5201 pr " fputs (%s[i], stdout);\n" n;
5203 pr " putchar ('\"');\n";
5204 | Bool n -> (* boolean *)
5205 pr " fputs (%s ? \" true\" : \" false\", stdout);\n" n
5206 | Int n -> (* int *)
5207 pr " printf (\" %%d\", %s);\n" n
5209 pr " printf (\" %%\" PRIi64, %s);\n" n
5211 pr " putchar ('\\n');\n";
5216 (* For non-daemon functions, generate a wrapper around each function. *)
5218 fun (shortname, style, _, _, _, _, _) ->
5219 let name = "guestfs_" ^ shortname in
5221 generate_prototype ~extern:false ~semicolon:false ~newline:true
5222 ~handle:"g" name style;
5224 trace_call shortname style;
5225 pr " return guestfs__%s " shortname;
5226 generate_c_call_args ~handle:"g" style;
5230 ) non_daemon_functions;
5232 (* Client-side stubs for each function. *)
5234 fun (shortname, style, _, _, _, _, _) ->
5235 let name = "guestfs_" ^ shortname in
5237 (* Generate the action stub. *)
5238 generate_prototype ~extern:false ~semicolon:false ~newline:true
5239 ~handle:"g" name style;
5242 match fst style with
5243 | RErr | RInt _ | RInt64 _ | RBool _ -> "-1"
5244 | RConstString _ | RConstOptString _ ->
5245 failwithf "RConstString|RConstOptString cannot be used by daemon functions"
5246 | RString _ | RStringList _
5247 | RStruct _ | RStructList _
5248 | RHashtable _ | RBufferOut _ ->
5253 (match snd style with
5255 | _ -> pr " struct %s_args args;\n" name
5258 pr " guestfs_message_header hdr;\n";
5259 pr " guestfs_message_error err;\n";
5261 match fst style with
5263 | RConstString _ | RConstOptString _ ->
5264 failwithf "RConstString|RConstOptString cannot be used by daemon functions"
5266 | RBool _ | RString _ | RStringList _
5267 | RStruct _ | RStructList _
5268 | RHashtable _ | RBufferOut _ ->
5269 pr " struct %s_ret ret;\n" name;
5272 pr " int serial;\n";
5275 trace_call shortname style;
5276 pr " if (check_state (g, \"%s\") == -1) return %s;\n" name error_code;
5277 pr " guestfs___set_busy (g);\n";
5280 (* Send the main header and arguments. *)
5281 (match snd style with
5283 pr " serial = guestfs___send (g, GUESTFS_PROC_%s, NULL, NULL);\n"
5284 (String.uppercase shortname)
5288 | Pathname n | Device n | Dev_or_Path n | String n ->
5289 pr " args.%s = (char *) %s;\n" n n
5291 pr " args.%s = %s ? (char **) &%s : NULL;\n" n n n
5292 | StringList n | DeviceList n ->
5293 pr " args.%s.%s_val = (char **) %s;\n" n n n;
5294 pr " for (args.%s.%s_len = 0; %s[args.%s.%s_len]; args.%s.%s_len++) ;\n" n n n n n n n;
5296 pr " args.%s = %s;\n" n n
5298 pr " args.%s = %s;\n" n n
5300 pr " args.%s = %s;\n" n n
5301 | FileIn _ | FileOut _ -> ()
5303 pr " serial = guestfs___send (g, GUESTFS_PROC_%s,\n"
5304 (String.uppercase shortname);
5305 pr " (xdrproc_t) xdr_%s_args, (char *) &args);\n"
5308 pr " if (serial == -1) {\n";
5309 pr " guestfs___end_busy (g);\n";
5310 pr " return %s;\n" error_code;
5314 (* Send any additional files (FileIn) requested. *)
5315 let need_read_reply_label = ref false in
5319 pr " r = guestfs___send_file (g, %s);\n" n;
5320 pr " if (r == -1) {\n";
5321 pr " guestfs___end_busy (g);\n";
5322 pr " return %s;\n" error_code;
5324 pr " if (r == -2) /* daemon cancelled */\n";
5325 pr " goto read_reply;\n";
5326 need_read_reply_label := true;
5331 (* Wait for the reply from the remote end. *)
5332 if !need_read_reply_label then pr " read_reply:\n";
5333 pr " memset (&hdr, 0, sizeof hdr);\n";
5334 pr " memset (&err, 0, sizeof err);\n";
5335 if has_ret then pr " memset (&ret, 0, sizeof ret);\n";
5337 pr " r = guestfs___recv (g, \"%s\", &hdr, &err,\n " shortname;
5341 pr "(xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret" shortname;
5344 pr " if (r == -1) {\n";
5345 pr " guestfs___end_busy (g);\n";
5346 pr " return %s;\n" error_code;
5350 pr " if (check_reply_header (g, &hdr, GUESTFS_PROC_%s, serial) == -1) {\n"
5351 (String.uppercase shortname);
5352 pr " guestfs___end_busy (g);\n";
5353 pr " return %s;\n" error_code;
5357 pr " if (hdr.status == GUESTFS_STATUS_ERROR) {\n";
5358 pr " error (g, \"%%s: %%s\", \"%s\", err.error_message);\n" shortname;
5359 pr " free (err.error_message);\n";
5360 pr " guestfs___end_busy (g);\n";
5361 pr " return %s;\n" error_code;
5365 (* Expecting to receive further files (FileOut)? *)
5369 pr " if (guestfs___recv_file (g, %s) == -1) {\n" n;
5370 pr " guestfs___end_busy (g);\n";
5371 pr " return %s;\n" error_code;
5377 pr " guestfs___end_busy (g);\n";
5379 (match fst style with
5380 | RErr -> pr " return 0;\n"
5381 | RInt n | RInt64 n | RBool n ->
5382 pr " return ret.%s;\n" n
5383 | RConstString _ | RConstOptString _ ->
5384 failwithf "RConstString|RConstOptString cannot be used by daemon functions"
5386 pr " return ret.%s; /* caller will free */\n" n
5387 | RStringList n | RHashtable n ->
5388 pr " /* caller will free this, but we need to add a NULL entry */\n";
5389 pr " ret.%s.%s_val =\n" n n;
5390 pr " safe_realloc (g, ret.%s.%s_val,\n" n n;
5391 pr " sizeof (char *) * (ret.%s.%s_len + 1));\n"
5393 pr " ret.%s.%s_val[ret.%s.%s_len] = NULL;\n" n n n n;
5394 pr " return ret.%s.%s_val;\n" n n
5396 pr " /* caller will free this */\n";
5397 pr " return safe_memdup (g, &ret.%s, sizeof (ret.%s));\n" n n
5398 | RStructList (n, _) ->
5399 pr " /* caller will free this */\n";
5400 pr " return safe_memdup (g, &ret.%s, sizeof (ret.%s));\n" n n
5402 pr " /* RBufferOut is tricky: If the buffer is zero-length, then\n";
5403 pr " * _val might be NULL here. To make the API saner for\n";
5404 pr " * callers, we turn this case into a unique pointer (using\n";
5405 pr " * malloc(1)).\n";
5407 pr " if (ret.%s.%s_len > 0) {\n" n n;
5408 pr " *size_r = ret.%s.%s_len;\n" n n;
5409 pr " return ret.%s.%s_val; /* caller will free */\n" n n;
5411 pr " free (ret.%s.%s_val);\n" n n;
5412 pr " char *p = safe_malloc (g, 1);\n";
5413 pr " *size_r = ret.%s.%s_len;\n" n n;
5421 (* Functions to free structures. *)
5422 pr "/* Structure-freeing functions. These rely on the fact that the\n";
5423 pr " * structure format is identical to the XDR format. See note in\n";
5424 pr " * generator.ml.\n";
5431 pr "guestfs_free_%s (struct guestfs_%s *x)\n" typ typ;
5433 pr " xdr_free ((xdrproc_t) xdr_guestfs_int_%s, (char *) x);\n" typ;
5439 pr "guestfs_free_%s_list (struct guestfs_%s_list *x)\n" typ typ;
5441 pr " xdr_free ((xdrproc_t) xdr_guestfs_int_%s_list, (char *) x);\n" typ;
5448 (* Generate daemon/actions.h. *)
5449 and generate_daemon_actions_h () =
5450 generate_header CStyle GPLv2;
5452 pr "#include \"../src/guestfs_protocol.h\"\n";
5456 fun (name, style, _, _, _, _, _) ->
5458 ~single_line:true ~newline:true ~in_daemon:true ~prefix:"do_"
5462 (* Generate the server-side stubs. *)
5463 and generate_daemon_actions () =
5464 generate_header CStyle GPLv2;
5466 pr "#include <config.h>\n";
5468 pr "#include <stdio.h>\n";
5469 pr "#include <stdlib.h>\n";
5470 pr "#include <string.h>\n";
5471 pr "#include <inttypes.h>\n";
5472 pr "#include <rpc/types.h>\n";
5473 pr "#include <rpc/xdr.h>\n";
5475 pr "#include \"daemon.h\"\n";
5476 pr "#include \"c-ctype.h\"\n";
5477 pr "#include \"../src/guestfs_protocol.h\"\n";
5478 pr "#include \"actions.h\"\n";
5482 fun (name, style, _, _, _, _, _) ->
5483 (* Generate server-side stubs. *)
5484 pr "static void %s_stub (XDR *xdr_in)\n" name;
5487 match fst style with
5488 | RErr | RInt _ -> pr " int r;\n"; "-1"
5489 | RInt64 _ -> pr " int64_t r;\n"; "-1"
5490 | RBool _ -> pr " int r;\n"; "-1"
5491 | RConstString _ | RConstOptString _ ->
5492 failwithf "RConstString|RConstOptString cannot be used by daemon functions"
5493 | RString _ -> pr " char *r;\n"; "NULL"
5494 | RStringList _ | RHashtable _ -> pr " char **r;\n"; "NULL"
5495 | RStruct (_, typ) -> pr " guestfs_int_%s *r;\n" typ; "NULL"
5496 | RStructList (_, typ) -> pr " guestfs_int_%s_list *r;\n" typ; "NULL"
5498 pr " size_t size = 1;\n";
5502 (match snd style with
5505 pr " struct guestfs_%s_args args;\n" name;
5508 | Device n | Dev_or_Path n
5511 | OptString n -> pr " char *%s;\n" n
5512 | StringList n | DeviceList n -> pr " char **%s;\n" n
5513 | Bool n -> pr " int %s;\n" n
5514 | Int n -> pr " int %s;\n" n
5515 | Int64 n -> pr " int64_t %s;\n" n
5516 | FileIn _ | FileOut _ -> ()
5521 (match snd style with
5524 pr " memset (&args, 0, sizeof args);\n";
5526 pr " if (!xdr_guestfs_%s_args (xdr_in, &args)) {\n" name;
5527 pr " reply_with_error (\"%%s: daemon failed to decode procedure arguments\", \"%s\");\n" name;
5531 pr " char *%s = args.%s;\n" n n
5533 let pr_list_handling_code n =
5534 pr " %s = realloc (args.%s.%s_val,\n" n n n;
5535 pr " sizeof (char *) * (args.%s.%s_len+1));\n" n n;
5536 pr " if (%s == NULL) {\n" n;
5537 pr " reply_with_perror (\"realloc\");\n";
5540 pr " %s[args.%s.%s_len] = NULL;\n" n n n;
5541 pr " args.%s.%s_val = %s;\n" n n n;
5547 pr " ABS_PATH (%s, goto done);\n" n;
5550 pr " RESOLVE_DEVICE (%s, goto done);\n" n;
5553 pr " REQUIRE_ROOT_OR_RESOLVE_DEVICE (%s, goto done);\n" n;
5554 | String n -> pr_args n
5555 | OptString n -> pr " %s = args.%s ? *args.%s : NULL;\n" n n n
5557 pr_list_handling_code n;
5559 pr_list_handling_code n;
5560 pr " /* Ensure that each is a device,\n";
5561 pr " * and perform device name translation. */\n";
5562 pr " { int pvi; for (pvi = 0; physvols[pvi] != NULL; ++pvi)\n";
5563 pr " RESOLVE_DEVICE (physvols[pvi], goto done);\n";
5565 | Bool n -> pr " %s = args.%s;\n" n n
5566 | Int n -> pr " %s = args.%s;\n" n n
5567 | Int64 n -> pr " %s = args.%s;\n" n n
5568 | FileIn _ | FileOut _ -> ()
5574 (* this is used at least for do_equal *)
5575 if List.exists (function Pathname _ -> true | _ -> false) (snd style) then (
5576 (* Emit NEED_ROOT just once, even when there are two or
5577 more Pathname args *)
5578 pr " NEED_ROOT (goto done);\n";
5581 (* Don't want to call the impl with any FileIn or FileOut
5582 * parameters, since these go "outside" the RPC protocol.
5585 List.filter (function FileIn _ | FileOut _ -> false | _ -> true)
5587 pr " r = do_%s " name;
5588 generate_c_call_args (fst style, args');
5591 (match fst style with
5592 | RErr | RInt _ | RInt64 _ | RBool _
5593 | RConstString _ | RConstOptString _
5594 | RString _ | RStringList _ | RHashtable _
5595 | RStruct (_, _) | RStructList (_, _) ->
5596 pr " if (r == %s)\n" error_code;
5597 pr " /* do_%s has already called reply_with_error */\n" name;
5601 pr " /* size == 0 && r == NULL could be a non-error case (just\n";
5602 pr " * an ordinary zero-length buffer), so be careful ...\n";
5604 pr " if (size == 1 && r == %s)\n" error_code;
5605 pr " /* do_%s has already called reply_with_error */\n" name;
5610 (* If there are any FileOut parameters, then the impl must
5611 * send its own reply.
5614 List.exists (function FileOut _ -> true | _ -> false) (snd style) in
5616 pr " /* do_%s has already sent a reply */\n" name
5618 match fst style with
5619 | RErr -> pr " reply (NULL, NULL);\n"
5620 | RInt n | RInt64 n | RBool n ->
5621 pr " struct guestfs_%s_ret ret;\n" name;
5622 pr " ret.%s = r;\n" n;
5623 pr " reply ((xdrproc_t) &xdr_guestfs_%s_ret, (char *) &ret);\n"
5625 | RConstString _ | RConstOptString _ ->
5626 failwithf "RConstString|RConstOptString cannot be used by daemon functions"
5628 pr " struct guestfs_%s_ret ret;\n" name;
5629 pr " ret.%s = r;\n" n;
5630 pr " reply ((xdrproc_t) &xdr_guestfs_%s_ret, (char *) &ret);\n"
5633 | RStringList n | RHashtable n ->
5634 pr " struct guestfs_%s_ret ret;\n" name;
5635 pr " ret.%s.%s_len = count_strings (r);\n" n n;
5636 pr " ret.%s.%s_val = r;\n" n n;
5637 pr " reply ((xdrproc_t) &xdr_guestfs_%s_ret, (char *) &ret);\n"
5639 pr " free_strings (r);\n"
5641 pr " struct guestfs_%s_ret ret;\n" name;
5642 pr " ret.%s = *r;\n" n;
5643 pr " reply ((xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret);\n"
5645 pr " xdr_free ((xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret);\n"
5647 | RStructList (n, _) ->
5648 pr " struct guestfs_%s_ret ret;\n" name;
5649 pr " ret.%s = *r;\n" n;
5650 pr " reply ((xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret);\n"
5652 pr " xdr_free ((xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret);\n"
5655 pr " struct guestfs_%s_ret ret;\n" name;
5656 pr " ret.%s.%s_val = r;\n" n n;
5657 pr " ret.%s.%s_len = size;\n" n n;
5658 pr " reply ((xdrproc_t) &xdr_guestfs_%s_ret, (char *) &ret);\n"
5663 (* Free the args. *)
5664 (match snd style with
5669 pr " xdr_free ((xdrproc_t) xdr_guestfs_%s_args, (char *) &args);\n"
5676 (* Dispatch function. *)
5677 pr "void dispatch_incoming_message (XDR *xdr_in)\n";
5679 pr " switch (proc_nr) {\n";
5682 fun (name, style, _, _, _, _, _) ->
5683 pr " case GUESTFS_PROC_%s:\n" (String.uppercase name);
5684 pr " %s_stub (xdr_in);\n" name;
5689 pr " reply_with_error (\"dispatch_incoming_message: unknown procedure number %%d, set LIBGUESTFS_PATH to point to the matching libguestfs appliance directory\", proc_nr);\n";
5694 (* LVM columns and tokenization functions. *)
5695 (* XXX This generates crap code. We should rethink how we
5701 pr "static const char *lvm_%s_cols = \"%s\";\n"
5702 typ (String.concat "," (List.map fst cols));
5705 pr "static int lvm_tokenize_%s (char *str, guestfs_int_lvm_%s *r)\n" typ typ;
5707 pr " char *tok, *p, *next;\n";
5711 pr " fprintf (stderr, \"%%s: <<%%s>>\\n\", __func__, str);\n";
5714 pr " if (!str) {\n";
5715 pr " fprintf (stderr, \"%%s: failed: passed a NULL string\\n\", __func__);\n";
5718 pr " if (!*str || c_isspace (*str)) {\n";
5719 pr " fprintf (stderr, \"%%s: failed: passed a empty string or one beginning with whitespace\\n\", __func__);\n";
5724 fun (name, coltype) ->
5725 pr " if (!tok) {\n";
5726 pr " fprintf (stderr, \"%%s: failed: string finished early, around token %%s\\n\", __func__, \"%s\");\n" name;
5729 pr " p = strchrnul (tok, ',');\n";
5730 pr " if (*p) next = p+1; else next = NULL;\n";
5731 pr " *p = '\\0';\n";
5734 pr " r->%s = strdup (tok);\n" name;
5735 pr " if (r->%s == NULL) {\n" name;
5736 pr " perror (\"strdup\");\n";
5740 pr " for (i = j = 0; i < 32; ++j) {\n";
5741 pr " if (tok[j] == '\\0') {\n";
5742 pr " fprintf (stderr, \"%%s: failed to parse UUID from '%%s'\\n\", __func__, tok);\n";
5744 pr " } else if (tok[j] != '-')\n";
5745 pr " r->%s[i++] = tok[j];\n" name;
5748 pr " if (sscanf (tok, \"%%\"SCNu64, &r->%s) != 1) {\n" name;
5749 pr " fprintf (stderr, \"%%s: failed to parse size '%%s' from token %%s\\n\", __func__, tok, \"%s\");\n" name;
5753 pr " if (sscanf (tok, \"%%\"SCNi64, &r->%s) != 1) {\n" name;
5754 pr " fprintf (stderr, \"%%s: failed to parse int '%%s' from token %%s\\n\", __func__, tok, \"%s\");\n" name;
5758 pr " if (tok[0] == '\\0')\n";
5759 pr " r->%s = -1;\n" name;
5760 pr " else if (sscanf (tok, \"%%f\", &r->%s) != 1) {\n" name;
5761 pr " fprintf (stderr, \"%%s: failed to parse float '%%s' from token %%s\\n\", __func__, tok, \"%s\");\n" name;
5764 | FBuffer | FInt32 | FUInt32 | FUInt64 | FChar ->
5765 assert false (* can never be an LVM column *)
5767 pr " tok = next;\n";
5770 pr " if (tok != NULL) {\n";
5771 pr " fprintf (stderr, \"%%s: failed: extra tokens at end of string\\n\", __func__);\n";
5778 pr "guestfs_int_lvm_%s_list *\n" typ;
5779 pr "parse_command_line_%ss (void)\n" typ;
5781 pr " char *out, *err;\n";
5782 pr " char *p, *pend;\n";
5784 pr " guestfs_int_lvm_%s_list *ret;\n" typ;
5785 pr " void *newp;\n";
5787 pr " ret = malloc (sizeof *ret);\n";
5788 pr " if (!ret) {\n";
5789 pr " reply_with_perror (\"malloc\");\n";
5790 pr " return NULL;\n";
5793 pr " ret->guestfs_int_lvm_%s_list_len = 0;\n" typ;
5794 pr " ret->guestfs_int_lvm_%s_list_val = NULL;\n" typ;
5796 pr " r = command (&out, &err,\n";
5797 pr " \"/sbin/lvm\", \"%ss\",\n" typ;
5798 pr " \"-o\", lvm_%s_cols, \"--unbuffered\", \"--noheadings\",\n" typ;
5799 pr " \"--nosuffix\", \"--separator\", \",\", \"--units\", \"b\", NULL);\n";
5800 pr " if (r == -1) {\n";
5801 pr " reply_with_error (\"%%s\", err);\n";
5802 pr " free (out);\n";
5803 pr " free (err);\n";
5804 pr " free (ret);\n";
5805 pr " return NULL;\n";
5808 pr " free (err);\n";
5810 pr " /* Tokenize each line of the output. */\n";
5813 pr " while (p) {\n";
5814 pr " pend = strchr (p, '\\n'); /* Get the next line of output. */\n";
5815 pr " if (pend) {\n";
5816 pr " *pend = '\\0';\n";
5820 pr " while (*p && c_isspace (*p)) /* Skip any leading whitespace. */\n";
5823 pr " if (!*p) { /* Empty line? Skip it. */\n";
5828 pr " /* Allocate some space to store this next entry. */\n";
5829 pr " newp = realloc (ret->guestfs_int_lvm_%s_list_val,\n" typ;
5830 pr " sizeof (guestfs_int_lvm_%s) * (i+1));\n" typ;
5831 pr " if (newp == NULL) {\n";
5832 pr " reply_with_perror (\"realloc\");\n";
5833 pr " free (ret->guestfs_int_lvm_%s_list_val);\n" typ;
5834 pr " free (ret);\n";
5835 pr " free (out);\n";
5836 pr " return NULL;\n";
5838 pr " ret->guestfs_int_lvm_%s_list_val = newp;\n" typ;
5840 pr " /* Tokenize the next entry. */\n";
5841 pr " r = lvm_tokenize_%s (p, &ret->guestfs_int_lvm_%s_list_val[i]);\n" typ typ;
5842 pr " if (r == -1) {\n";
5843 pr " reply_with_error (\"failed to parse output of '%ss' command\");\n" typ;
5844 pr " free (ret->guestfs_int_lvm_%s_list_val);\n" typ;
5845 pr " free (ret);\n";
5846 pr " free (out);\n";
5847 pr " return NULL;\n";
5854 pr " ret->guestfs_int_lvm_%s_list_len = i;\n" typ;
5856 pr " free (out);\n";
5857 pr " return ret;\n";
5860 ) ["pv", lvm_pv_cols; "vg", lvm_vg_cols; "lv", lvm_lv_cols]
5862 (* Generate a list of function names, for debugging in the daemon.. *)
5863 and generate_daemon_names () =
5864 generate_header CStyle GPLv2;
5866 pr "#include <config.h>\n";
5868 pr "#include \"daemon.h\"\n";
5871 pr "/* This array is indexed by proc_nr. See guestfs_protocol.x. */\n";
5872 pr "const char *function_names[] = {\n";
5874 fun (name, _, proc_nr, _, _, _, _) -> pr " [%d] = \"%s\",\n" proc_nr name
5878 (* Generate the tests. *)
5879 and generate_tests () =
5880 generate_header CStyle GPLv2;
5887 #include <sys/types.h>
5890 #include \"guestfs.h\"
5891 #include \"guestfs-internal.h\"
5893 static guestfs_h *g;
5894 static int suppress_error = 0;
5896 static void print_error (guestfs_h *g, void *data, const char *msg)
5898 if (!suppress_error)
5899 fprintf (stderr, \"%%s\\n\", msg);
5902 /* FIXME: nearly identical code appears in fish.c */
5903 static void print_strings (char *const *argv)
5907 for (argc = 0; argv[argc] != NULL; ++argc)
5908 printf (\"\\t%%s\\n\", argv[argc]);
5912 static void print_table (char const *const *argv)
5916 for (i = 0; argv[i] != NULL; i += 2)
5917 printf (\"%%s: %%s\\n\", argv[i], argv[i+1]);
5923 (* Generate a list of commands which are not tested anywhere. *)
5924 pr "static void no_test_warnings (void)\n";
5927 let hash : (string, bool) Hashtbl.t = Hashtbl.create 13 in
5929 fun (_, _, _, _, tests, _, _) ->
5930 let tests = filter_map (
5932 | (_, (Always|If _|Unless _), test) -> Some test
5933 | (_, Disabled, _) -> None
5935 let seq = List.concat (List.map seq_of_test tests) in
5936 let cmds_tested = List.map List.hd seq in
5937 List.iter (fun cmd -> Hashtbl.replace hash cmd true) cmds_tested
5941 fun (name, _, _, _, _, _, _) ->
5942 if not (Hashtbl.mem hash name) then
5943 pr " fprintf (stderr, \"warning: \\\"guestfs_%s\\\" has no tests\\n\");\n" name
5949 (* Generate the actual tests. Note that we generate the tests
5950 * in reverse order, deliberately, so that (in general) the
5951 * newest tests run first. This makes it quicker and easier to
5956 fun (name, _, _, _, tests, _, _) ->
5957 mapi (generate_one_test name) tests
5958 ) (List.rev all_functions) in
5959 let test_names = List.concat test_names in
5960 let nr_tests = List.length test_names in
5963 int main (int argc, char *argv[])
5966 unsigned long int n_failed = 0;
5967 const char *filename;
5969 int nr_tests, test_num = 0;
5971 setbuf (stdout, NULL);
5973 no_test_warnings ();
5975 g = guestfs_create ();
5977 printf (\"guestfs_create FAILED\\n\");
5978 exit (EXIT_FAILURE);
5981 guestfs_set_error_handler (g, print_error, NULL);
5983 guestfs_set_path (g, \"../appliance\");
5985 filename = \"test1.img\";
5986 fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666);
5989 exit (EXIT_FAILURE);
5991 if (lseek (fd, %d, SEEK_SET) == -1) {
5995 exit (EXIT_FAILURE);
5997 if (write (fd, &c, 1) == -1) {
6001 exit (EXIT_FAILURE);
6003 if (close (fd) == -1) {
6006 exit (EXIT_FAILURE);
6008 if (guestfs_add_drive (g, filename) == -1) {
6009 printf (\"guestfs_add_drive %%s FAILED\\n\", filename);
6010 exit (EXIT_FAILURE);
6013 filename = \"test2.img\";
6014 fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666);
6017 exit (EXIT_FAILURE);
6019 if (lseek (fd, %d, SEEK_SET) == -1) {
6023 exit (EXIT_FAILURE);
6025 if (write (fd, &c, 1) == -1) {
6029 exit (EXIT_FAILURE);
6031 if (close (fd) == -1) {
6034 exit (EXIT_FAILURE);
6036 if (guestfs_add_drive (g, filename) == -1) {
6037 printf (\"guestfs_add_drive %%s FAILED\\n\", filename);
6038 exit (EXIT_FAILURE);
6041 filename = \"test3.img\";
6042 fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666);
6045 exit (EXIT_FAILURE);
6047 if (lseek (fd, %d, SEEK_SET) == -1) {
6051 exit (EXIT_FAILURE);
6053 if (write (fd, &c, 1) == -1) {
6057 exit (EXIT_FAILURE);
6059 if (close (fd) == -1) {
6062 exit (EXIT_FAILURE);
6064 if (guestfs_add_drive (g, filename) == -1) {
6065 printf (\"guestfs_add_drive %%s FAILED\\n\", filename);
6066 exit (EXIT_FAILURE);
6069 if (guestfs_add_drive_ro (g, \"../images/test.iso\") == -1) {
6070 printf (\"guestfs_add_drive_ro ../images/test.iso FAILED\\n\");
6071 exit (EXIT_FAILURE);
6074 if (guestfs_launch (g) == -1) {
6075 printf (\"guestfs_launch FAILED\\n\");
6076 exit (EXIT_FAILURE);
6079 /* Set a timeout in case qemu hangs during launch (RHBZ#505329). */
6082 /* Cancel previous alarm. */
6087 " (500 * 1024 * 1024) (50 * 1024 * 1024) (10 * 1024 * 1024) nr_tests;
6091 pr " test_num++;\n";
6092 pr " printf (\"%%3d/%%3d %s\\n\", test_num, nr_tests);\n" test_name;
6093 pr " if (%s () == -1) {\n" test_name;
6094 pr " printf (\"%s FAILED\\n\");\n" test_name;
6095 pr " n_failed++;\n";
6100 pr " guestfs_close (g);\n";
6101 pr " unlink (\"test1.img\");\n";
6102 pr " unlink (\"test2.img\");\n";
6103 pr " unlink (\"test3.img\");\n";
6106 pr " if (n_failed > 0) {\n";
6107 pr " printf (\"***** %%lu / %%d tests FAILED *****\\n\", n_failed, nr_tests);\n";
6108 pr " exit (EXIT_FAILURE);\n";
6112 pr " exit (EXIT_SUCCESS);\n";
6115 and generate_one_test name i (init, prereq, test) =
6116 let test_name = sprintf "test_%s_%d" name i in
6119 static int %s_skip (void)
6123 str = getenv (\"TEST_ONLY\");
6125 return strstr (str, \"%s\") == NULL;
6126 str = getenv (\"SKIP_%s\");
6127 if (str && STREQ (str, \"1\")) return 1;
6128 str = getenv (\"SKIP_TEST_%s\");
6129 if (str && STREQ (str, \"1\")) return 1;
6133 " test_name name (String.uppercase test_name) (String.uppercase name);
6136 | Disabled | Always -> ()
6137 | If code | Unless code ->
6138 pr "static int %s_prereq (void)\n" test_name;
6146 static int %s (void)
6149 printf (\" %%s skipped (reason: environment variable set)\\n\", \"%s\");
6153 " test_name test_name test_name;
6157 pr " printf (\" %%s skipped (reason: test disabled in generator)\\n\", \"%s\");\n" test_name
6159 pr " if (! %s_prereq ()) {\n" test_name;
6160 pr " printf (\" %%s skipped (reason: test prerequisite)\\n\", \"%s\");\n" test_name;
6164 generate_one_test_body name i test_name init test;
6166 pr " if (%s_prereq ()) {\n" test_name;
6167 pr " printf (\" %%s skipped (reason: test prerequisite)\\n\", \"%s\");\n" test_name;
6171 generate_one_test_body name i test_name init test;
6173 generate_one_test_body name i test_name init test
6181 and generate_one_test_body name i test_name init test =
6183 | InitNone (* XXX at some point, InitNone and InitEmpty became
6184 * folded together as the same thing. Really we should
6185 * make InitNone do nothing at all, but the tests may
6186 * need to be checked to make sure this is OK.
6189 pr " /* InitNone|InitEmpty for %s */\n" test_name;
6190 List.iter (generate_test_command_call test_name)
6191 [["blockdev_setrw"; "/dev/sda"];
6195 pr " /* InitPartition for %s: create /dev/sda1 */\n" test_name;
6196 List.iter (generate_test_command_call test_name)
6197 [["blockdev_setrw"; "/dev/sda"];
6200 ["part_disk"; "/dev/sda"; "mbr"]]
6202 pr " /* InitBasicFS for %s: create ext2 on /dev/sda1 */\n" test_name;
6203 List.iter (generate_test_command_call test_name)
6204 [["blockdev_setrw"; "/dev/sda"];
6207 ["part_disk"; "/dev/sda"; "mbr"];
6208 ["mkfs"; "ext2"; "/dev/sda1"];
6209 ["mount"; "/dev/sda1"; "/"]]
6210 | InitBasicFSonLVM ->
6211 pr " /* InitBasicFSonLVM for %s: create ext2 on /dev/VG/LV */\n"
6213 List.iter (generate_test_command_call test_name)
6214 [["blockdev_setrw"; "/dev/sda"];
6217 ["part_disk"; "/dev/sda"; "mbr"];
6218 ["pvcreate"; "/dev/sda1"];
6219 ["vgcreate"; "VG"; "/dev/sda1"];
6220 ["lvcreate"; "LV"; "VG"; "8"];
6221 ["mkfs"; "ext2"; "/dev/VG/LV"];
6222 ["mount"; "/dev/VG/LV"; "/"]]
6224 pr " /* InitISOFS for %s */\n" test_name;
6225 List.iter (generate_test_command_call test_name)
6226 [["blockdev_setrw"; "/dev/sda"];
6229 ["mount_ro"; "/dev/sdd"; "/"]]
6232 let get_seq_last = function
6234 failwithf "%s: you cannot use [] (empty list) when expecting a command"
6237 let seq = List.rev seq in
6238 List.rev (List.tl seq), List.hd seq
6243 pr " /* TestRun for %s (%d) */\n" name i;
6244 List.iter (generate_test_command_call test_name) seq
6245 | TestOutput (seq, expected) ->
6246 pr " /* TestOutput for %s (%d) */\n" name i;
6247 pr " const char *expected = \"%s\";\n" (c_quote expected);
6248 let seq, last = get_seq_last seq in
6250 pr " if (STRNEQ (r, expected)) {\n";
6251 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r);\n" test_name;
6255 List.iter (generate_test_command_call test_name) seq;
6256 generate_test_command_call ~test test_name last
6257 | TestOutputList (seq, expected) ->
6258 pr " /* TestOutputList for %s (%d) */\n" name i;
6259 let seq, last = get_seq_last seq in
6263 pr " if (!r[%d]) {\n" i;
6264 pr " fprintf (stderr, \"%s: short list returned from command\\n\");\n" test_name;
6265 pr " print_strings (r);\n";
6269 pr " const char *expected = \"%s\";\n" (c_quote str);
6270 pr " if (STRNEQ (r[%d], expected)) {\n" i;
6271 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r[%d]);\n" test_name i;
6276 pr " if (r[%d] != NULL) {\n" (List.length expected);
6277 pr " fprintf (stderr, \"%s: extra elements returned from command\\n\");\n"
6279 pr " print_strings (r);\n";
6283 List.iter (generate_test_command_call test_name) seq;
6284 generate_test_command_call ~test test_name last
6285 | TestOutputListOfDevices (seq, expected) ->
6286 pr " /* TestOutputListOfDevices for %s (%d) */\n" name i;
6287 let seq, last = get_seq_last seq in
6291 pr " if (!r[%d]) {\n" i;
6292 pr " fprintf (stderr, \"%s: short list returned from command\\n\");\n" test_name;
6293 pr " print_strings (r);\n";
6297 pr " const char *expected = \"%s\";\n" (c_quote str);
6298 pr " r[%d][5] = 's';\n" i;
6299 pr " if (STRNEQ (r[%d], expected)) {\n" i;
6300 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r[%d]);\n" test_name i;
6305 pr " if (r[%d] != NULL) {\n" (List.length expected);
6306 pr " fprintf (stderr, \"%s: extra elements returned from command\\n\");\n"
6308 pr " print_strings (r);\n";
6312 List.iter (generate_test_command_call test_name) seq;
6313 generate_test_command_call ~test test_name last
6314 | TestOutputInt (seq, expected) ->
6315 pr " /* TestOutputInt for %s (%d) */\n" name i;
6316 let seq, last = get_seq_last seq in
6318 pr " if (r != %d) {\n" expected;
6319 pr " fprintf (stderr, \"%s: expected %d but got %%d\\n\","
6325 List.iter (generate_test_command_call test_name) seq;
6326 generate_test_command_call ~test test_name last
6327 | TestOutputIntOp (seq, op, expected) ->
6328 pr " /* TestOutputIntOp for %s (%d) */\n" name i;
6329 let seq, last = get_seq_last seq in
6331 pr " if (! (r %s %d)) {\n" op expected;
6332 pr " fprintf (stderr, \"%s: expected %s %d but got %%d\\n\","
6333 test_name op expected;
6338 List.iter (generate_test_command_call test_name) seq;
6339 generate_test_command_call ~test test_name last
6340 | TestOutputTrue seq ->
6341 pr " /* TestOutputTrue for %s (%d) */\n" name i;
6342 let seq, last = get_seq_last seq in
6345 pr " fprintf (stderr, \"%s: expected true, got false\\n\");\n"
6350 List.iter (generate_test_command_call test_name) seq;
6351 generate_test_command_call ~test test_name last
6352 | TestOutputFalse seq ->
6353 pr " /* TestOutputFalse for %s (%d) */\n" name i;
6354 let seq, last = get_seq_last seq in
6357 pr " fprintf (stderr, \"%s: expected false, got true\\n\");\n"
6362 List.iter (generate_test_command_call test_name) seq;
6363 generate_test_command_call ~test test_name last
6364 | TestOutputLength (seq, expected) ->
6365 pr " /* TestOutputLength for %s (%d) */\n" name i;
6366 let seq, last = get_seq_last seq in
6369 pr " for (j = 0; j < %d; ++j)\n" expected;
6370 pr " if (r[j] == NULL) {\n";
6371 pr " fprintf (stderr, \"%s: short list returned\\n\");\n"
6373 pr " print_strings (r);\n";
6376 pr " if (r[j] != NULL) {\n";
6377 pr " fprintf (stderr, \"%s: long list returned\\n\");\n"
6379 pr " print_strings (r);\n";
6383 List.iter (generate_test_command_call test_name) seq;
6384 generate_test_command_call ~test test_name last
6385 | TestOutputBuffer (seq, expected) ->
6386 pr " /* TestOutputBuffer for %s (%d) */\n" name i;
6387 pr " const char *expected = \"%s\";\n" (c_quote expected);
6388 let seq, last = get_seq_last seq in
6389 let len = String.length expected in
6391 pr " if (size != %d) {\n" len;
6392 pr " fprintf (stderr, \"%s: returned size of buffer wrong, expected %d but got %%zu\\n\", size);\n" test_name len;
6395 pr " if (STRNEQLEN (r, expected, size)) {\n";
6396 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r);\n" test_name;
6400 List.iter (generate_test_command_call test_name) seq;
6401 generate_test_command_call ~test test_name last
6402 | TestOutputStruct (seq, checks) ->
6403 pr " /* TestOutputStruct for %s (%d) */\n" name i;
6404 let seq, last = get_seq_last seq in
6408 | CompareWithInt (field, expected) ->
6409 pr " if (r->%s != %d) {\n" field expected;
6410 pr " fprintf (stderr, \"%s: %s was %%d, expected %d\\n\",\n"
6411 test_name field expected;
6412 pr " (int) r->%s);\n" field;
6415 | CompareWithIntOp (field, op, expected) ->
6416 pr " if (!(r->%s %s %d)) {\n" field op expected;
6417 pr " fprintf (stderr, \"%s: %s was %%d, expected %s %d\\n\",\n"
6418 test_name field op expected;
6419 pr " (int) r->%s);\n" field;
6422 | CompareWithString (field, expected) ->
6423 pr " if (STRNEQ (r->%s, \"%s\")) {\n" field expected;
6424 pr " fprintf (stderr, \"%s: %s was \"%%s\", expected \"%s\"\\n\",\n"
6425 test_name field expected;
6426 pr " r->%s);\n" field;
6429 | CompareFieldsIntEq (field1, field2) ->
6430 pr " if (r->%s != r->%s) {\n" field1 field2;
6431 pr " fprintf (stderr, \"%s: %s (%%d) <> %s (%%d)\\n\",\n"
6432 test_name field1 field2;
6433 pr " (int) r->%s, (int) r->%s);\n" field1 field2;
6436 | CompareFieldsStrEq (field1, field2) ->
6437 pr " if (STRNEQ (r->%s, r->%s)) {\n" field1 field2;
6438 pr " fprintf (stderr, \"%s: %s (\"%%s\") <> %s (\"%%s\")\\n\",\n"
6439 test_name field1 field2;
6440 pr " r->%s, r->%s);\n" field1 field2;
6445 List.iter (generate_test_command_call test_name) seq;
6446 generate_test_command_call ~test test_name last
6447 | TestLastFail seq ->
6448 pr " /* TestLastFail for %s (%d) */\n" name i;
6449 let seq, last = get_seq_last seq in
6450 List.iter (generate_test_command_call test_name) seq;
6451 generate_test_command_call test_name ~expect_error:true last
6453 (* Generate the code to run a command, leaving the result in 'r'.
6454 * If you expect to get an error then you should set expect_error:true.
6456 and generate_test_command_call ?(expect_error = false) ?test test_name cmd =
6458 | [] -> assert false
6460 (* Look up the command to find out what args/ret it has. *)
6463 let _, style, _, _, _, _, _ =
6464 List.find (fun (n, _, _, _, _, _, _) -> n = name) all_functions in
6467 failwithf "%s: in test, command %s was not found" test_name name in
6469 if List.length (snd style) <> List.length args then
6470 failwithf "%s: in test, wrong number of args given to %s"
6477 | OptString n, "NULL" -> ()
6480 | Dev_or_Path n, arg
6482 | OptString n, arg ->
6483 pr " const char *%s = \"%s\";\n" n (c_quote arg);
6487 | FileIn _, _ | FileOut _, _ -> ()
6488 | StringList n, arg | DeviceList n, arg ->
6489 let strs = string_split " " arg in
6492 pr " const char *%s_%d = \"%s\";\n" n i (c_quote str);
6494 pr " const char *const %s[] = {\n" n;
6496 fun i _ -> pr " %s_%d,\n" n i
6500 ) (List.combine (snd style) args);
6503 match fst style with
6504 | RErr | RInt _ | RBool _ -> pr " int r;\n"; "-1"
6505 | RInt64 _ -> pr " int64_t r;\n"; "-1"
6506 | RConstString _ | RConstOptString _ ->
6507 pr " const char *r;\n"; "NULL"
6508 | RString _ -> pr " char *r;\n"; "NULL"
6509 | RStringList _ | RHashtable _ ->
6513 | RStruct (_, typ) ->
6514 pr " struct guestfs_%s *r;\n" typ; "NULL"
6515 | RStructList (_, typ) ->
6516 pr " struct guestfs_%s_list *r;\n" typ; "NULL"
6519 pr " size_t size;\n";
6522 pr " suppress_error = %d;\n" (if expect_error then 1 else 0);
6523 pr " r = guestfs_%s (g" name;
6525 (* Generate the parameters. *)
6528 | OptString _, "NULL" -> pr ", NULL"
6530 | Device n, _ | Dev_or_Path n, _
6534 | FileIn _, arg | FileOut _, arg ->
6535 pr ", \"%s\"" (c_quote arg)
6536 | StringList n, _ | DeviceList n, _ ->
6537 pr ", (char **) %s" n
6540 try int_of_string arg
6541 with Failure "int_of_string" ->
6542 failwithf "%s: expecting an int, but got '%s'" test_name arg in
6546 try Int64.of_string arg
6547 with Failure "int_of_string" ->
6548 failwithf "%s: expecting an int64, but got '%s'" test_name arg in
6551 let b = bool_of_string arg in pr ", %d" (if b then 1 else 0)
6552 ) (List.combine (snd style) args);
6554 (match fst style with
6555 | RBufferOut _ -> pr ", &size"
6561 if not expect_error then
6562 pr " if (r == %s)\n" error_code
6564 pr " if (r != %s)\n" error_code;
6567 (* Insert the test code. *)
6573 (match fst style with
6574 | RErr | RInt _ | RInt64 _ | RBool _
6575 | RConstString _ | RConstOptString _ -> ()
6576 | RString _ | RBufferOut _ -> pr " free (r);\n"
6577 | RStringList _ | RHashtable _ ->
6578 pr " for (i = 0; r[i] != NULL; ++i)\n";
6579 pr " free (r[i]);\n";
6581 | RStruct (_, typ) ->
6582 pr " guestfs_free_%s (r);\n" typ
6583 | RStructList (_, typ) ->
6584 pr " guestfs_free_%s_list (r);\n" typ
6590 let str = replace_str str "\r" "\\r" in
6591 let str = replace_str str "\n" "\\n" in
6592 let str = replace_str str "\t" "\\t" in
6593 let str = replace_str str "\000" "\\0" in
6596 (* Generate a lot of different functions for guestfish. *)
6597 and generate_fish_cmds () =
6598 generate_header CStyle GPLv2;
6602 fun (_, _, _, flags, _, _, _) -> not (List.mem NotInFish flags)
6604 let all_functions_sorted =
6606 fun (_, _, _, flags, _, _, _) -> not (List.mem NotInFish flags)
6607 ) all_functions_sorted in
6609 pr "#include <stdio.h>\n";
6610 pr "#include <stdlib.h>\n";
6611 pr "#include <string.h>\n";
6612 pr "#include <inttypes.h>\n";
6614 pr "#include <guestfs.h>\n";
6615 pr "#include \"c-ctype.h\"\n";
6616 pr "#include \"fish.h\"\n";
6619 (* list_commands function, which implements guestfish -h *)
6620 pr "void list_commands (void)\n";
6622 pr " printf (\" %%-16s %%s\\n\", _(\"Command\"), _(\"Description\"));\n";
6623 pr " list_builtin_commands ();\n";
6625 fun (name, _, _, flags, _, shortdesc, _) ->
6626 let name = replace_char name '_' '-' in
6627 pr " printf (\"%%-20s %%s\\n\", \"%s\", _(\"%s\"));\n"
6629 ) all_functions_sorted;
6630 pr " printf (\" %%s\\n\",";
6631 pr " _(\"Use -h <cmd> / help <cmd> to show detailed help for a command.\"));\n";
6635 (* display_command function, which implements guestfish -h cmd *)
6636 pr "void display_command (const char *cmd)\n";
6639 fun (name, style, _, flags, _, shortdesc, longdesc) ->
6640 let name2 = replace_char name '_' '-' in
6642 try find_map (function FishAlias n -> Some n | _ -> None) flags
6643 with Not_found -> name in
6644 let longdesc = replace_str longdesc "C<guestfs_" "C<" in
6646 match snd style with
6650 name2 (String.concat " " (List.map name_of_argt args)) in
6653 if List.mem ProtocolLimitWarning flags then
6654 ("\n\n" ^ protocol_limit_warning)
6657 (* For DangerWillRobinson commands, we should probably have
6658 * guestfish prompt before allowing you to use them (especially
6659 * in interactive mode). XXX
6663 if List.mem DangerWillRobinson flags then
6664 ("\n\n" ^ danger_will_robinson)
6669 match deprecation_notice flags with
6671 | Some txt -> "\n\n" ^ txt in
6673 let describe_alias =
6674 if name <> alias then
6675 sprintf "\n\nYou can use '%s' as an alias for this command." alias
6679 pr "STRCASEEQ (cmd, \"%s\")" name;
6680 if name <> name2 then
6681 pr " || STRCASEEQ (cmd, \"%s\")" name2;
6682 if name <> alias then
6683 pr " || STRCASEEQ (cmd, \"%s\")" alias;
6685 pr " pod2text (\"%s\", _(\"%s\"), %S);\n"
6687 ("=head1 SYNOPSIS\n\n " ^ synopsis ^ "\n\n" ^
6688 "=head1 DESCRIPTION\n\n" ^
6689 longdesc ^ warnings ^ describe_alias);
6692 pr " display_builtin_command (cmd);\n";
6696 let emit_print_list_function typ =
6697 pr "static void print_%s_list (struct guestfs_%s_list *%ss)\n"
6700 pr " unsigned int i;\n";
6702 pr " for (i = 0; i < %ss->len; ++i) {\n" typ;
6703 pr " printf (\"[%%d] = {\\n\", i);\n";
6704 pr " print_%s_indent (&%ss->val[i], \" \");\n" typ typ;
6705 pr " printf (\"}\\n\");\n";
6711 (* print_* functions *)
6715 List.exists (function (_, (FUUID|FBuffer)) -> true | _ -> false) cols in
6717 pr "static void print_%s_indent (struct guestfs_%s *%s, const char *indent)\n" typ typ typ;
6720 pr " unsigned int i;\n";
6726 pr " printf (\"%%s%s: %%s\\n\", indent, %s->%s);\n" name typ name
6728 pr " printf (\"%%s%s: \", indent);\n" name;
6729 pr " for (i = 0; i < 32; ++i)\n";
6730 pr " printf (\"%%c\", %s->%s[i]);\n" typ name;
6731 pr " printf (\"\\n\");\n"
6733 pr " printf (\"%%s%s: \", indent);\n" name;
6734 pr " for (i = 0; i < %s->%s_len; ++i)\n" typ name;
6735 pr " if (c_isprint (%s->%s[i]))\n" typ name;
6736 pr " printf (\"%%c\", %s->%s[i]);\n" typ name;
6738 pr " printf (\"\\\\x%%02x\", %s->%s[i]);\n" typ name;
6739 pr " printf (\"\\n\");\n"
6740 | name, (FUInt64|FBytes) ->
6741 pr " printf (\"%%s%s: %%\" PRIu64 \"\\n\", indent, %s->%s);\n"
6744 pr " printf (\"%%s%s: %%\" PRIi64 \"\\n\", indent, %s->%s);\n"
6747 pr " printf (\"%%s%s: %%\" PRIu32 \"\\n\", indent, %s->%s);\n"
6750 pr " printf (\"%%s%s: %%\" PRIi32 \"\\n\", indent, %s->%s);\n"
6753 pr " printf (\"%%s%s: %%c\\n\", indent, %s->%s);\n"
6755 | name, FOptPercent ->
6756 pr " if (%s->%s >= 0) printf (\"%%s%s: %%g %%%%\\n\", indent, %s->%s);\n"
6757 typ name name typ name;
6758 pr " else printf (\"%%s%s: \\n\", indent);\n" name
6764 (* Emit a print_TYPE_list function definition only if that function is used. *)
6767 | typ, (RStructListOnly | RStructAndList) ->
6768 (* generate the function for typ *)
6769 emit_print_list_function typ
6770 | typ, _ -> () (* empty *)
6771 ) (rstructs_used_by all_functions);
6773 (* Emit a print_TYPE function definition only if that function is used. *)
6776 | typ, (RStructOnly | RStructAndList) ->
6777 pr "static void print_%s (struct guestfs_%s *%s)\n" typ typ typ;
6779 pr " print_%s_indent (%s, \"\");\n" typ typ;
6782 | typ, _ -> () (* empty *)
6783 ) (rstructs_used_by all_functions);
6785 (* run_<action> actions *)
6787 fun (name, style, _, flags, _, _, _) ->
6788 pr "static int run_%s (const char *cmd, int argc, char *argv[])\n" name;
6790 (match fst style with
6793 | RBool _ -> pr " int r;\n"
6794 | RInt64 _ -> pr " int64_t r;\n"
6795 | RConstString _ | RConstOptString _ -> pr " const char *r;\n"
6796 | RString _ -> pr " char *r;\n"
6797 | RStringList _ | RHashtable _ -> pr " char **r;\n"
6798 | RStruct (_, typ) -> pr " struct guestfs_%s *r;\n" typ
6799 | RStructList (_, typ) -> pr " struct guestfs_%s_list *r;\n" typ
6802 pr " size_t size;\n";
6810 | FileOut n -> pr " const char *%s;\n" n
6812 | Dev_or_Path n -> pr " char *%s;\n" n
6813 | StringList n | DeviceList n -> pr " char **%s;\n" n
6814 | Bool n -> pr " int %s;\n" n
6815 | Int n -> pr " int %s;\n" n
6816 | Int64 n -> pr " int64_t %s;\n" n
6819 (* Check and convert parameters. *)
6820 let argc_expected = List.length (snd style) in
6821 pr " if (argc != %d) {\n" argc_expected;
6822 pr " fprintf (stderr, _(\"%%s should have %%d parameter(s)\\n\"), cmd, %d);\n"
6824 pr " fprintf (stderr, _(\"type 'help %%s' for help on %%s\\n\"), cmd, cmd);\n";
6832 pr " %s = argv[%d];\n" name i
6834 | Dev_or_Path name ->
6835 pr " %s = resolve_win_path (argv[%d]);\n" name i;
6836 pr " if (%s == NULL) return -1;\n" name
6838 pr " %s = STRNEQ (argv[%d], \"\") ? argv[%d] : NULL;\n"
6841 pr " %s = STRNEQ (argv[%d], \"-\") ? argv[%d] : \"/dev/stdin\";\n"
6844 pr " %s = STRNEQ (argv[%d], \"-\") ? argv[%d] : \"/dev/stdout\";\n"
6846 | StringList name | DeviceList name ->
6847 pr " %s = parse_string_list (argv[%d]);\n" name i;
6848 pr " if (%s == NULL) return -1;\n" name;
6850 pr " %s = is_true (argv[%d]) ? 1 : 0;\n" name i
6852 pr " %s = atoi (argv[%d]);\n" name i
6854 pr " %s = atoll (argv[%d]);\n" name i
6857 (* Call C API function. *)
6859 try find_map (function FishAction n -> Some n | _ -> None) flags
6860 with Not_found -> sprintf "guestfs_%s" name in
6862 generate_c_call_args ~handle:"g" style;
6867 | Device name | String name
6868 | OptString name | FileIn name | FileOut name | Bool name
6869 | Int name | Int64 name -> ()
6870 | Pathname name | Dev_or_Path name ->
6871 pr " free (%s);\n" name
6872 | StringList name | DeviceList name ->
6873 pr " free_strings (%s);\n" name
6876 (* Check return value for errors and display command results. *)
6877 (match fst style with
6878 | RErr -> pr " return r;\n"
6880 pr " if (r == -1) return -1;\n";
6881 pr " printf (\"%%d\\n\", r);\n";
6884 pr " if (r == -1) return -1;\n";
6885 pr " printf (\"%%\" PRIi64 \"\\n\", r);\n";
6888 pr " if (r == -1) return -1;\n";
6889 pr " if (r) printf (\"true\\n\"); else printf (\"false\\n\");\n";
6892 pr " if (r == NULL) return -1;\n";
6893 pr " printf (\"%%s\\n\", r);\n";
6895 | RConstOptString _ ->
6896 pr " printf (\"%%s\\n\", r ? : \"(null)\");\n";
6899 pr " if (r == NULL) return -1;\n";
6900 pr " printf (\"%%s\\n\", r);\n";
6904 pr " if (r == NULL) return -1;\n";
6905 pr " print_strings (r);\n";
6906 pr " free_strings (r);\n";
6908 | RStruct (_, typ) ->
6909 pr " if (r == NULL) return -1;\n";
6910 pr " print_%s (r);\n" typ;
6911 pr " guestfs_free_%s (r);\n" typ;
6913 | RStructList (_, typ) ->
6914 pr " if (r == NULL) return -1;\n";
6915 pr " print_%s_list (r);\n" typ;
6916 pr " guestfs_free_%s_list (r);\n" typ;
6919 pr " if (r == NULL) return -1;\n";
6920 pr " print_table (r);\n";
6921 pr " free_strings (r);\n";
6924 pr " if (r == NULL) return -1;\n";
6925 pr " fwrite (r, size, 1, stdout);\n";
6933 (* run_action function *)
6934 pr "int run_action (const char *cmd, int argc, char *argv[])\n";
6937 fun (name, _, _, flags, _, _, _) ->
6938 let name2 = replace_char name '_' '-' in
6940 try find_map (function FishAlias n -> Some n | _ -> None) flags
6941 with Not_found -> name in
6943 pr "STRCASEEQ (cmd, \"%s\")" name;
6944 if name <> name2 then
6945 pr " || STRCASEEQ (cmd, \"%s\")" name2;
6946 if name <> alias then
6947 pr " || STRCASEEQ (cmd, \"%s\")" alias;
6949 pr " return run_%s (cmd, argc, argv);\n" name;
6953 pr " fprintf (stderr, _(\"%%s: unknown command\\n\"), cmd);\n";
6960 (* Readline completion for guestfish. *)
6961 and generate_fish_completion () =
6962 generate_header CStyle GPLv2;
6966 fun (_, _, _, flags, _, _, _) -> not (List.mem NotInFish flags)
6976 #ifdef HAVE_LIBREADLINE
6977 #include <readline/readline.h>
6982 #ifdef HAVE_LIBREADLINE
6984 static const char *const commands[] = {
6985 BUILTIN_COMMANDS_FOR_COMPLETION,
6988 (* Get the commands, including the aliases. They don't need to be
6989 * sorted - the generator() function just does a dumb linear search.
6993 fun (name, _, _, flags, _, _, _) ->
6994 let name2 = replace_char name '_' '-' in
6996 try find_map (function FishAlias n -> Some n | _ -> None) flags
6997 with Not_found -> name in
6999 if name <> alias then [name2; alias] else [name2]
7001 let commands = List.flatten commands in
7003 List.iter (pr " \"%s\",\n") commands;
7009 generator (const char *text, int state)
7011 static int index, len;
7016 len = strlen (text);
7019 rl_attempted_completion_over = 1;
7021 while ((name = commands[index]) != NULL) {
7023 if (STRCASEEQLEN (name, text, len))
7024 return strdup (name);
7030 #endif /* HAVE_LIBREADLINE */
7032 char **do_completion (const char *text, int start, int end)
7034 char **matches = NULL;
7036 #ifdef HAVE_LIBREADLINE
7037 rl_completion_append_character = ' ';
7040 matches = rl_completion_matches (text, generator);
7041 else if (complete_dest_paths)
7042 matches = rl_completion_matches (text, complete_dest_paths_generator);
7049 (* Generate the POD documentation for guestfish. *)
7050 and generate_fish_actions_pod () =
7051 let all_functions_sorted =
7053 fun (_, _, _, flags, _, _, _) ->
7054 not (List.mem NotInFish flags || List.mem NotInDocs flags)
7055 ) all_functions_sorted in
7057 let rex = Str.regexp "C<guestfs_\\([^>]+\\)>" in
7060 fun (name, style, _, flags, _, _, longdesc) ->
7062 Str.global_substitute rex (
7065 try Str.matched_group 1 s
7067 failwithf "error substituting C<guestfs_...> in longdesc of function %s" name in
7068 "C<" ^ replace_char sub '_' '-' ^ ">"
7070 let name = replace_char name '_' '-' in
7072 try find_map (function FishAlias n -> Some n | _ -> None) flags
7073 with Not_found -> name in
7075 pr "=head2 %s" name;
7076 if name <> alias then
7083 | Pathname n | Device n | Dev_or_Path n | String n -> pr " %s" n
7084 | OptString n -> pr " %s" n
7085 | StringList n | DeviceList n -> pr " '%s ...'" n
7086 | Bool _ -> pr " true|false"
7087 | Int n -> pr " %s" n
7088 | Int64 n -> pr " %s" n
7089 | FileIn n | FileOut n -> pr " (%s|-)" n
7093 pr "%s\n\n" longdesc;
7095 if List.exists (function FileIn _ | FileOut _ -> true
7096 | _ -> false) (snd style) then
7097 pr "Use C<-> instead of a filename to read/write from stdin/stdout.\n\n";
7099 if List.mem ProtocolLimitWarning flags then
7100 pr "%s\n\n" protocol_limit_warning;
7102 if List.mem DangerWillRobinson flags then
7103 pr "%s\n\n" danger_will_robinson;
7105 match deprecation_notice flags with
7107 | Some txt -> pr "%s\n\n" txt
7108 ) all_functions_sorted
7110 (* Generate a C function prototype. *)
7111 and generate_prototype ?(extern = true) ?(static = false) ?(semicolon = true)
7112 ?(single_line = false) ?(newline = false) ?(in_daemon = false)
7114 ?handle name style =
7115 if extern then pr "extern ";
7116 if static then pr "static ";
7117 (match fst style with
7119 | RInt _ -> pr "int "
7120 | RInt64 _ -> pr "int64_t "
7121 | RBool _ -> pr "int "
7122 | RConstString _ | RConstOptString _ -> pr "const char *"
7123 | RString _ | RBufferOut _ -> pr "char *"
7124 | RStringList _ | RHashtable _ -> pr "char **"
7125 | RStruct (_, typ) ->
7126 if not in_daemon then pr "struct guestfs_%s *" typ
7127 else pr "guestfs_int_%s *" typ
7128 | RStructList (_, typ) ->
7129 if not in_daemon then pr "struct guestfs_%s_list *" typ
7130 else pr "guestfs_int_%s_list *" typ
7132 let is_RBufferOut = match fst style with RBufferOut _ -> true | _ -> false in
7133 pr "%s%s (" prefix name;
7134 if handle = None && List.length (snd style) = 0 && not is_RBufferOut then
7137 let comma = ref false in
7140 | Some handle -> pr "guestfs_h *%s" handle; comma := true
7144 if single_line then pr ", " else pr ",\n\t\t"
7151 | Device n | Dev_or_Path n
7155 pr "const char *%s" n
7156 | StringList n | DeviceList n ->
7158 pr "char *const *%s" n
7159 | Bool n -> next (); pr "int %s" n
7160 | Int n -> next (); pr "int %s" n
7161 | Int64 n -> next (); pr "int64_t %s" n
7164 if not in_daemon then (next (); pr "const char *%s" n)
7166 if is_RBufferOut then (next (); pr "size_t *size_r");
7169 if semicolon then pr ";";
7170 if newline then pr "\n"
7172 (* Generate C call arguments, eg "(handle, foo, bar)" *)
7173 and generate_c_call_args ?handle ?(decl = false) style =
7175 let comma = ref false in
7177 if !comma then pr ", ";
7182 | Some handle -> pr "%s" handle; comma := true
7187 pr "%s" (name_of_argt arg)
7189 (* For RBufferOut calls, add implicit &size parameter. *)
7191 match fst style with
7199 (* Generate the OCaml bindings interface. *)
7200 and generate_ocaml_mli () =
7201 generate_header OCamlStyle LGPLv2;
7204 (** For API documentation you should refer to the C API
7205 in the guestfs(3) manual page. The OCaml API uses almost
7206 exactly the same calls. *)
7209 (** A [guestfs_h] handle. *)
7211 exception Error of string
7212 (** This exception is raised when there is an error. *)
7214 exception Handle_closed of string
7215 (** This exception is raised if you use a {!Guestfs.t} handle
7216 after calling {!close} on it. The string is the name of
7219 val create : unit -> t
7220 (** Create a {!Guestfs.t} handle. *)
7222 val close : t -> unit
7223 (** Close the {!Guestfs.t} handle and free up all resources used
7226 Handles are closed by the garbage collector when they become
7227 unreferenced, but callers can call this in order to provide
7228 predictable cleanup. *)
7231 generate_ocaml_structure_decls ();
7235 fun (name, style, _, _, _, shortdesc, _) ->
7236 generate_ocaml_prototype name style;
7237 pr "(** %s *)\n" shortdesc;
7239 ) all_functions_sorted
7241 (* Generate the OCaml bindings implementation. *)
7242 and generate_ocaml_ml () =
7243 generate_header OCamlStyle LGPLv2;
7248 exception Error of string
7249 exception Handle_closed of string
7251 external create : unit -> t = \"ocaml_guestfs_create\"
7252 external close : t -> unit = \"ocaml_guestfs_close\"
7254 (* Give the exceptions names, so they can be raised from the C code. *)
7256 Callback.register_exception \"ocaml_guestfs_error\" (Error \"\");
7257 Callback.register_exception \"ocaml_guestfs_closed\" (Handle_closed \"\")
7261 generate_ocaml_structure_decls ();
7265 fun (name, style, _, _, _, shortdesc, _) ->
7266 generate_ocaml_prototype ~is_external:true name style;
7267 ) all_functions_sorted
7269 (* Generate the OCaml bindings C implementation. *)
7270 and generate_ocaml_c () =
7271 generate_header CStyle LGPLv2;
7278 #include <caml/config.h>
7279 #include <caml/alloc.h>
7280 #include <caml/callback.h>
7281 #include <caml/fail.h>
7282 #include <caml/memory.h>
7283 #include <caml/mlvalues.h>
7284 #include <caml/signals.h>
7286 #include <guestfs.h>
7288 #include \"guestfs_c.h\"
7290 /* Copy a hashtable of string pairs into an assoc-list. We return
7291 * the list in reverse order, but hashtables aren't supposed to be
7294 static CAMLprim value
7295 copy_table (char * const * argv)
7298 CAMLlocal5 (rv, pairv, kv, vv, cons);
7302 for (i = 0; argv[i] != NULL; i += 2) {
7303 kv = caml_copy_string (argv[i]);
7304 vv = caml_copy_string (argv[i+1]);
7305 pairv = caml_alloc (2, 0);
7306 Store_field (pairv, 0, kv);
7307 Store_field (pairv, 1, vv);
7308 cons = caml_alloc (2, 0);
7309 Store_field (cons, 1, rv);
7311 Store_field (cons, 0, pairv);
7319 (* Struct copy functions. *)
7321 let emit_ocaml_copy_list_function typ =
7322 pr "static CAMLprim value\n";
7323 pr "copy_%s_list (const struct guestfs_%s_list *%ss)\n" typ typ typ;
7325 pr " CAMLparam0 ();\n";
7326 pr " CAMLlocal2 (rv, v);\n";
7327 pr " unsigned int i;\n";
7329 pr " if (%ss->len == 0)\n" typ;
7330 pr " CAMLreturn (Atom (0));\n";
7332 pr " rv = caml_alloc (%ss->len, 0);\n" typ;
7333 pr " for (i = 0; i < %ss->len; ++i) {\n" typ;
7334 pr " v = copy_%s (&%ss->val[i]);\n" typ typ;
7335 pr " caml_modify (&Field (rv, i), v);\n";
7337 pr " CAMLreturn (rv);\n";
7345 let has_optpercent_col =
7346 List.exists (function (_, FOptPercent) -> true | _ -> false) cols in
7348 pr "static CAMLprim value\n";
7349 pr "copy_%s (const struct guestfs_%s *%s)\n" typ typ typ;
7351 pr " CAMLparam0 ();\n";
7352 if has_optpercent_col then
7353 pr " CAMLlocal3 (rv, v, v2);\n"
7355 pr " CAMLlocal2 (rv, v);\n";
7357 pr " rv = caml_alloc (%d, 0);\n" (List.length cols);
7362 pr " v = caml_copy_string (%s->%s);\n" typ name
7364 pr " v = caml_alloc_string (%s->%s_len);\n" typ name;
7365 pr " memcpy (String_val (v), %s->%s, %s->%s_len);\n"
7368 pr " v = caml_alloc_string (32);\n";
7369 pr " memcpy (String_val (v), %s->%s, 32);\n" typ name
7370 | name, (FBytes|FInt64|FUInt64) ->
7371 pr " v = caml_copy_int64 (%s->%s);\n" typ name
7372 | name, (FInt32|FUInt32) ->
7373 pr " v = caml_copy_int32 (%s->%s);\n" typ name
7374 | name, FOptPercent ->
7375 pr " if (%s->%s >= 0) { /* Some %s */\n" typ name name;
7376 pr " v2 = caml_copy_double (%s->%s);\n" typ name;
7377 pr " v = caml_alloc (1, 0);\n";
7378 pr " Store_field (v, 0, v2);\n";
7379 pr " } else /* None */\n";
7380 pr " v = Val_int (0);\n";
7382 pr " v = Val_int (%s->%s);\n" typ name
7384 pr " Store_field (rv, %d, v);\n" i
7386 pr " CAMLreturn (rv);\n";
7391 (* Emit a copy_TYPE_list function definition only if that function is used. *)
7394 | typ, (RStructListOnly | RStructAndList) ->
7395 (* generate the function for typ *)
7396 emit_ocaml_copy_list_function typ
7397 | typ, _ -> () (* empty *)
7398 ) (rstructs_used_by all_functions);
7402 fun (name, style, _, _, _, _, _) ->
7403 pr "/* Automatically generated wrapper for function\n";
7405 generate_ocaml_prototype name style;
7410 "gv" :: List.map (fun arg -> name_of_argt arg ^ "v") (snd style) in
7412 let needs_extra_vs =
7413 match fst style with RConstOptString _ -> true | _ -> false in
7415 pr "/* Emit prototype to appease gcc's -Wmissing-prototypes. */\n";
7416 pr "CAMLprim value ocaml_guestfs_%s (value %s" name (List.hd params);
7417 List.iter (pr ", value %s") (List.tl params); pr ");\n";
7420 pr "CAMLprim value\n";
7421 pr "ocaml_guestfs_%s (value %s" name (List.hd params);
7422 List.iter (pr ", value %s") (List.tl params);
7427 | [p1; p2; p3; p4; p5] ->
7428 pr " CAMLparam5 (%s);\n" (String.concat ", " params)
7429 | p1 :: p2 :: p3 :: p4 :: p5 :: rest ->
7430 pr " CAMLparam5 (%s);\n" (String.concat ", " [p1; p2; p3; p4; p5]);
7431 pr " CAMLxparam%d (%s);\n"
7432 (List.length rest) (String.concat ", " rest)
7434 pr " CAMLparam%d (%s);\n" (List.length ps) (String.concat ", " ps)
7436 if not needs_extra_vs then
7437 pr " CAMLlocal1 (rv);\n"
7439 pr " CAMLlocal3 (rv, v, v2);\n";
7442 pr " guestfs_h *g = Guestfs_val (gv);\n";
7443 pr " if (g == NULL)\n";
7444 pr " ocaml_guestfs_raise_closed (\"%s\");\n" name;
7450 | Device n | Dev_or_Path n
7454 pr " const char *%s = String_val (%sv);\n" n n
7456 pr " const char *%s =\n" n;
7457 pr " %sv != Val_int (0) ? String_val (Field (%sv, 0)) : NULL;\n"
7459 | StringList n | DeviceList n ->
7460 pr " char **%s = ocaml_guestfs_strings_val (g, %sv);\n" n n
7462 pr " int %s = Bool_val (%sv);\n" n n
7464 pr " int %s = Int_val (%sv);\n" n n
7466 pr " int64_t %s = Int64_val (%sv);\n" n n
7469 match fst style with
7470 | RErr -> pr " int r;\n"; "-1"
7471 | RInt _ -> pr " int r;\n"; "-1"
7472 | RInt64 _ -> pr " int64_t r;\n"; "-1"
7473 | RBool _ -> pr " int r;\n"; "-1"
7474 | RConstString _ | RConstOptString _ ->
7475 pr " const char *r;\n"; "NULL"
7476 | RString _ -> pr " char *r;\n"; "NULL"
7481 | RStruct (_, typ) ->
7482 pr " struct guestfs_%s *r;\n" typ; "NULL"
7483 | RStructList (_, typ) ->
7484 pr " struct guestfs_%s_list *r;\n" typ; "NULL"
7491 pr " size_t size;\n";
7495 pr " caml_enter_blocking_section ();\n";
7496 pr " r = guestfs_%s " name;
7497 generate_c_call_args ~handle:"g" style;
7499 pr " caml_leave_blocking_section ();\n";
7503 | StringList n | DeviceList n ->
7504 pr " ocaml_guestfs_free_strings (%s);\n" n;
7505 | Pathname _ | Device _ | Dev_or_Path _ | String _ | OptString _
7506 | Bool _ | Int _ | Int64 _
7507 | FileIn _ | FileOut _ -> ()
7510 pr " if (r == %s)\n" error_code;
7511 pr " ocaml_guestfs_raise_error (g, \"%s\");\n" name;
7514 (match fst style with
7515 | RErr -> pr " rv = Val_unit;\n"
7516 | RInt _ -> pr " rv = Val_int (r);\n"
7518 pr " rv = caml_copy_int64 (r);\n"
7519 | RBool _ -> pr " rv = Val_bool (r);\n"
7521 pr " rv = caml_copy_string (r);\n"
7522 | RConstOptString _ ->
7523 pr " if (r) { /* Some string */\n";
7524 pr " v = caml_alloc (1, 0);\n";
7525 pr " v2 = caml_copy_string (r);\n";
7526 pr " Store_field (v, 0, v2);\n";
7527 pr " } else /* None */\n";
7528 pr " v = Val_int (0);\n";
7530 pr " rv = caml_copy_string (r);\n";
7533 pr " rv = caml_copy_string_array ((const char **) r);\n";
7534 pr " for (i = 0; r[i] != NULL; ++i) free (r[i]);\n";
7536 | RStruct (_, typ) ->
7537 pr " rv = copy_%s (r);\n" typ;
7538 pr " guestfs_free_%s (r);\n" typ;
7539 | RStructList (_, typ) ->
7540 pr " rv = copy_%s_list (r);\n" typ;
7541 pr " guestfs_free_%s_list (r);\n" typ;
7543 pr " rv = copy_table (r);\n";
7544 pr " for (i = 0; r[i] != NULL; ++i) free (r[i]);\n";
7547 pr " rv = caml_alloc_string (size);\n";
7548 pr " memcpy (String_val (rv), r, size);\n";
7551 pr " CAMLreturn (rv);\n";
7555 if List.length params > 5 then (
7556 pr "/* Emit prototype to appease gcc's -Wmissing-prototypes. */\n";
7557 pr "CAMLprim value ";
7558 pr "ocaml_guestfs_%s_byte (value *argv, int argn);\n" name;
7559 pr "CAMLprim value\n";
7560 pr "ocaml_guestfs_%s_byte (value *argv, int argn)\n" name;
7562 pr " return ocaml_guestfs_%s (argv[0]" name;
7563 iteri (fun i _ -> pr ", argv[%d]" i) (List.tl params);
7568 ) all_functions_sorted
7570 and generate_ocaml_structure_decls () =
7573 pr "type %s = {\n" typ;
7576 | name, FString -> pr " %s : string;\n" name
7577 | name, FBuffer -> pr " %s : string;\n" name
7578 | name, FUUID -> pr " %s : string;\n" name
7579 | name, (FBytes|FInt64|FUInt64) -> pr " %s : int64;\n" name
7580 | name, (FInt32|FUInt32) -> pr " %s : int32;\n" name
7581 | name, FChar -> pr " %s : char;\n" name
7582 | name, FOptPercent -> pr " %s : float option;\n" name
7588 and generate_ocaml_prototype ?(is_external = false) name style =
7589 if is_external then pr "external " else pr "val ";
7590 pr "%s : t -> " name;
7593 | Pathname _ | Device _ | Dev_or_Path _ | String _ | FileIn _ | FileOut _ -> pr "string -> "
7594 | OptString _ -> pr "string option -> "
7595 | StringList _ | DeviceList _ -> pr "string array -> "
7596 | Bool _ -> pr "bool -> "
7597 | Int _ -> pr "int -> "
7598 | Int64 _ -> pr "int64 -> "
7600 (match fst style with
7601 | RErr -> pr "unit" (* all errors are turned into exceptions *)
7602 | RInt _ -> pr "int"
7603 | RInt64 _ -> pr "int64"
7604 | RBool _ -> pr "bool"
7605 | RConstString _ -> pr "string"
7606 | RConstOptString _ -> pr "string option"
7607 | RString _ | RBufferOut _ -> pr "string"
7608 | RStringList _ -> pr "string array"
7609 | RStruct (_, typ) -> pr "%s" typ
7610 | RStructList (_, typ) -> pr "%s array" typ
7611 | RHashtable _ -> pr "(string * string) list"
7613 if is_external then (
7615 if List.length (snd style) + 1 > 5 then
7616 pr "\"ocaml_guestfs_%s_byte\" " name;
7617 pr "\"ocaml_guestfs_%s\"" name
7621 (* Generate Perl xs code, a sort of crazy variation of C with macros. *)
7622 and generate_perl_xs () =
7623 generate_header CStyle LGPLv2;
7626 #include \"EXTERN.h\"
7630 #include <guestfs.h>
7633 #define PRId64 \"lld\"
7637 my_newSVll(long long val) {
7638 #ifdef USE_64_BIT_ALL
7639 return newSViv(val);
7643 len = snprintf(buf, 100, \"%%\" PRId64, val);
7644 return newSVpv(buf, len);
7649 #define PRIu64 \"llu\"
7653 my_newSVull(unsigned long long val) {
7654 #ifdef USE_64_BIT_ALL
7655 return newSVuv(val);
7659 len = snprintf(buf, 100, \"%%\" PRIu64, val);
7660 return newSVpv(buf, len);
7664 /* http://www.perlmonks.org/?node_id=680842 */
7666 XS_unpack_charPtrPtr (SV *arg) {
7671 if (!arg || !SvOK (arg) || !SvROK (arg) || SvTYPE (SvRV (arg)) != SVt_PVAV)
7672 croak (\"array reference expected\");
7674 av = (AV *)SvRV (arg);
7675 ret = malloc ((av_len (av) + 1 + 1) * sizeof (char *));
7677 croak (\"malloc failed\");
7679 for (i = 0; i <= av_len (av); i++) {
7680 SV **elem = av_fetch (av, i, 0);
7682 if (!elem || !*elem)
7683 croak (\"missing element in list\");
7685 ret[i] = SvPV_nolen (*elem);
7693 MODULE = Sys::Guestfs PACKAGE = Sys::Guestfs
7700 RETVAL = guestfs_create ();
7702 croak (\"could not create guestfs handle\");
7703 guestfs_set_error_handler (RETVAL, NULL, NULL);
7716 fun (name, style, _, _, _, _, _) ->
7717 (match fst style with
7718 | RErr -> pr "void\n"
7719 | RInt _ -> pr "SV *\n"
7720 | RInt64 _ -> pr "SV *\n"
7721 | RBool _ -> pr "SV *\n"
7722 | RConstString _ -> pr "SV *\n"
7723 | RConstOptString _ -> pr "SV *\n"
7724 | RString _ -> pr "SV *\n"
7725 | RBufferOut _ -> pr "SV *\n"
7727 | RStruct _ | RStructList _
7729 pr "void\n" (* all lists returned implictly on the stack *)
7731 (* Call and arguments. *)
7733 generate_c_call_args ~handle:"g" ~decl:true style;
7735 pr " guestfs_h *g;\n";
7739 | Pathname n | Device n | Dev_or_Path n | String n | FileIn n | FileOut n ->
7742 (* http://www.perlmonks.org/?node_id=554277
7743 * Note that the implicit handle argument means we have
7744 * to add 1 to the ST(x) operator.
7746 pr " char *%s = SvOK(ST(%d)) ? SvPV_nolen(ST(%d)) : NULL;\n" n (i+1) (i+1)
7747 | StringList n | DeviceList n -> pr " char **%s;\n" n
7748 | Bool n -> pr " int %s;\n" n
7749 | Int n -> pr " int %s;\n" n
7750 | Int64 n -> pr " int64_t %s;\n" n
7753 let do_cleanups () =
7756 | Pathname _ | Device _ | Dev_or_Path _ | String _ | OptString _
7757 | Bool _ | Int _ | Int64 _
7758 | FileIn _ | FileOut _ -> ()
7759 | StringList n | DeviceList n -> pr " free (%s);\n" n
7764 (match fst style with
7769 pr " r = guestfs_%s " name;
7770 generate_c_call_args ~handle:"g" style;
7773 pr " if (r == -1)\n";
7774 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
7780 pr " %s = guestfs_%s " n name;
7781 generate_c_call_args ~handle:"g" style;
7784 pr " if (%s == -1)\n" n;
7785 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
7786 pr " RETVAL = newSViv (%s);\n" n;
7791 pr " int64_t %s;\n" n;
7793 pr " %s = guestfs_%s " n name;
7794 generate_c_call_args ~handle:"g" style;
7797 pr " if (%s == -1)\n" n;
7798 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
7799 pr " RETVAL = my_newSVll (%s);\n" n;
7804 pr " const char *%s;\n" n;
7806 pr " %s = guestfs_%s " n name;
7807 generate_c_call_args ~handle:"g" style;
7810 pr " if (%s == NULL)\n" n;
7811 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
7812 pr " RETVAL = newSVpv (%s, 0);\n" n;
7815 | RConstOptString n ->
7817 pr " const char *%s;\n" n;
7819 pr " %s = guestfs_%s " n name;
7820 generate_c_call_args ~handle:"g" style;
7823 pr " if (%s == NULL)\n" n;
7824 pr " RETVAL = &PL_sv_undef;\n";
7826 pr " RETVAL = newSVpv (%s, 0);\n" n;
7831 pr " char *%s;\n" n;
7833 pr " %s = guestfs_%s " n name;
7834 generate_c_call_args ~handle:"g" style;
7837 pr " if (%s == NULL)\n" n;
7838 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
7839 pr " RETVAL = newSVpv (%s, 0);\n" n;
7840 pr " free (%s);\n" n;
7843 | RStringList n | RHashtable n ->
7845 pr " char **%s;\n" n;
7848 pr " %s = guestfs_%s " n name;
7849 generate_c_call_args ~handle:"g" style;
7852 pr " if (%s == NULL)\n" n;
7853 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
7854 pr " for (n = 0; %s[n] != NULL; ++n) /**/;\n" n;
7855 pr " EXTEND (SP, n);\n";
7856 pr " for (i = 0; i < n; ++i) {\n";
7857 pr " PUSHs (sv_2mortal (newSVpv (%s[i], 0)));\n" n;
7858 pr " free (%s[i]);\n" n;
7860 pr " free (%s);\n" n;
7861 | RStruct (n, typ) ->
7862 let cols = cols_of_struct typ in
7863 generate_perl_struct_code typ cols name style n do_cleanups
7864 | RStructList (n, typ) ->
7865 let cols = cols_of_struct typ in
7866 generate_perl_struct_list_code typ cols name style n do_cleanups
7869 pr " char *%s;\n" n;
7870 pr " size_t size;\n";
7872 pr " %s = guestfs_%s " n name;
7873 generate_c_call_args ~handle:"g" style;
7876 pr " if (%s == NULL)\n" n;
7877 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
7878 pr " RETVAL = newSVpv (%s, size);\n" n;
7879 pr " free (%s);\n" n;
7887 and generate_perl_struct_list_code typ cols name style n do_cleanups =
7889 pr " struct guestfs_%s_list *%s;\n" typ n;
7893 pr " %s = guestfs_%s " n name;
7894 generate_c_call_args ~handle:"g" style;
7897 pr " if (%s == NULL)\n" n;
7898 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
7899 pr " EXTEND (SP, %s->len);\n" n;
7900 pr " for (i = 0; i < %s->len; ++i) {\n" n;
7901 pr " hv = newHV ();\n";
7905 pr " (void) hv_store (hv, \"%s\", %d, newSVpv (%s->val[i].%s, 0), 0);\n"
7906 name (String.length name) n name
7908 pr " (void) hv_store (hv, \"%s\", %d, newSVpv (%s->val[i].%s, 32), 0);\n"
7909 name (String.length name) n name
7911 pr " (void) hv_store (hv, \"%s\", %d, newSVpv (%s->val[i].%s, %s->val[i].%s_len), 0);\n"
7912 name (String.length name) n name n name
7913 | name, (FBytes|FUInt64) ->
7914 pr " (void) hv_store (hv, \"%s\", %d, my_newSVull (%s->val[i].%s), 0);\n"
7915 name (String.length name) n name
7917 pr " (void) hv_store (hv, \"%s\", %d, my_newSVll (%s->val[i].%s), 0);\n"
7918 name (String.length name) n name
7919 | name, (FInt32|FUInt32) ->
7920 pr " (void) hv_store (hv, \"%s\", %d, newSVnv (%s->val[i].%s), 0);\n"
7921 name (String.length name) n name
7923 pr " (void) hv_store (hv, \"%s\", %d, newSVpv (&%s->val[i].%s, 1), 0);\n"
7924 name (String.length name) n name
7925 | name, FOptPercent ->
7926 pr " (void) hv_store (hv, \"%s\", %d, newSVnv (%s->val[i].%s), 0);\n"
7927 name (String.length name) n name
7929 pr " PUSHs (sv_2mortal (newRV ((SV *) hv)));\n";
7931 pr " guestfs_free_%s_list (%s);\n" typ n
7933 and generate_perl_struct_code typ cols name style n do_cleanups =
7935 pr " struct guestfs_%s *%s;\n" typ n;
7937 pr " %s = guestfs_%s " n name;
7938 generate_c_call_args ~handle:"g" style;
7941 pr " if (%s == NULL)\n" n;
7942 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
7943 pr " EXTEND (SP, 2 * %d);\n" (List.length cols);
7945 fun ((name, _) as col) ->
7946 pr " PUSHs (sv_2mortal (newSVpv (\"%s\", 0)));\n" name;
7950 pr " PUSHs (sv_2mortal (newSVpv (%s->%s, 0)));\n"
7953 pr " PUSHs (sv_2mortal (newSVpv (%s->%s, %s->%s_len)));\n"
7956 pr " PUSHs (sv_2mortal (newSVpv (%s->%s, 32)));\n"
7958 | name, (FBytes|FUInt64) ->
7959 pr " PUSHs (sv_2mortal (my_newSVull (%s->%s)));\n"
7962 pr " PUSHs (sv_2mortal (my_newSVll (%s->%s)));\n"
7964 | name, (FInt32|FUInt32) ->
7965 pr " PUSHs (sv_2mortal (newSVnv (%s->%s)));\n"
7968 pr " PUSHs (sv_2mortal (newSVpv (&%s->%s, 1)));\n"
7970 | name, FOptPercent ->
7971 pr " PUSHs (sv_2mortal (newSVnv (%s->%s)));\n"
7974 pr " free (%s);\n" n
7976 (* Generate Sys/Guestfs.pm. *)
7977 and generate_perl_pm () =
7978 generate_header HashStyle LGPLv2;
7985 Sys::Guestfs - Perl bindings for libguestfs
7991 my $h = Sys::Guestfs->new ();
7992 $h->add_drive ('guest.img');
7994 $h->mount ('/dev/sda1', '/');
7995 $h->touch ('/hello');
8000 The C<Sys::Guestfs> module provides a Perl XS binding to the
8001 libguestfs API for examining and modifying virtual machine
8004 Amongst the things this is good for: making batch configuration
8005 changes to guests, getting disk used/free statistics (see also:
8006 virt-df), migrating between virtualization systems (see also:
8007 virt-p2v), performing partial backups, performing partial guest
8008 clones, cloning guests and changing registry/UUID/hostname info, and
8011 Libguestfs uses Linux kernel and qemu code, and can access any type of
8012 guest filesystem that Linux and qemu can, including but not limited
8013 to: ext2/3/4, btrfs, FAT and NTFS, LVM, many different disk partition
8014 schemes, qcow, qcow2, vmdk.
8016 Libguestfs provides ways to enumerate guest storage (eg. partitions,
8017 LVs, what filesystem is in each LV, etc.). It can also run commands
8018 in the context of the guest. Also you can access filesystems over FTP.
8020 See also L<Sys::Guestfs::Lib(3)> for a set of useful library
8021 functions for using libguestfs from Perl, including integration
8026 All errors turn into calls to C<croak> (see L<Carp(3)>).
8034 package Sys::Guestfs;
8040 XSLoader::load ('Sys::Guestfs');
8042 =item $h = Sys::Guestfs->new ();
8044 Create a new guestfs handle.
8050 my $class = ref ($proto) || $proto;
8052 my $self = Sys::Guestfs::_create ();
8053 bless $self, $class;
8059 (* Actions. We only need to print documentation for these as
8060 * they are pulled in from the XS code automatically.
8063 fun (name, style, _, flags, _, _, longdesc) ->
8064 if not (List.mem NotInDocs flags) then (
8065 let longdesc = replace_str longdesc "C<guestfs_" "C<$h-E<gt>" in
8067 generate_perl_prototype name style;
8069 pr "%s\n\n" longdesc;
8070 if List.mem ProtocolLimitWarning flags then
8071 pr "%s\n\n" protocol_limit_warning;
8072 if List.mem DangerWillRobinson flags then
8073 pr "%s\n\n" danger_will_robinson;
8074 match deprecation_notice flags with
8076 | Some txt -> pr "%s\n\n" txt
8078 ) all_functions_sorted;
8090 Copyright (C) 2009 Red Hat Inc.
8094 Please see the file COPYING.LIB for the full license.
8100 L<http://libguestfs.org>,
8101 L<Sys::Guestfs::Lib(3)>.
8106 and generate_perl_prototype name style =
8107 (match fst style with
8115 | RBufferOut n -> pr "$%s = " n
8117 | RHashtable n -> pr "%%%s = " n
8119 | RStructList (n,_) -> pr "@%s = " n
8122 let comma = ref false in
8125 if !comma then pr ", ";
8128 | Pathname n | Device n | Dev_or_Path n | String n
8129 | OptString n | Bool n | Int n | Int64 n | FileIn n | FileOut n ->
8131 | StringList n | DeviceList n ->
8136 (* Generate Python C module. *)
8137 and generate_python_c () =
8138 generate_header CStyle LGPLv2;
8147 #include \"guestfs.h\"
8155 get_handle (PyObject *obj)
8158 assert (obj != Py_None);
8159 return ((Pyguestfs_Object *) obj)->g;
8163 put_handle (guestfs_h *g)
8167 PyCObject_FromVoidPtrAndDesc ((void *) g, (char *) \"guestfs_h\", NULL);
8170 /* This list should be freed (but not the strings) after use. */
8172 get_string_list (PyObject *obj)
8179 if (!PyList_Check (obj)) {
8180 PyErr_SetString (PyExc_RuntimeError, \"expecting a list parameter\");
8184 len = PyList_Size (obj);
8185 r = malloc (sizeof (char *) * (len+1));
8187 PyErr_SetString (PyExc_RuntimeError, \"get_string_list: out of memory\");
8191 for (i = 0; i < len; ++i)
8192 r[i] = PyString_AsString (PyList_GetItem (obj, i));
8199 put_string_list (char * const * const argv)
8204 for (argc = 0; argv[argc] != NULL; ++argc)
8207 list = PyList_New (argc);
8208 for (i = 0; i < argc; ++i)
8209 PyList_SetItem (list, i, PyString_FromString (argv[i]));
8215 put_table (char * const * const argv)
8217 PyObject *list, *item;
8220 for (argc = 0; argv[argc] != NULL; ++argc)
8223 list = PyList_New (argc >> 1);
8224 for (i = 0; i < argc; i += 2) {
8225 item = PyTuple_New (2);
8226 PyTuple_SetItem (item, 0, PyString_FromString (argv[i]));
8227 PyTuple_SetItem (item, 1, PyString_FromString (argv[i+1]));
8228 PyList_SetItem (list, i >> 1, item);
8235 free_strings (char **argv)
8239 for (argc = 0; argv[argc] != NULL; ++argc)
8245 py_guestfs_create (PyObject *self, PyObject *args)
8249 g = guestfs_create ();
8251 PyErr_SetString (PyExc_RuntimeError,
8252 \"guestfs.create: failed to allocate handle\");
8255 guestfs_set_error_handler (g, NULL, NULL);
8256 return put_handle (g);
8260 py_guestfs_close (PyObject *self, PyObject *args)
8265 if (!PyArg_ParseTuple (args, (char *) \"O:guestfs_close\", &py_g))
8267 g = get_handle (py_g);
8271 Py_INCREF (Py_None);
8277 let emit_put_list_function typ =
8278 pr "static PyObject *\n";
8279 pr "put_%s_list (struct guestfs_%s_list *%ss)\n" typ typ typ;
8281 pr " PyObject *list;\n";
8284 pr " list = PyList_New (%ss->len);\n" typ;
8285 pr " for (i = 0; i < %ss->len; ++i)\n" typ;
8286 pr " PyList_SetItem (list, i, put_%s (&%ss->val[i]));\n" typ typ;
8287 pr " return list;\n";
8292 (* Structures, turned into Python dictionaries. *)
8295 pr "static PyObject *\n";
8296 pr "put_%s (struct guestfs_%s *%s)\n" typ typ typ;
8298 pr " PyObject *dict;\n";
8300 pr " dict = PyDict_New ();\n";
8304 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
8305 pr " PyString_FromString (%s->%s));\n"
8308 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
8309 pr " PyString_FromStringAndSize (%s->%s, %s->%s_len));\n"
8312 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
8313 pr " PyString_FromStringAndSize (%s->%s, 32));\n"
8315 | name, (FBytes|FUInt64) ->
8316 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
8317 pr " PyLong_FromUnsignedLongLong (%s->%s));\n"
8320 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
8321 pr " PyLong_FromLongLong (%s->%s));\n"
8324 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
8325 pr " PyLong_FromUnsignedLong (%s->%s));\n"
8328 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
8329 pr " PyLong_FromLong (%s->%s));\n"
8331 | name, FOptPercent ->
8332 pr " if (%s->%s >= 0)\n" typ name;
8333 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
8334 pr " PyFloat_FromDouble ((double) %s->%s));\n"
8337 pr " Py_INCREF (Py_None);\n";
8338 pr " PyDict_SetItemString (dict, \"%s\", Py_None);\n" name;
8341 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
8342 pr " PyString_FromStringAndSize (&dirent->%s, 1));\n" name
8344 pr " return dict;\n";
8350 (* Emit a put_TYPE_list function definition only if that function is used. *)
8353 | typ, (RStructListOnly | RStructAndList) ->
8354 (* generate the function for typ *)
8355 emit_put_list_function typ
8356 | typ, _ -> () (* empty *)
8357 ) (rstructs_used_by all_functions);
8359 (* Python wrapper functions. *)
8361 fun (name, style, _, _, _, _, _) ->
8362 pr "static PyObject *\n";
8363 pr "py_guestfs_%s (PyObject *self, PyObject *args)\n" name;
8366 pr " PyObject *py_g;\n";
8367 pr " guestfs_h *g;\n";
8368 pr " PyObject *py_r;\n";
8371 match fst style with
8372 | RErr | RInt _ | RBool _ -> pr " int r;\n"; "-1"
8373 | RInt64 _ -> pr " int64_t r;\n"; "-1"
8374 | RConstString _ | RConstOptString _ ->
8375 pr " const char *r;\n"; "NULL"
8376 | RString _ -> pr " char *r;\n"; "NULL"
8377 | RStringList _ | RHashtable _ -> pr " char **r;\n"; "NULL"
8378 | RStruct (_, typ) -> pr " struct guestfs_%s *r;\n" typ; "NULL"
8379 | RStructList (_, typ) ->
8380 pr " struct guestfs_%s_list *r;\n" typ; "NULL"
8383 pr " size_t size;\n";
8388 | Pathname n | Device n | Dev_or_Path n | String n | FileIn n | FileOut n ->
8389 pr " const char *%s;\n" n
8390 | OptString n -> pr " const char *%s;\n" n
8391 | StringList n | DeviceList n ->
8392 pr " PyObject *py_%s;\n" n;
8393 pr " char **%s;\n" n
8394 | Bool n -> pr " int %s;\n" n
8395 | Int n -> pr " int %s;\n" n
8396 | Int64 n -> pr " long long %s;\n" n
8401 (* Convert the parameters. *)
8402 pr " if (!PyArg_ParseTuple (args, (char *) \"O";
8405 | Pathname _ | Device _ | Dev_or_Path _ | String _ | FileIn _ | FileOut _ -> pr "s"
8406 | OptString _ -> pr "z"
8407 | StringList _ | DeviceList _ -> pr "O"
8408 | Bool _ -> pr "i" (* XXX Python has booleans? *)
8410 | Int64 _ -> pr "L" (* XXX Whoever thought it was a good idea to
8411 * emulate C's int/long/long long in Python?
8414 pr ":guestfs_%s\",\n" name;
8418 | Pathname n | Device n | Dev_or_Path n | String n | FileIn n | FileOut n -> pr ", &%s" n
8419 | OptString n -> pr ", &%s" n
8420 | StringList n | DeviceList n -> pr ", &py_%s" n
8421 | Bool n -> pr ", &%s" n
8422 | Int n -> pr ", &%s" n
8423 | Int64 n -> pr ", &%s" n
8427 pr " return NULL;\n";
8429 pr " g = get_handle (py_g);\n";
8432 | Pathname _ | Device _ | Dev_or_Path _ | String _
8433 | FileIn _ | FileOut _ | OptString _ | Bool _ | Int _ | Int64 _ -> ()
8434 | StringList n | DeviceList n ->
8435 pr " %s = get_string_list (py_%s);\n" n n;
8436 pr " if (!%s) return NULL;\n" n
8441 pr " r = guestfs_%s " name;
8442 generate_c_call_args ~handle:"g" style;
8447 | Pathname _ | Device _ | Dev_or_Path _ | String _
8448 | FileIn _ | FileOut _ | OptString _ | Bool _ | Int _ | Int64 _ -> ()
8449 | StringList n | DeviceList n ->
8450 pr " free (%s);\n" n
8453 pr " if (r == %s) {\n" error_code;
8454 pr " PyErr_SetString (PyExc_RuntimeError, guestfs_last_error (g));\n";
8455 pr " return NULL;\n";
8459 (match fst style with
8461 pr " Py_INCREF (Py_None);\n";
8462 pr " py_r = Py_None;\n"
8464 | RBool _ -> pr " py_r = PyInt_FromLong ((long) r);\n"
8465 | RInt64 _ -> pr " py_r = PyLong_FromLongLong (r);\n"
8466 | RConstString _ -> pr " py_r = PyString_FromString (r);\n"
8467 | RConstOptString _ ->
8469 pr " py_r = PyString_FromString (r);\n";
8471 pr " Py_INCREF (Py_None);\n";
8472 pr " py_r = Py_None;\n";
8475 pr " py_r = PyString_FromString (r);\n";
8478 pr " py_r = put_string_list (r);\n";
8479 pr " free_strings (r);\n"
8480 | RStruct (_, typ) ->
8481 pr " py_r = put_%s (r);\n" typ;
8482 pr " guestfs_free_%s (r);\n" typ
8483 | RStructList (_, typ) ->
8484 pr " py_r = put_%s_list (r);\n" typ;
8485 pr " guestfs_free_%s_list (r);\n" typ
8487 pr " py_r = put_table (r);\n";
8488 pr " free_strings (r);\n"
8490 pr " py_r = PyString_FromStringAndSize (r, size);\n";
8494 pr " return py_r;\n";
8499 (* Table of functions. *)
8500 pr "static PyMethodDef methods[] = {\n";
8501 pr " { (char *) \"create\", py_guestfs_create, METH_VARARGS, NULL },\n";
8502 pr " { (char *) \"close\", py_guestfs_close, METH_VARARGS, NULL },\n";
8504 fun (name, _, _, _, _, _, _) ->
8505 pr " { (char *) \"%s\", py_guestfs_%s, METH_VARARGS, NULL },\n"
8508 pr " { NULL, NULL, 0, NULL }\n";
8512 (* Init function. *)
8515 initlibguestfsmod (void)
8517 static int initialized = 0;
8519 if (initialized) return;
8520 Py_InitModule ((char *) \"libguestfsmod\", methods);
8525 (* Generate Python module. *)
8526 and generate_python_py () =
8527 generate_header HashStyle LGPLv2;
8530 u\"\"\"Python bindings for libguestfs
8533 g = guestfs.GuestFS ()
8534 g.add_drive (\"guest.img\")
8536 parts = g.list_partitions ()
8538 The guestfs module provides a Python binding to the libguestfs API
8539 for examining and modifying virtual machine disk images.
8541 Amongst the things this is good for: making batch configuration
8542 changes to guests, getting disk used/free statistics (see also:
8543 virt-df), migrating between virtualization systems (see also:
8544 virt-p2v), performing partial backups, performing partial guest
8545 clones, cloning guests and changing registry/UUID/hostname info, and
8548 Libguestfs uses Linux kernel and qemu code, and can access any type of
8549 guest filesystem that Linux and qemu can, including but not limited
8550 to: ext2/3/4, btrfs, FAT and NTFS, LVM, many different disk partition
8551 schemes, qcow, qcow2, vmdk.
8553 Libguestfs provides ways to enumerate guest storage (eg. partitions,
8554 LVs, what filesystem is in each LV, etc.). It can also run commands
8555 in the context of the guest. Also you can access filesystems over FTP.
8557 Errors which happen while using the API are turned into Python
8558 RuntimeError exceptions.
8560 To create a guestfs handle you usually have to perform the following
8563 # Create the handle, call add_drive at least once, and possibly
8564 # several times if the guest has multiple block devices:
8565 g = guestfs.GuestFS ()
8566 g.add_drive (\"guest.img\")
8568 # Launch the qemu subprocess and wait for it to become ready:
8571 # Now you can issue commands, for example:
8576 import libguestfsmod
8579 \"\"\"Instances of this class are libguestfs API handles.\"\"\"
8581 def __init__ (self):
8582 \"\"\"Create a new libguestfs handle.\"\"\"
8583 self._o = libguestfsmod.create ()
8586 libguestfsmod.close (self._o)
8591 fun (name, style, _, flags, _, _, longdesc) ->
8593 generate_py_call_args ~handle:"self" (snd style);
8596 if not (List.mem NotInDocs flags) then (
8597 let doc = replace_str longdesc "C<guestfs_" "C<g." in
8599 match fst style with
8600 | RErr | RInt _ | RInt64 _ | RBool _
8601 | RConstOptString _ | RConstString _
8602 | RString _ | RBufferOut _ -> doc
8604 doc ^ "\n\nThis function returns a list of strings."
8605 | RStruct (_, typ) ->
8606 doc ^ sprintf "\n\nThis function returns a dictionary, with keys matching the various fields in the guestfs_%s structure." typ
8607 | RStructList (_, typ) ->
8608 doc ^ sprintf "\n\nThis function returns a list of %ss. Each %s is represented as a dictionary." typ typ
8610 doc ^ "\n\nThis function returns a dictionary." in
8612 if List.mem ProtocolLimitWarning flags then
8613 doc ^ "\n\n" ^ protocol_limit_warning
8616 if List.mem DangerWillRobinson flags then
8617 doc ^ "\n\n" ^ danger_will_robinson
8620 match deprecation_notice flags with
8622 | Some txt -> doc ^ "\n\n" ^ txt in
8623 let doc = pod2text ~width:60 name doc in
8624 let doc = List.map (fun line -> replace_str line "\\" "\\\\") doc in
8625 let doc = String.concat "\n " doc in
8626 pr " u\"\"\"%s\"\"\"\n" doc;
8628 pr " return libguestfsmod.%s " name;
8629 generate_py_call_args ~handle:"self._o" (snd style);
8634 (* Generate Python call arguments, eg "(handle, foo, bar)" *)
8635 and generate_py_call_args ~handle args =
8637 List.iter (fun arg -> pr ", %s" (name_of_argt arg)) args;
8640 (* Useful if you need the longdesc POD text as plain text. Returns a
8643 * Because this is very slow (the slowest part of autogeneration),
8644 * we memoize the results.
8646 and pod2text ~width name longdesc =
8647 let key = width, name, longdesc in
8648 try Hashtbl.find pod2text_memo key
8650 let filename, chan = Filename.open_temp_file "gen" ".tmp" in
8651 fprintf chan "=head1 %s\n\n%s\n" name longdesc;
8653 let cmd = sprintf "pod2text -w %d %s" width (Filename.quote filename) in
8654 let chan = open_process_in cmd in
8655 let lines = ref [] in
8657 let line = input_line chan in
8658 if i = 1 then (* discard the first line of output *)
8661 let line = triml line in
8662 lines := line :: !lines;
8665 let lines = try loop 1 with End_of_file -> List.rev !lines in
8667 (match close_process_in chan with
8670 failwithf "pod2text: process exited with non-zero status (%d)" i
8671 | WSIGNALED i | WSTOPPED i ->
8672 failwithf "pod2text: process signalled or stopped by signal %d" i
8674 Hashtbl.add pod2text_memo key lines;
8675 pod2text_memo_updated ();
8678 (* Generate ruby bindings. *)
8679 and generate_ruby_c () =
8680 generate_header CStyle LGPLv2;
8688 #include \"guestfs.h\"
8690 #include \"extconf.h\"
8692 /* For Ruby < 1.9 */
8694 #define RARRAY_LEN(r) (RARRAY((r))->len)
8697 static VALUE m_guestfs; /* guestfs module */
8698 static VALUE c_guestfs; /* guestfs_h handle */
8699 static VALUE e_Error; /* used for all errors */
8701 static void ruby_guestfs_free (void *p)
8704 guestfs_close ((guestfs_h *) p);
8707 static VALUE ruby_guestfs_create (VALUE m)
8711 g = guestfs_create ();
8713 rb_raise (e_Error, \"failed to create guestfs handle\");
8715 /* Don't print error messages to stderr by default. */
8716 guestfs_set_error_handler (g, NULL, NULL);
8718 /* Wrap it, and make sure the close function is called when the
8721 return Data_Wrap_Struct (c_guestfs, NULL, ruby_guestfs_free, g);
8724 static VALUE ruby_guestfs_close (VALUE gv)
8727 Data_Get_Struct (gv, guestfs_h, g);
8729 ruby_guestfs_free (g);
8730 DATA_PTR (gv) = NULL;
8738 fun (name, style, _, _, _, _, _) ->
8739 pr "static VALUE ruby_guestfs_%s (VALUE gv" name;
8740 List.iter (fun arg -> pr ", VALUE %sv" (name_of_argt arg)) (snd style);
8743 pr " guestfs_h *g;\n";
8744 pr " Data_Get_Struct (gv, guestfs_h, g);\n";
8746 pr " rb_raise (rb_eArgError, \"%%s: used handle after closing it\", \"%s\");\n"
8752 | Pathname n | Device n | Dev_or_Path n | String n | FileIn n | FileOut n ->
8753 pr " Check_Type (%sv, T_STRING);\n" n;
8754 pr " const char *%s = StringValueCStr (%sv);\n" n n;
8756 pr " rb_raise (rb_eTypeError, \"expected string for parameter %%s of %%s\",\n";
8757 pr " \"%s\", \"%s\");\n" n name
8759 pr " const char *%s = !NIL_P (%sv) ? StringValueCStr (%sv) : NULL;\n" n n n
8760 | StringList n | DeviceList n ->
8761 pr " char **%s;\n" n;
8762 pr " Check_Type (%sv, T_ARRAY);\n" n;
8764 pr " int i, len;\n";
8765 pr " len = RARRAY_LEN (%sv);\n" n;
8766 pr " %s = guestfs_safe_malloc (g, sizeof (char *) * (len+1));\n"
8768 pr " for (i = 0; i < len; ++i) {\n";
8769 pr " VALUE v = rb_ary_entry (%sv, i);\n" n;
8770 pr " %s[i] = StringValueCStr (v);\n" n;
8772 pr " %s[len] = NULL;\n" n;
8775 pr " int %s = RTEST (%sv);\n" n n
8777 pr " int %s = NUM2INT (%sv);\n" n n
8779 pr " long long %s = NUM2LL (%sv);\n" n n
8784 match fst style with
8785 | RErr | RInt _ | RBool _ -> pr " int r;\n"; "-1"
8786 | RInt64 _ -> pr " int64_t r;\n"; "-1"
8787 | RConstString _ | RConstOptString _ ->
8788 pr " const char *r;\n"; "NULL"
8789 | RString _ -> pr " char *r;\n"; "NULL"
8790 | RStringList _ | RHashtable _ -> pr " char **r;\n"; "NULL"
8791 | RStruct (_, typ) -> pr " struct guestfs_%s *r;\n" typ; "NULL"
8792 | RStructList (_, typ) ->
8793 pr " struct guestfs_%s_list *r;\n" typ; "NULL"
8796 pr " size_t size;\n";
8800 pr " r = guestfs_%s " name;
8801 generate_c_call_args ~handle:"g" style;
8806 | Pathname _ | Device _ | Dev_or_Path _ | String _
8807 | FileIn _ | FileOut _ | OptString _ | Bool _ | Int _ | Int64 _ -> ()
8808 | StringList n | DeviceList n ->
8809 pr " free (%s);\n" n
8812 pr " if (r == %s)\n" error_code;
8813 pr " rb_raise (e_Error, \"%%s\", guestfs_last_error (g));\n";
8816 (match fst style with
8818 pr " return Qnil;\n"
8819 | RInt _ | RBool _ ->
8820 pr " return INT2NUM (r);\n"
8822 pr " return ULL2NUM (r);\n"
8824 pr " return rb_str_new2 (r);\n";
8825 | RConstOptString _ ->
8827 pr " return rb_str_new2 (r);\n";
8829 pr " return Qnil;\n";
8831 pr " VALUE rv = rb_str_new2 (r);\n";
8835 pr " int i, len = 0;\n";
8836 pr " for (i = 0; r[i] != NULL; ++i) len++;\n";
8837 pr " VALUE rv = rb_ary_new2 (len);\n";
8838 pr " for (i = 0; r[i] != NULL; ++i) {\n";
8839 pr " rb_ary_push (rv, rb_str_new2 (r[i]));\n";
8840 pr " free (r[i]);\n";
8844 | RStruct (_, typ) ->
8845 let cols = cols_of_struct typ in
8846 generate_ruby_struct_code typ cols
8847 | RStructList (_, typ) ->
8848 let cols = cols_of_struct typ in
8849 generate_ruby_struct_list_code typ cols
8851 pr " VALUE rv = rb_hash_new ();\n";
8853 pr " for (i = 0; r[i] != NULL; i+=2) {\n";
8854 pr " rb_hash_aset (rv, rb_str_new2 (r[i]), rb_str_new2 (r[i+1]));\n";
8855 pr " free (r[i]);\n";
8856 pr " free (r[i+1]);\n";
8861 pr " VALUE rv = rb_str_new (r, size);\n";
8871 /* Initialize the module. */
8872 void Init__guestfs ()
8874 m_guestfs = rb_define_module (\"Guestfs\");
8875 c_guestfs = rb_define_class_under (m_guestfs, \"Guestfs\", rb_cObject);
8876 e_Error = rb_define_class_under (m_guestfs, \"Error\", rb_eStandardError);
8878 rb_define_module_function (m_guestfs, \"create\", ruby_guestfs_create, 0);
8879 rb_define_method (c_guestfs, \"close\", ruby_guestfs_close, 0);
8882 (* Define the rest of the methods. *)
8884 fun (name, style, _, _, _, _, _) ->
8885 pr " rb_define_method (c_guestfs, \"%s\",\n" name;
8886 pr " ruby_guestfs_%s, %d);\n" name (List.length (snd style))
8891 (* Ruby code to return a struct. *)
8892 and generate_ruby_struct_code typ cols =
8893 pr " VALUE rv = rb_hash_new ();\n";
8897 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), rb_str_new2 (r->%s));\n" name name
8899 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), rb_str_new (r->%s, r->%s_len));\n" name name name
8901 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), rb_str_new (r->%s, 32));\n" name name
8902 | name, (FBytes|FUInt64) ->
8903 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), ULL2NUM (r->%s));\n" name name
8905 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), LL2NUM (r->%s));\n" name name
8907 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), UINT2NUM (r->%s));\n" name name
8909 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), INT2NUM (r->%s));\n" name name
8910 | name, FOptPercent ->
8911 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), rb_dbl2big (r->%s));\n" name name
8912 | name, FChar -> (* XXX wrong? *)
8913 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), ULL2NUM (r->%s));\n" name name
8915 pr " guestfs_free_%s (r);\n" typ;
8918 (* Ruby code to return a struct list. *)
8919 and generate_ruby_struct_list_code typ cols =
8920 pr " VALUE rv = rb_ary_new2 (r->len);\n";
8922 pr " for (i = 0; i < r->len; ++i) {\n";
8923 pr " VALUE hv = rb_hash_new ();\n";
8927 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), rb_str_new2 (r->val[i].%s));\n" name name
8929 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), rb_str_new (r->val[i].%s, r->val[i].%s_len));\n" name name name
8931 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), rb_str_new (r->val[i].%s, 32));\n" name name
8932 | name, (FBytes|FUInt64) ->
8933 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), ULL2NUM (r->val[i].%s));\n" name name
8935 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), LL2NUM (r->val[i].%s));\n" name name
8937 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), UINT2NUM (r->val[i].%s));\n" name name
8939 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), INT2NUM (r->val[i].%s));\n" name name
8940 | name, FOptPercent ->
8941 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), rb_dbl2big (r->val[i].%s));\n" name name
8942 | name, FChar -> (* XXX wrong? *)
8943 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), ULL2NUM (r->val[i].%s));\n" name name
8945 pr " rb_ary_push (rv, hv);\n";
8947 pr " guestfs_free_%s_list (r);\n" typ;
8950 (* Generate Java bindings GuestFS.java file. *)
8951 and generate_java_java () =
8952 generate_header CStyle LGPLv2;
8955 package com.redhat.et.libguestfs;
8957 import java.util.HashMap;
8958 import com.redhat.et.libguestfs.LibGuestFSException;
8959 import com.redhat.et.libguestfs.PV;
8960 import com.redhat.et.libguestfs.VG;
8961 import com.redhat.et.libguestfs.LV;
8962 import com.redhat.et.libguestfs.Stat;
8963 import com.redhat.et.libguestfs.StatVFS;
8964 import com.redhat.et.libguestfs.IntBool;
8965 import com.redhat.et.libguestfs.Dirent;
8968 * The GuestFS object is a libguestfs handle.
8972 public class GuestFS {
8973 // Load the native code.
8975 System.loadLibrary (\"guestfs_jni\");
8979 * The native guestfs_h pointer.
8984 * Create a libguestfs handle.
8986 * @throws LibGuestFSException
8988 public GuestFS () throws LibGuestFSException
8992 private native long _create () throws LibGuestFSException;
8995 * Close a libguestfs handle.
8997 * You can also leave handles to be collected by the garbage
8998 * collector, but this method ensures that the resources used
8999 * by the handle are freed up immediately. If you call any
9000 * other methods after closing the handle, you will get an
9003 * @throws LibGuestFSException
9005 public void close () throws LibGuestFSException
9011 private native void _close (long g) throws LibGuestFSException;
9013 public void finalize () throws LibGuestFSException
9021 fun (name, style, _, flags, _, shortdesc, longdesc) ->
9022 if not (List.mem NotInDocs flags); then (
9023 let doc = replace_str longdesc "C<guestfs_" "C<g." in
9025 if List.mem ProtocolLimitWarning flags then
9026 doc ^ "\n\n" ^ protocol_limit_warning
9029 if List.mem DangerWillRobinson flags then
9030 doc ^ "\n\n" ^ danger_will_robinson
9033 match deprecation_notice flags with
9035 | Some txt -> doc ^ "\n\n" ^ txt in
9036 let doc = pod2text ~width:60 name doc in
9037 let doc = List.map ( (* RHBZ#501883 *)
9040 | nonempty -> nonempty
9042 let doc = String.concat "\n * " doc in
9045 pr " * %s\n" shortdesc;
9048 pr " * @throws LibGuestFSException\n";
9052 generate_java_prototype ~public:true ~semicolon:false name style;
9055 pr " if (g == 0)\n";
9056 pr " throw new LibGuestFSException (\"%s: handle is closed\");\n"
9059 if fst style <> RErr then pr "return ";
9061 generate_java_call_args ~handle:"g" (snd style);
9065 generate_java_prototype ~privat:true ~native:true name style;
9072 (* Generate Java call arguments, eg "(handle, foo, bar)" *)
9073 and generate_java_call_args ~handle args =
9075 List.iter (fun arg -> pr ", %s" (name_of_argt arg)) args;
9078 and generate_java_prototype ?(public=false) ?(privat=false) ?(native=false)
9079 ?(semicolon=true) name style =
9080 if privat then pr "private ";
9081 if public then pr "public ";
9082 if native then pr "native ";
9085 (match fst style with
9086 | RErr -> pr "void ";
9087 | RInt _ -> pr "int ";
9088 | RInt64 _ -> pr "long ";
9089 | RBool _ -> pr "boolean ";
9090 | RConstString _ | RConstOptString _ | RString _
9091 | RBufferOut _ -> pr "String ";
9092 | RStringList _ -> pr "String[] ";
9093 | RStruct (_, typ) ->
9094 let name = java_name_of_struct typ in
9096 | RStructList (_, typ) ->
9097 let name = java_name_of_struct typ in
9099 | RHashtable _ -> pr "HashMap<String,String> ";
9102 if native then pr "_%s " name else pr "%s " name;
9104 let needs_comma = ref false in
9113 if !needs_comma then pr ", ";
9114 needs_comma := true;
9118 | Device n | Dev_or_Path n
9124 | StringList n | DeviceList n ->
9135 pr " throws LibGuestFSException";
9136 if semicolon then pr ";"
9138 and generate_java_struct jtyp cols =
9139 generate_header CStyle LGPLv2;
9142 package com.redhat.et.libguestfs;
9145 * Libguestfs %s structure.
9157 | name, FBuffer -> pr " public String %s;\n" name
9158 | name, (FBytes|FUInt64|FInt64) -> pr " public long %s;\n" name
9159 | name, (FUInt32|FInt32) -> pr " public int %s;\n" name
9160 | name, FChar -> pr " public char %s;\n" name
9161 | name, FOptPercent ->
9162 pr " /* The next field is [0..100] or -1 meaning 'not present': */\n";
9163 pr " public float %s;\n" name
9168 and generate_java_c () =
9169 generate_header CStyle LGPLv2;
9176 #include \"com_redhat_et_libguestfs_GuestFS.h\"
9177 #include \"guestfs.h\"
9179 /* Note that this function returns. The exception is not thrown
9180 * until after the wrapper function returns.
9183 throw_exception (JNIEnv *env, const char *msg)
9186 cl = (*env)->FindClass (env,
9187 \"com/redhat/et/libguestfs/LibGuestFSException\");
9188 (*env)->ThrowNew (env, cl, msg);
9191 JNIEXPORT jlong JNICALL
9192 Java_com_redhat_et_libguestfs_GuestFS__1create
9193 (JNIEnv *env, jobject obj)
9197 g = guestfs_create ();
9199 throw_exception (env, \"GuestFS.create: failed to allocate handle\");
9202 guestfs_set_error_handler (g, NULL, NULL);
9203 return (jlong) (long) g;
9206 JNIEXPORT void JNICALL
9207 Java_com_redhat_et_libguestfs_GuestFS__1close
9208 (JNIEnv *env, jobject obj, jlong jg)
9210 guestfs_h *g = (guestfs_h *) (long) jg;
9217 fun (name, style, _, _, _, _, _) ->
9219 (match fst style with
9220 | RErr -> pr "void ";
9221 | RInt _ -> pr "jint ";
9222 | RInt64 _ -> pr "jlong ";
9223 | RBool _ -> pr "jboolean ";
9224 | RConstString _ | RConstOptString _ | RString _
9225 | RBufferOut _ -> pr "jstring ";
9226 | RStruct _ | RHashtable _ ->
9228 | RStringList _ | RStructList _ ->
9232 pr "Java_com_redhat_et_libguestfs_GuestFS_";
9233 pr "%s" (replace_str ("_" ^ name) "_" "_1");
9235 pr " (JNIEnv *env, jobject obj, jlong jg";
9239 | Device n | Dev_or_Path n
9244 pr ", jstring j%s" n
9245 | StringList n | DeviceList n ->
9246 pr ", jobjectArray j%s" n
9248 pr ", jboolean j%s" n
9256 pr " guestfs_h *g = (guestfs_h *) (long) jg;\n";
9257 let error_code, no_ret =
9258 match fst style with
9259 | RErr -> pr " int r;\n"; "-1", ""
9261 | RInt _ -> pr " int r;\n"; "-1", "0"
9262 | RInt64 _ -> pr " int64_t r;\n"; "-1", "0"
9263 | RConstString _ -> pr " const char *r;\n"; "NULL", "NULL"
9264 | RConstOptString _ -> pr " const char *r;\n"; "NULL", "NULL"
9266 pr " jstring jr;\n";
9267 pr " char *r;\n"; "NULL", "NULL"
9269 pr " jobjectArray jr;\n";
9272 pr " jstring jstr;\n";
9273 pr " char **r;\n"; "NULL", "NULL"
9274 | RStruct (_, typ) ->
9275 pr " jobject jr;\n";
9277 pr " jfieldID fl;\n";
9278 pr " struct guestfs_%s *r;\n" typ; "NULL", "NULL"
9279 | RStructList (_, typ) ->
9280 pr " jobjectArray jr;\n";
9282 pr " jfieldID fl;\n";
9283 pr " jobject jfl;\n";
9284 pr " struct guestfs_%s_list *r;\n" typ; "NULL", "NULL"
9285 | RHashtable _ -> pr " char **r;\n"; "NULL", "NULL"
9287 pr " jstring jr;\n";
9289 pr " size_t size;\n";
9294 | Device n | Dev_or_Path n
9299 pr " const char *%s;\n" n
9300 | StringList n | DeviceList n ->
9301 pr " int %s_len;\n" n;
9302 pr " const char **%s;\n" n
9307 pr " int64_t %s;\n" n
9311 (match fst style with
9312 | RStringList _ | RStructList _ -> true
9313 | RErr | RBool _ | RInt _ | RInt64 _ | RConstString _
9315 | RString _ | RBufferOut _ | RStruct _ | RHashtable _ -> false) ||
9316 List.exists (function
9317 | StringList _ -> true
9318 | DeviceList _ -> true
9319 | _ -> false) (snd style) in
9325 (* Get the parameters. *)
9329 | Device n | Dev_or_Path n
9333 pr " %s = (*env)->GetStringUTFChars (env, j%s, NULL);\n" n n
9335 (* This is completely undocumented, but Java null becomes
9338 pr " %s = j%s ? (*env)->GetStringUTFChars (env, j%s, NULL) : NULL;\n" n n n
9339 | StringList n | DeviceList n ->
9340 pr " %s_len = (*env)->GetArrayLength (env, j%s);\n" n n;
9341 pr " %s = guestfs_safe_malloc (g, sizeof (char *) * (%s_len+1));\n" n n;
9342 pr " for (i = 0; i < %s_len; ++i) {\n" n;
9343 pr " jobject o = (*env)->GetObjectArrayElement (env, j%s, i);\n"
9345 pr " %s[i] = (*env)->GetStringUTFChars (env, o, NULL);\n" n;
9347 pr " %s[%s_len] = NULL;\n" n n;
9351 pr " %s = j%s;\n" n n
9354 (* Make the call. *)
9355 pr " r = guestfs_%s " name;
9356 generate_c_call_args ~handle:"g" style;
9359 (* Release the parameters. *)
9363 | Device n | Dev_or_Path n
9367 pr " (*env)->ReleaseStringUTFChars (env, j%s, %s);\n" n n
9370 pr " (*env)->ReleaseStringUTFChars (env, j%s, %s);\n" n n
9371 | StringList n | DeviceList n ->
9372 pr " for (i = 0; i < %s_len; ++i) {\n" n;
9373 pr " jobject o = (*env)->GetObjectArrayElement (env, j%s, i);\n"
9375 pr " (*env)->ReleaseStringUTFChars (env, o, %s[i]);\n" n;
9377 pr " free (%s);\n" n
9383 (* Check for errors. *)
9384 pr " if (r == %s) {\n" error_code;
9385 pr " throw_exception (env, guestfs_last_error (g));\n";
9386 pr " return %s;\n" no_ret;
9390 (match fst style with
9392 | RInt _ -> pr " return (jint) r;\n"
9393 | RBool _ -> pr " return (jboolean) r;\n"
9394 | RInt64 _ -> pr " return (jlong) r;\n"
9395 | RConstString _ -> pr " return (*env)->NewStringUTF (env, r);\n"
9396 | RConstOptString _ ->
9397 pr " return (*env)->NewStringUTF (env, r); /* XXX r NULL? */\n"
9399 pr " jr = (*env)->NewStringUTF (env, r);\n";
9403 pr " for (r_len = 0; r[r_len] != NULL; ++r_len) ;\n";
9404 pr " cl = (*env)->FindClass (env, \"java/lang/String\");\n";
9405 pr " jstr = (*env)->NewStringUTF (env, \"\");\n";
9406 pr " jr = (*env)->NewObjectArray (env, r_len, cl, jstr);\n";
9407 pr " for (i = 0; i < r_len; ++i) {\n";
9408 pr " jstr = (*env)->NewStringUTF (env, r[i]);\n";
9409 pr " (*env)->SetObjectArrayElement (env, jr, i, jstr);\n";
9410 pr " free (r[i]);\n";
9414 | RStruct (_, typ) ->
9415 let jtyp = java_name_of_struct typ in
9416 let cols = cols_of_struct typ in
9417 generate_java_struct_return typ jtyp cols
9418 | RStructList (_, typ) ->
9419 let jtyp = java_name_of_struct typ in
9420 let cols = cols_of_struct typ in
9421 generate_java_struct_list_return typ jtyp cols
9424 pr " throw_exception (env, \"%s: internal error: please let us know how to make a Java HashMap from JNI bindings!\");\n" name;
9425 pr " return NULL;\n"
9427 pr " jr = (*env)->NewStringUTF (env, r); /* XXX size */\n";
9436 and generate_java_struct_return typ jtyp cols =
9437 pr " cl = (*env)->FindClass (env, \"com/redhat/et/libguestfs/%s\");\n" jtyp;
9438 pr " jr = (*env)->AllocObject (env, cl);\n";
9442 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
9443 pr " (*env)->SetObjectField (env, jr, fl, (*env)->NewStringUTF (env, r->%s));\n" name;
9446 pr " char s[33];\n";
9447 pr " memcpy (s, r->%s, 32);\n" name;
9449 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
9450 pr " (*env)->SetObjectField (env, jr, fl, (*env)->NewStringUTF (env, s));\n";
9454 pr " int len = r->%s_len;\n" name;
9455 pr " char s[len+1];\n";
9456 pr " memcpy (s, r->%s, len);\n" name;
9457 pr " s[len] = 0;\n";
9458 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
9459 pr " (*env)->SetObjectField (env, jr, fl, (*env)->NewStringUTF (env, s));\n";
9461 | name, (FBytes|FUInt64|FInt64) ->
9462 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"J\");\n" name;
9463 pr " (*env)->SetLongField (env, jr, fl, r->%s);\n" name;
9464 | name, (FUInt32|FInt32) ->
9465 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"I\");\n" name;
9466 pr " (*env)->SetLongField (env, jr, fl, r->%s);\n" name;
9467 | name, FOptPercent ->
9468 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"F\");\n" name;
9469 pr " (*env)->SetFloatField (env, jr, fl, r->%s);\n" name;
9471 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"C\");\n" name;
9472 pr " (*env)->SetLongField (env, jr, fl, r->%s);\n" name;
9477 and generate_java_struct_list_return typ jtyp cols =
9478 pr " cl = (*env)->FindClass (env, \"com/redhat/et/libguestfs/%s\");\n" jtyp;
9479 pr " jr = (*env)->NewObjectArray (env, r->len, cl, NULL);\n";
9480 pr " for (i = 0; i < r->len; ++i) {\n";
9481 pr " jfl = (*env)->AllocObject (env, cl);\n";
9485 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
9486 pr " (*env)->SetObjectField (env, jfl, fl, (*env)->NewStringUTF (env, r->val[i].%s));\n" name;
9489 pr " char s[33];\n";
9490 pr " memcpy (s, r->val[i].%s, 32);\n" name;
9492 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
9493 pr " (*env)->SetObjectField (env, jfl, fl, (*env)->NewStringUTF (env, s));\n";
9497 pr " int len = r->val[i].%s_len;\n" name;
9498 pr " char s[len+1];\n";
9499 pr " memcpy (s, r->val[i].%s, len);\n" name;
9500 pr " s[len] = 0;\n";
9501 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
9502 pr " (*env)->SetObjectField (env, jfl, fl, (*env)->NewStringUTF (env, s));\n";
9504 | name, (FBytes|FUInt64|FInt64) ->
9505 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"J\");\n" name;
9506 pr " (*env)->SetLongField (env, jfl, fl, r->val[i].%s);\n" name;
9507 | name, (FUInt32|FInt32) ->
9508 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"I\");\n" name;
9509 pr " (*env)->SetLongField (env, jfl, fl, r->val[i].%s);\n" name;
9510 | name, FOptPercent ->
9511 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"F\");\n" name;
9512 pr " (*env)->SetFloatField (env, jfl, fl, r->val[i].%s);\n" name;
9514 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"C\");\n" name;
9515 pr " (*env)->SetLongField (env, jfl, fl, r->val[i].%s);\n" name;
9517 pr " (*env)->SetObjectArrayElement (env, jfl, i, jfl);\n";
9519 pr " guestfs_free_%s_list (r);\n" typ;
9522 and generate_java_makefile_inc () =
9523 generate_header HashStyle GPLv2;
9525 pr "java_built_sources = \\\n";
9528 pr "\tcom/redhat/et/libguestfs/%s.java \\\n" jtyp;
9530 pr "\tcom/redhat/et/libguestfs/GuestFS.java\n"
9532 and generate_haskell_hs () =
9533 generate_header HaskellStyle LGPLv2;
9535 (* XXX We only know how to generate partial FFI for Haskell
9536 * at the moment. Please help out!
9538 let can_generate style =
9542 | RInt64 _, _ -> true
9545 | RConstOptString _, _
9551 | RBufferOut _, _ -> false in
9554 {-# INCLUDE <guestfs.h> #-}
9555 {-# LANGUAGE ForeignFunctionInterface #-}
9560 (* List out the names of the actions we want to export. *)
9562 fun (name, style, _, _, _, _, _) ->
9563 if can_generate style then pr ",\n %s" name
9569 -- Unfortunately some symbols duplicate ones already present
9570 -- in Prelude. We don't know which, so we hard-code a list
9572 import Prelude hiding (truncate)
9576 import Foreign.C.Types
9578 import Control.Exception
9579 import Data.Typeable
9581 data GuestfsS = GuestfsS -- represents the opaque C struct
9582 type GuestfsP = Ptr GuestfsS -- guestfs_h *
9583 type GuestfsH = ForeignPtr GuestfsS -- guestfs_h * with attached finalizer
9585 -- XXX define properly later XXX
9589 data IntBool = IntBool
9591 data StatVFS = StatVFS
9592 data Hashtable = Hashtable
9594 foreign import ccall unsafe \"guestfs_create\" c_create
9596 foreign import ccall unsafe \"&guestfs_close\" c_close
9597 :: FunPtr (GuestfsP -> IO ())
9598 foreign import ccall unsafe \"guestfs_set_error_handler\" c_set_error_handler
9599 :: GuestfsP -> Ptr CInt -> Ptr CInt -> IO ()
9601 create :: IO GuestfsH
9604 c_set_error_handler p nullPtr nullPtr
9605 h <- newForeignPtr c_close p
9608 foreign import ccall unsafe \"guestfs_last_error\" c_last_error
9609 :: GuestfsP -> IO CString
9611 -- last_error :: GuestfsH -> IO (Maybe String)
9612 -- last_error h = do
9613 -- str <- withForeignPtr h (\\p -> c_last_error p)
9614 -- maybePeek peekCString str
9616 last_error :: GuestfsH -> IO (String)
9618 str <- withForeignPtr h (\\p -> c_last_error p)
9620 then return \"no error\"
9621 else peekCString str
9625 (* Generate wrappers for each foreign function. *)
9627 fun (name, style, _, _, _, _, _) ->
9628 if can_generate style then (
9629 pr "foreign import ccall unsafe \"guestfs_%s\" c_%s\n" name name;
9631 generate_haskell_prototype ~handle:"GuestfsP" style;
9635 generate_haskell_prototype ~handle:"GuestfsH" ~hs:true style;
9637 pr "%s %s = do\n" name
9638 (String.concat " " ("h" :: List.map name_of_argt (snd style)));
9640 (* Convert pointer arguments using with* functions. *)
9645 | Pathname n | Device n | Dev_or_Path n | String n -> pr "withCString %s $ \\%s -> " n n
9646 | OptString n -> pr "maybeWith withCString %s $ \\%s -> " n n
9647 | StringList n | DeviceList n -> pr "withMany withCString %s $ \\%s -> withArray0 nullPtr %s $ \\%s -> " n n n n
9648 | Bool _ | Int _ | Int64 _ -> ()
9650 (* Convert integer arguments. *)
9654 | Bool n -> sprintf "(fromBool %s)" n
9655 | Int n -> sprintf "(fromIntegral %s)" n
9656 | Int64 n -> sprintf "(fromIntegral %s)" n
9657 | FileIn n | FileOut n
9658 | Pathname n | Device n | Dev_or_Path n | String n | OptString n | StringList n | DeviceList n -> n
9660 pr "withForeignPtr h (\\p -> c_%s %s)\n" name
9661 (String.concat " " ("p" :: args));
9662 (match fst style with
9663 | RErr | RInt _ | RInt64 _ | RBool _ ->
9664 pr " if (r == -1)\n";
9666 pr " err <- last_error h\n";
9668 | RConstString _ | RConstOptString _ | RString _
9669 | RStringList _ | RStruct _
9670 | RStructList _ | RHashtable _ | RBufferOut _ ->
9671 pr " if (r == nullPtr)\n";
9673 pr " err <- last_error h\n";
9676 (match fst style with
9678 pr " else return ()\n"
9680 pr " else return (fromIntegral r)\n"
9682 pr " else return (fromIntegral r)\n"
9684 pr " else return (toBool r)\n"
9693 pr " else return ()\n" (* XXXXXXXXXXXXXXXXXXXX *)
9699 and generate_haskell_prototype ~handle ?(hs = false) style =
9701 let string = if hs then "String" else "CString" in
9702 let int = if hs then "Int" else "CInt" in
9703 let bool = if hs then "Bool" else "CInt" in
9704 let int64 = if hs then "Integer" else "Int64" in
9708 | Pathname _ | Device _ | Dev_or_Path _ | String _ -> pr "%s" string
9709 | OptString _ -> if hs then pr "Maybe String" else pr "CString"
9710 | StringList _ | DeviceList _ -> if hs then pr "[String]" else pr "Ptr CString"
9711 | Bool _ -> pr "%s" bool
9712 | Int _ -> pr "%s" int
9713 | Int64 _ -> pr "%s" int
9714 | FileIn _ -> pr "%s" string
9715 | FileOut _ -> pr "%s" string
9720 (match fst style with
9721 | RErr -> if not hs then pr "CInt"
9722 | RInt _ -> pr "%s" int
9723 | RInt64 _ -> pr "%s" int64
9724 | RBool _ -> pr "%s" bool
9725 | RConstString _ -> pr "%s" string
9726 | RConstOptString _ -> pr "Maybe %s" string
9727 | RString _ -> pr "%s" string
9728 | RStringList _ -> pr "[%s]" string
9729 | RStruct (_, typ) ->
9730 let name = java_name_of_struct typ in
9732 | RStructList (_, typ) ->
9733 let name = java_name_of_struct typ in
9735 | RHashtable _ -> pr "Hashtable"
9736 | RBufferOut _ -> pr "%s" string
9740 and generate_bindtests () =
9741 generate_header CStyle LGPLv2;
9746 #include <inttypes.h>
9749 #include \"guestfs.h\"
9750 #include \"guestfs-internal.h\"
9751 #include \"guestfs-internal-actions.h\"
9752 #include \"guestfs_protocol.h\"
9754 #define error guestfs_error
9755 #define safe_calloc guestfs_safe_calloc
9756 #define safe_malloc guestfs_safe_malloc
9759 print_strings (char *const *argv)
9764 for (argc = 0; argv[argc] != NULL; ++argc) {
9765 if (argc > 0) printf (\", \");
9766 printf (\"\\\"%%s\\\"\", argv[argc]);
9771 /* The test0 function prints its parameters to stdout. */
9775 match test_functions with
9776 | [] -> assert false
9777 | test0 :: tests -> test0, tests in
9780 let (name, style, _, _, _, _, _) = test0 in
9781 generate_prototype ~extern:false ~semicolon:false ~newline:true
9782 ~handle:"g" ~prefix:"guestfs__" name style;
9787 | Device n | Dev_or_Path n
9790 | FileOut n -> pr " printf (\"%%s\\n\", %s);\n" n
9791 | OptString n -> pr " printf (\"%%s\\n\", %s ? %s : \"null\");\n" n n
9792 | StringList n | DeviceList n -> pr " print_strings (%s);\n" n
9793 | Bool n -> pr " printf (\"%%s\\n\", %s ? \"true\" : \"false\");\n" n
9794 | Int n -> pr " printf (\"%%d\\n\", %s);\n" n
9795 | Int64 n -> pr " printf (\"%%\" PRIi64 \"\\n\", %s);\n" n
9797 pr " /* Java changes stdout line buffering so we need this: */\n";
9798 pr " fflush (stdout);\n";
9804 fun (name, style, _, _, _, _, _) ->
9805 if String.sub name (String.length name - 3) 3 <> "err" then (
9806 pr "/* Test normal return. */\n";
9807 generate_prototype ~extern:false ~semicolon:false ~newline:true
9808 ~handle:"g" ~prefix:"guestfs__" name style;
9810 (match fst style with
9815 pr " sscanf (val, \"%%d\", &r);\n";
9819 pr " sscanf (val, \"%%\" SCNi64, &r);\n";
9822 pr " return STREQ (val, \"true\");\n"
9824 | RConstOptString _ ->
9825 (* Can't return the input string here. Return a static
9826 * string so we ensure we get a segfault if the caller
9829 pr " return \"static string\";\n"
9831 pr " return strdup (val);\n"
9833 pr " char **strs;\n";
9835 pr " sscanf (val, \"%%d\", &n);\n";
9836 pr " strs = safe_malloc (g, (n+1) * sizeof (char *));\n";
9837 pr " for (i = 0; i < n; ++i) {\n";
9838 pr " strs[i] = safe_malloc (g, 16);\n";
9839 pr " snprintf (strs[i], 16, \"%%d\", i);\n";
9841 pr " strs[n] = NULL;\n";
9842 pr " return strs;\n"
9843 | RStruct (_, typ) ->
9844 pr " struct guestfs_%s *r;\n" typ;
9845 pr " r = safe_calloc (g, sizeof *r, 1);\n";
9847 | RStructList (_, typ) ->
9848 pr " struct guestfs_%s_list *r;\n" typ;
9849 pr " r = safe_calloc (g, sizeof *r, 1);\n";
9850 pr " sscanf (val, \"%%d\", &r->len);\n";
9851 pr " r->val = safe_calloc (g, r->len, sizeof *r->val);\n";
9854 pr " char **strs;\n";
9856 pr " sscanf (val, \"%%d\", &n);\n";
9857 pr " strs = safe_malloc (g, (n*2+1) * sizeof (*strs));\n";
9858 pr " for (i = 0; i < n; ++i) {\n";
9859 pr " strs[i*2] = safe_malloc (g, 16);\n";
9860 pr " strs[i*2+1] = safe_malloc (g, 16);\n";
9861 pr " snprintf (strs[i*2], 16, \"%%d\", i);\n";
9862 pr " snprintf (strs[i*2+1], 16, \"%%d\", i);\n";
9864 pr " strs[n*2] = NULL;\n";
9865 pr " return strs;\n"
9867 pr " return strdup (val);\n"
9872 pr "/* Test error return. */\n";
9873 generate_prototype ~extern:false ~semicolon:false ~newline:true
9874 ~handle:"g" ~prefix:"guestfs__" name style;
9876 pr " error (g, \"error\");\n";
9877 (match fst style with
9878 | RErr | RInt _ | RInt64 _ | RBool _ ->
9880 | RConstString _ | RConstOptString _
9881 | RString _ | RStringList _ | RStruct _
9885 pr " return NULL;\n"
9892 and generate_ocaml_bindtests () =
9893 generate_header OCamlStyle GPLv2;
9897 let g = Guestfs.create () in
9904 | CallString s -> "\"" ^ s ^ "\""
9905 | CallOptString None -> "None"
9906 | CallOptString (Some s) -> sprintf "(Some \"%s\")" s
9907 | CallStringList xs ->
9908 "[|" ^ String.concat ";" (List.map (sprintf "\"%s\"") xs) ^ "|]"
9909 | CallInt i when i >= 0 -> string_of_int i
9910 | CallInt i (* when i < 0 *) -> "(" ^ string_of_int i ^ ")"
9911 | CallInt64 i when i >= 0L -> Int64.to_string i ^ "L"
9912 | CallInt64 i (* when i < 0L *) -> "(" ^ Int64.to_string i ^ "L)"
9913 | CallBool b -> string_of_bool b
9918 generate_lang_bindtests (
9919 fun f args -> pr " Guestfs.%s g %s;\n" f (mkargs args)
9922 pr "print_endline \"EOF\"\n"
9924 and generate_perl_bindtests () =
9925 pr "#!/usr/bin/perl -w\n";
9926 generate_header HashStyle GPLv2;
9933 my $g = Sys::Guestfs->new ();
9937 String.concat ", " (
9940 | CallString s -> "\"" ^ s ^ "\""
9941 | CallOptString None -> "undef"
9942 | CallOptString (Some s) -> sprintf "\"%s\"" s
9943 | CallStringList xs ->
9944 "[" ^ String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "]"
9945 | CallInt i -> string_of_int i
9946 | CallInt64 i -> Int64.to_string i
9947 | CallBool b -> if b then "1" else "0"
9952 generate_lang_bindtests (
9953 fun f args -> pr "$g->%s (%s);\n" f (mkargs args)
9956 pr "print \"EOF\\n\"\n"
9958 and generate_python_bindtests () =
9959 generate_header HashStyle GPLv2;
9964 g = guestfs.GuestFS ()
9968 String.concat ", " (
9971 | CallString s -> "\"" ^ s ^ "\""
9972 | CallOptString None -> "None"
9973 | CallOptString (Some s) -> sprintf "\"%s\"" s
9974 | CallStringList xs ->
9975 "[" ^ String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "]"
9976 | CallInt i -> string_of_int i
9977 | CallInt64 i -> Int64.to_string i
9978 | CallBool b -> if b then "1" else "0"
9983 generate_lang_bindtests (
9984 fun f args -> pr "g.%s (%s)\n" f (mkargs args)
9987 pr "print \"EOF\"\n"
9989 and generate_ruby_bindtests () =
9990 generate_header HashStyle GPLv2;
9995 g = Guestfs::create()
9999 String.concat ", " (
10002 | CallString s -> "\"" ^ s ^ "\""
10003 | CallOptString None -> "nil"
10004 | CallOptString (Some s) -> sprintf "\"%s\"" s
10005 | CallStringList xs ->
10006 "[" ^ String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "]"
10007 | CallInt i -> string_of_int i
10008 | CallInt64 i -> Int64.to_string i
10009 | CallBool b -> string_of_bool b
10014 generate_lang_bindtests (
10015 fun f args -> pr "g.%s(%s)\n" f (mkargs args)
10018 pr "print \"EOF\\n\"\n"
10020 and generate_java_bindtests () =
10021 generate_header CStyle GPLv2;
10024 import com.redhat.et.libguestfs.*;
10026 public class Bindtests {
10027 public static void main (String[] argv)
10030 GuestFS g = new GuestFS ();
10034 String.concat ", " (
10037 | CallString s -> "\"" ^ s ^ "\""
10038 | CallOptString None -> "null"
10039 | CallOptString (Some s) -> sprintf "\"%s\"" s
10040 | CallStringList xs ->
10042 String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "}"
10043 | CallInt i -> string_of_int i
10044 | CallInt64 i -> Int64.to_string i
10045 | CallBool b -> string_of_bool b
10050 generate_lang_bindtests (
10051 fun f args -> pr " g.%s (%s);\n" f (mkargs args)
10055 System.out.println (\"EOF\");
10057 catch (Exception exn) {
10058 System.err.println (exn);
10065 and generate_haskell_bindtests () =
10066 generate_header HaskellStyle GPLv2;
10069 module Bindtests where
10070 import qualified Guestfs
10073 g <- Guestfs.create
10077 String.concat " " (
10080 | CallString s -> "\"" ^ s ^ "\""
10081 | CallOptString None -> "Nothing"
10082 | CallOptString (Some s) -> sprintf "(Just \"%s\")" s
10083 | CallStringList xs ->
10084 "[" ^ String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "]"
10085 | CallInt i when i < 0 -> "(" ^ string_of_int i ^ ")"
10086 | CallInt i -> string_of_int i
10087 | CallInt64 i when i < 0L -> "(" ^ Int64.to_string i ^ ")"
10088 | CallInt64 i -> Int64.to_string i
10089 | CallBool true -> "True"
10090 | CallBool false -> "False"
10095 generate_lang_bindtests (
10096 fun f args -> pr " Guestfs.%s g %s\n" f (mkargs args)
10099 pr " putStrLn \"EOF\"\n"
10101 (* Language-independent bindings tests - we do it this way to
10102 * ensure there is parity in testing bindings across all languages.
10104 and generate_lang_bindtests call =
10105 call "test0" [CallString "abc"; CallOptString (Some "def");
10106 CallStringList []; CallBool false;
10107 CallInt 0; CallInt64 0L; CallString "123"; CallString "456"];
10108 call "test0" [CallString "abc"; CallOptString None;
10109 CallStringList []; CallBool false;
10110 CallInt 0; CallInt64 0L; CallString "123"; CallString "456"];
10111 call "test0" [CallString ""; CallOptString (Some "def");
10112 CallStringList []; CallBool false;
10113 CallInt 0; CallInt64 0L; CallString "123"; CallString "456"];
10114 call "test0" [CallString ""; CallOptString (Some "");
10115 CallStringList []; CallBool false;
10116 CallInt 0; CallInt64 0L; CallString "123"; CallString "456"];
10117 call "test0" [CallString "abc"; CallOptString (Some "def");
10118 CallStringList ["1"]; CallBool false;
10119 CallInt 0; CallInt64 0L; CallString "123"; CallString "456"];
10120 call "test0" [CallString "abc"; CallOptString (Some "def");
10121 CallStringList ["1"; "2"]; CallBool false;
10122 CallInt 0; CallInt64 0L; CallString "123"; CallString "456"];
10123 call "test0" [CallString "abc"; CallOptString (Some "def");
10124 CallStringList ["1"]; CallBool true;
10125 CallInt 0; CallInt64 0L; CallString "123"; CallString "456"];
10126 call "test0" [CallString "abc"; CallOptString (Some "def");
10127 CallStringList ["1"]; CallBool false;
10128 CallInt (-1); CallInt64 (-1L); CallString "123"; CallString "456"];
10129 call "test0" [CallString "abc"; CallOptString (Some "def");
10130 CallStringList ["1"]; CallBool false;
10131 CallInt (-2); CallInt64 (-2L); CallString "123"; CallString "456"];
10132 call "test0" [CallString "abc"; CallOptString (Some "def");
10133 CallStringList ["1"]; CallBool false;
10134 CallInt 1; CallInt64 1L; CallString "123"; CallString "456"];
10135 call "test0" [CallString "abc"; CallOptString (Some "def");
10136 CallStringList ["1"]; CallBool false;
10137 CallInt 2; CallInt64 2L; CallString "123"; CallString "456"];
10138 call "test0" [CallString "abc"; CallOptString (Some "def");
10139 CallStringList ["1"]; CallBool false;
10140 CallInt 4095; CallInt64 4095L; CallString "123"; CallString "456"];
10141 call "test0" [CallString "abc"; CallOptString (Some "def");
10142 CallStringList ["1"]; CallBool false;
10143 CallInt 0; CallInt64 0L; CallString ""; CallString ""]
10145 (* XXX Add here tests of the return and error functions. *)
10147 (* This is used to generate the src/MAX_PROC_NR file which
10148 * contains the maximum procedure number, a surrogate for the
10149 * ABI version number. See src/Makefile.am for the details.
10151 and generate_max_proc_nr () =
10152 let proc_nrs = List.map (
10153 fun (_, _, proc_nr, _, _, _, _) -> proc_nr
10154 ) daemon_functions in
10156 let max_proc_nr = List.fold_left max 0 proc_nrs in
10158 pr "%d\n" max_proc_nr
10160 let output_to filename =
10161 let filename_new = filename ^ ".new" in
10162 chan := open_out filename_new;
10165 chan := Pervasives.stdout;
10167 (* Is the new file different from the current file? *)
10168 if Sys.file_exists filename && files_equal filename filename_new then
10169 unlink filename_new (* same, so skip it *)
10171 (* different, overwrite old one *)
10172 (try chmod filename 0o644 with Unix_error _ -> ());
10173 rename filename_new filename;
10174 chmod filename 0o444;
10175 printf "written %s\n%!" filename;
10180 let perror msg = function
10181 | Unix_error (err, _, _) ->
10182 eprintf "%s: %s\n" msg (error_message err)
10184 eprintf "%s: %s\n" msg (Printexc.to_string exn)
10186 (* Main program. *)
10189 try openfile "HACKING" [O_RDWR] 0
10191 | Unix_error (ENOENT, _, _) ->
10193 You are probably running this from the wrong directory.
10194 Run it from the top source directory using the command
10199 perror "open: HACKING" exn;
10202 (* Acquire a lock so parallel builds won't try to run the generator
10203 * twice at the same time. Subsequent builds will wait for the first
10204 * one to finish. Note the lock is released implicitly when the
10207 (try lockf lock_fd F_LOCK 1
10209 perror "lock: HACKING" exn;
10212 check_functions ();
10214 let close = output_to "src/guestfs_protocol.x" in
10218 let close = output_to "src/guestfs-structs.h" in
10219 generate_structs_h ();
10222 let close = output_to "src/guestfs-actions.h" in
10223 generate_actions_h ();
10226 let close = output_to "src/guestfs-internal-actions.h" in
10227 generate_internal_actions_h ();
10230 let close = output_to "src/guestfs-actions.c" in
10231 generate_client_actions ();
10234 let close = output_to "daemon/actions.h" in
10235 generate_daemon_actions_h ();
10238 let close = output_to "daemon/stubs.c" in
10239 generate_daemon_actions ();
10242 let close = output_to "daemon/names.c" in
10243 generate_daemon_names ();
10246 let close = output_to "capitests/tests.c" in
10250 let close = output_to "src/guestfs-bindtests.c" in
10251 generate_bindtests ();
10254 let close = output_to "fish/cmds.c" in
10255 generate_fish_cmds ();
10258 let close = output_to "fish/completion.c" in
10259 generate_fish_completion ();
10262 let close = output_to "guestfs-structs.pod" in
10263 generate_structs_pod ();
10266 let close = output_to "guestfs-actions.pod" in
10267 generate_actions_pod ();
10270 let close = output_to "guestfish-actions.pod" in
10271 generate_fish_actions_pod ();
10274 let close = output_to "ocaml/guestfs.mli" in
10275 generate_ocaml_mli ();
10278 let close = output_to "ocaml/guestfs.ml" in
10279 generate_ocaml_ml ();
10282 let close = output_to "ocaml/guestfs_c_actions.c" in
10283 generate_ocaml_c ();
10286 let close = output_to "ocaml/bindtests.ml" in
10287 generate_ocaml_bindtests ();
10290 let close = output_to "perl/Guestfs.xs" in
10291 generate_perl_xs ();
10294 let close = output_to "perl/lib/Sys/Guestfs.pm" in
10295 generate_perl_pm ();
10298 let close = output_to "perl/bindtests.pl" in
10299 generate_perl_bindtests ();
10302 let close = output_to "python/guestfs-py.c" in
10303 generate_python_c ();
10306 let close = output_to "python/guestfs.py" in
10307 generate_python_py ();
10310 let close = output_to "python/bindtests.py" in
10311 generate_python_bindtests ();
10314 let close = output_to "ruby/ext/guestfs/_guestfs.c" in
10315 generate_ruby_c ();
10318 let close = output_to "ruby/bindtests.rb" in
10319 generate_ruby_bindtests ();
10322 let close = output_to "java/com/redhat/et/libguestfs/GuestFS.java" in
10323 generate_java_java ();
10328 let cols = cols_of_struct typ in
10329 let filename = sprintf "java/com/redhat/et/libguestfs/%s.java" jtyp in
10330 let close = output_to filename in
10331 generate_java_struct jtyp cols;
10335 let close = output_to "java/Makefile.inc" in
10336 generate_java_makefile_inc ();
10339 let close = output_to "java/com_redhat_et_libguestfs_GuestFS.c" in
10340 generate_java_c ();
10343 let close = output_to "java/Bindtests.java" in
10344 generate_java_bindtests ();
10347 let close = output_to "haskell/Guestfs.hs" in
10348 generate_haskell_hs ();
10351 let close = output_to "haskell/Bindtests.hs" in
10352 generate_haskell_bindtests ();
10355 let close = output_to "src/MAX_PROC_NR" in
10356 generate_max_proc_nr ();
10359 (* Always generate this file last, and unconditionally. It's used
10360 * by the Makefile to know when we must re-run the generator.
10362 let chan = open_out "src/stamp-generator" in
10363 fprintf chan "1\n";