diff options
author | Richard W.M. Jones <rjones@redhat.com> | 2010-10-20 11:34:57 +0100 |
---|---|---|
committer | Richard W.M. Jones <rjones@redhat.com> | 2010-10-22 17:45:00 +0100 |
commit | 14490c3e1aac61c6ac90f28828896683f64f0dc9 (patch) | |
tree | 9855d3ef5cd421acf4841525c96696933097db79 /ruby | |
parent | 87be9b26e7004a9a58970b4e97d0d152dc1c0704 (diff) | |
download | libguestfs-14490c3e1aac61c6ac90f28828896683f64f0dc9.tar.gz libguestfs-14490c3e1aac61c6ac90f28828896683f64f0dc9.tar.xz libguestfs-14490c3e1aac61c6ac90f28828896683f64f0dc9.zip |
generator: Optional arguments, add-drive-opts (RHBZ#642934,CVE-2010-3851).
This large commit changes the generator so that optional arguments
can be supported for functions.
The model for arguments (known as the "style") is changed from
(ret, args) to (ret, args, optargs) where optargs is a more limited
list of arguments.
One function has been added which takes optional arguments, it is
"add-drive-opts", modelled as:
(RErr, [String "filename"], #required
[Bool "readonly"; String "format"; String "iface"]) #optional
Note that this function is processed in the library (does not go over
the RPC protocol to the daemon). This has allowed us to simplify
the current implementation by omitting changes related to RPC or the
daemon, although we plan to add these at some point in the future.
From C this function can be called in 3 different ways as in these
examples:
guestfs_add_drive_opts (g, filename,
GUESTFS_ADD_DRIVE_OPTS_READONLY, 1,
GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw",
-1);
(the argument(s) between 'filename' and '-1' are the optional ones).
guestfs_add_drive_opts_va (g, filename, args);
where 'args' is a va_list. This works like the first version.
struct guestfs_add_drive_opts_argv optargs = {
.bitmask = GUESTFS_ADD_DRIVE_OPTS_READONLY_BITMASK,
.readonly = 1,
}
guestfs_add_drive_opts_argv (g, filename, &optargs);
This last form lets you construct lists of optional arguments, and
is used by guestfish and the language bindings.
In guestfish optional arguments are used like this:
add-drive-opts filename readonly:true
In OCaml these are mapped naturally to OCaml optional arguments, eg:
g#add_drive_opts ~readonly:true filename;
In Perl these are mapped to extra arguments, eg:
$g->add_drive_opts ($filename, readonly => 1);
In Python these are mapped to optional arguments, eg:
g.add_drive_opts ("file", readonly = 1, format = "qcow2")
In Ruby these are mapped to a final hash argument, eg:
g.add_drive_opts("file", {})
g.add_drive_opts("file", :readonly => 1)
g.add_drive_opts("file", :readonly => 1, :iface => "virtio")
In PHP these are mapped to extra parameters. This is not quite
accurate since you cannot omit arbitrary optional parameters, but
there's not much than can be done within the limitations of PHP
as a language.
Unimplemented in: Haskell, C#, Java.
Diffstat (limited to 'ruby')
-rw-r--r-- | ruby/tests/tc_060_optargs.rb | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/ruby/tests/tc_060_optargs.rb b/ruby/tests/tc_060_optargs.rb new file mode 100644 index 00000000..232286a2 --- /dev/null +++ b/ruby/tests/tc_060_optargs.rb @@ -0,0 +1,33 @@ +# libguestfs Ruby bindings -*- ruby -*- +# Copyright (C) 2010 Red Hat Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +require 'test/unit' +$:.unshift(File::join(File::dirname(__FILE__), "..", "lib")) +$:.unshift(File::join(File::dirname(__FILE__), "..", "ext", "guestfs")) +require 'guestfs' + +class TestLoad < Test::Unit::TestCase + def test_optargs + g = Guestfs::create() + + g.add_drive_opts("/dev/null", {}) + g.add_drive_opts("/dev/null", :readonly => 1) + g.add_drive_opts("/dev/null", :readonly => 1, :iface => "virtio") + g.add_drive_opts("/dev/null", + :readonly => 1, :iface => "virtio", :format => "qcow2") + end +end |