mirror of
https://github.com/libguestfs/libguestfs.git
synced 2026-03-21 22:53:37 +00:00
lua: Various fixes to the bindings (thanks Jerome Vuarand).
See http://article.gmane.org/gmane.comp.lang.lua.general/95065 Note that this is incompatible with existing code. You have to do: local G = require "guestfs" local g = G.create () ie. give the module your own name ("G" in that example).
This commit is contained in:
@@ -742,9 +742,9 @@ and generate_erlang_bindtests () =
|
||||
and generate_lua_bindtests () =
|
||||
generate_header LuaStyle GPLv2plus;
|
||||
|
||||
pr "require \"guestfs\"\n";
|
||||
pr "local G = require \"guestfs\"\n";
|
||||
pr "\n";
|
||||
pr "g = Guestfs.create ()\n";
|
||||
pr "local g = G.create ()\n";
|
||||
pr "\n";
|
||||
|
||||
generate_lang_bindtests (
|
||||
|
||||
@@ -42,6 +42,7 @@ let generate_lua_c () =
|
||||
#include <inttypes.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <assert.h>
|
||||
|
||||
/*#define LUA_LIB*/
|
||||
#include <lua.h>
|
||||
@@ -148,6 +149,7 @@ lua_guestfs_create (lua_State *L)
|
||||
|
||||
u = lua_newuserdata (L, sizeof (struct userdata));
|
||||
luaL_getmetatable (L, LUA_GUESTFS_HANDLE);
|
||||
assert (lua_type (L, -1) == LUA_TTABLE);
|
||||
lua_setmetatable (L, -2);
|
||||
|
||||
u->g = g;
|
||||
@@ -818,9 +820,22 @@ push_event (lua_State *L, uint64_t event)
|
||||
|
||||
pr "\
|
||||
|
||||
static luaL_Reg handle_methods[] = {
|
||||
/* Metamethods.
|
||||
* See: http://article.gmane.org/gmane.comp.lang.lua.general/95065
|
||||
*/
|
||||
static luaL_Reg metamethods[] = {
|
||||
{ \"__gc\", lua_guestfs_finalizer },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
/* Module functions. */
|
||||
static luaL_Reg functions[] = {
|
||||
{ \"create\", lua_guestfs_create },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
/* Methods. */
|
||||
static luaL_Reg methods[] = {
|
||||
{ \"close\", lua_guestfs_close },
|
||||
{ \"user_cancel\", lua_guestfs_user_cancel },
|
||||
{ \"set_event_callback\", lua_guestfs_set_event_callback },
|
||||
@@ -859,20 +874,30 @@ luaopen_guestfs (lua_State *L)
|
||||
{
|
||||
char v[256];
|
||||
|
||||
/* Create metatable and register methods into it. */
|
||||
/* Create metatable. */
|
||||
luaL_newmetatable (L, LUA_GUESTFS_HANDLE);
|
||||
luaL_register (L, NULL /* \"guestfs\" ? XXX */, handle_methods);
|
||||
luaL_register (L, NULL, metamethods);
|
||||
|
||||
/* Set __index field of metatable to point to itself. */
|
||||
lua_pushvalue (L, -1);
|
||||
lua_setfield (L, -1, \"__index\");
|
||||
/* Create methods table. */
|
||||
lua_newtable (L);
|
||||
luaL_register (L, NULL, methods);
|
||||
|
||||
/* Globals in the Guestfs.* namespace. */
|
||||
/* Set __index field of metatable to point to methods table. */
|
||||
lua_setfield (L, -2, \"__index\");
|
||||
|
||||
/* Pop metatable, it is no longer needed. */
|
||||
lua_pop (L, 1);
|
||||
|
||||
/* Create module functions table. */
|
||||
lua_newtable (L);
|
||||
luaL_register (L, NULL, functions);
|
||||
|
||||
/* Globals in the module namespace. */
|
||||
lua_pushliteral (L, \"event_all\");
|
||||
push_string_list (L, (char **) event_all);
|
||||
lua_settable (L, -3);
|
||||
|
||||
/* Add _COPYRIGHT, etc. fields to the metatable. */
|
||||
/* Add _COPYRIGHT, etc. fields to the module namespace. */
|
||||
lua_pushliteral (L, \"_COPYRIGHT\");
|
||||
lua_pushliteral (L, \"Copyright (C) %s Red Hat Inc.\");
|
||||
lua_settable (L, -3);
|
||||
@@ -886,9 +911,10 @@ luaopen_guestfs (lua_State *L)
|
||||
lua_pushlstring (L, v, strlen (v));
|
||||
lua_settable (L, -3);
|
||||
|
||||
/* Expose metatable to lua as \"Guestfs\". */
|
||||
lua_setglobal (L, \"Guestfs\");
|
||||
|
||||
/* Return module table, so users choose their own name for the module:
|
||||
* local G = require \"guestfs\"
|
||||
* g = G.create ()
|
||||
*/
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
-- Example showing how to create a disk image.
|
||||
|
||||
require "guestfs"
|
||||
local G = require "guestfs"
|
||||
|
||||
output = "disk.img"
|
||||
local output = "disk.img"
|
||||
|
||||
g = Guestfs.create ()
|
||||
local g = G.create ()
|
||||
|
||||
-- Create a raw-format sparse disk image, 512 MB in size.
|
||||
file = io.open (output, "w")
|
||||
|
||||
@@ -6,8 +6,8 @@ guestfs-lua - How to use libguestfs from Lua
|
||||
|
||||
=head1 SYNOPSIS
|
||||
|
||||
require "guestfs"
|
||||
g = Guestfs.create ()
|
||||
local G = require "guestfs"
|
||||
g = G.create ()
|
||||
g:add_drive ("test.img", { format = "raw", readonly = true })
|
||||
g:launch ()
|
||||
devices = g:list_devices ()
|
||||
@@ -20,15 +20,25 @@ programming language. This page just documents the differences from
|
||||
the C API and gives some examples. If you are not familiar with using
|
||||
libguestfs, you also need to read L<guestfs(3)>.
|
||||
|
||||
=head2 REQUIRING THE MODULE
|
||||
|
||||
C<require "guestfs"> returns the module, so you have to assign it
|
||||
to a local variable. Typical usage is:
|
||||
|
||||
local G = require "guestfs"
|
||||
|
||||
(you can use any name you want instead of C<G>, but in the examples
|
||||
in this man page we always use C<G>).
|
||||
|
||||
=head2 OPENING AND CLOSING THE HANDLE
|
||||
|
||||
To create a new handle, call:
|
||||
|
||||
g = Guestfs.create ()
|
||||
g = G.create ()
|
||||
|
||||
You can also use the optional arguments:
|
||||
|
||||
g = Guestfs.create { environment = 0, close_on_exit = 0 }
|
||||
g = G.create { environment = 0, close_on_exit = 0 }
|
||||
|
||||
to set the flags C<GUESTFS_CREATE_NO_ENVIRONMENT>
|
||||
and/or C<GUESTFS_CREATE_NO_CLOSE_ON_EXIT>.
|
||||
@@ -95,7 +105,7 @@ second argument a list:
|
||||
eh = g:set_event_callback (cb, { "appliance", "library", "trace" })
|
||||
|
||||
A list of all valid event types (strings) is in the global variable
|
||||
C<Guestfs.event_all>.
|
||||
C<G.event_all>.
|
||||
|
||||
The callback (C<cb>) is called with the following parameters:
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
-- Example showing how to inspect a virtual machine disk.
|
||||
|
||||
require "guestfs"
|
||||
local G = require "guestfs"
|
||||
|
||||
if table.getn (arg) == 1 then
|
||||
disk = arg[1]
|
||||
@@ -8,7 +8,7 @@ else
|
||||
error ("usage: inspect_vm disk.img")
|
||||
end
|
||||
|
||||
g = Guestfs.create ()
|
||||
local g = G.create ()
|
||||
|
||||
-- Attach the disk image read-only to libguestfs.
|
||||
g:add_drive (disk, { -- format:"raw"
|
||||
@@ -18,7 +18,7 @@ g:add_drive (disk, { -- format:"raw"
|
||||
g:launch ()
|
||||
|
||||
-- Ask libguestfs to inspect for operating systems.
|
||||
roots = g:inspect_os ()
|
||||
local roots = g:inspect_os ()
|
||||
if table.getn (roots) == 0 then
|
||||
error ("inspect_vm: no operating systems found")
|
||||
end
|
||||
|
||||
@@ -16,11 +16,11 @@
|
||||
-- along with this program; if not, write to the Free Software
|
||||
-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
require "guestfs"
|
||||
local G = require "guestfs"
|
||||
|
||||
assert (Guestfs._COPYRIGHT)
|
||||
print ("_COPYRIGHT = ", Guestfs._COPYRIGHT)
|
||||
assert (Guestfs._DESCRIPTION)
|
||||
print ("_DESCRIPTION = ", Guestfs._DESCRIPTION)
|
||||
assert (Guestfs._VERSION)
|
||||
print ("_VERSION = ", Guestfs._VERSION)
|
||||
assert (G._COPYRIGHT)
|
||||
print ("_COPYRIGHT = ", G._COPYRIGHT)
|
||||
assert (G._DESCRIPTION)
|
||||
print ("_DESCRIPTION = ", G._DESCRIPTION)
|
||||
assert (G._VERSION)
|
||||
print ("_VERSION = ", G._VERSION)
|
||||
|
||||
@@ -16,6 +16,6 @@
|
||||
-- along with this program; if not, write to the Free Software
|
||||
-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
require "guestfs"
|
||||
local G = require "guestfs"
|
||||
|
||||
local g = Guestfs.create ()
|
||||
local g = G.create ()
|
||||
|
||||
@@ -16,6 +16,6 @@
|
||||
-- along with this program; if not, write to the Free Software
|
||||
-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
require "guestfs"
|
||||
local G = require "guestfs"
|
||||
|
||||
local g = Guestfs.create { environment = 0 }
|
||||
local g = G.create { environment = 0 }
|
||||
|
||||
@@ -16,11 +16,11 @@
|
||||
-- along with this program; if not, write to the Free Software
|
||||
-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
require "guestfs"
|
||||
local G = require "guestfs"
|
||||
|
||||
g1 = Guestfs.create ()
|
||||
g2 = Guestfs.create ()
|
||||
g3 = Guestfs.create ()
|
||||
local g1 = G.create ()
|
||||
local g2 = G.create ()
|
||||
local g3 = G.create ()
|
||||
|
||||
-- Check that each handle is independent.
|
||||
g1:set_path ("1")
|
||||
|
||||
@@ -16,9 +16,9 @@
|
||||
-- along with this program; if not, write to the Free Software
|
||||
-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
require "guestfs"
|
||||
local G = require "guestfs"
|
||||
|
||||
local g = Guestfs.create ()
|
||||
local g = G.create ()
|
||||
|
||||
local verbose = g:get_verbose ()
|
||||
g:set_verbose (true)
|
||||
|
||||
@@ -16,9 +16,9 @@
|
||||
-- along with this program; if not, write to the Free Software
|
||||
-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
require "guestfs"
|
||||
local G = require "guestfs"
|
||||
|
||||
local g = Guestfs.create ()
|
||||
local g = G.create ()
|
||||
|
||||
file = io.open ("test.img", "w")
|
||||
file:seek ("set", 500 * 1024 * 1024)
|
||||
|
||||
@@ -16,9 +16,9 @@
|
||||
-- along with this program; if not, write to the Free Software
|
||||
-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
require "guestfs"
|
||||
local G = require "guestfs"
|
||||
|
||||
local g = Guestfs.create ()
|
||||
local g = G.create ()
|
||||
|
||||
file = io.open ("test.img", "w")
|
||||
file:seek ("set", 10 * 1024 * 1024)
|
||||
|
||||
@@ -16,9 +16,9 @@
|
||||
-- along with this program; if not, write to the Free Software
|
||||
-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
require "guestfs"
|
||||
local G = require "guestfs"
|
||||
|
||||
local g = Guestfs.create ()
|
||||
local g = G.create ()
|
||||
|
||||
g:add_drive ("/dev/null")
|
||||
g:add_drive ("/dev/null", { readonly = true })
|
||||
|
||||
@@ -16,13 +16,13 @@
|
||||
-- along with this program; if not, write to the Free Software
|
||||
-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
require "guestfs"
|
||||
local G = require "guestfs"
|
||||
|
||||
for i, v in ipairs (Guestfs.event_all) do
|
||||
for i, v in ipairs (G.event_all) do
|
||||
print (i, v)
|
||||
end
|
||||
|
||||
g = Guestfs.create ()
|
||||
local g = G.create ()
|
||||
|
||||
function log_callback (g, event, eh, flags, buf, array)
|
||||
io.write (string.format ("lua event logged: event=%s eh=%d buf='%s'\n",
|
||||
|
||||
@@ -16,9 +16,9 @@
|
||||
-- along with this program; if not, write to the Free Software
|
||||
-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
require "guestfs"
|
||||
local G = require "guestfs"
|
||||
|
||||
g = Guestfs.create ()
|
||||
local g = G.create ()
|
||||
|
||||
g:add_drive ("/dev/null")
|
||||
g:launch ()
|
||||
|
||||
Reference in New Issue
Block a user