af9686fc316aa05118eb242a034e087d1efe9616
[libguestfs.git] / python / guestfs-py-byhand.c
1 /* libguestfs python bindings
2  * Copyright (C) 2009-2011 Red Hat Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 /* This file contains a small number of functions that are written by
20  * hand.  The majority of the bindings are generated (see
21  * guestfs-py.c).
22  */
23
24 #include <config.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <assert.h>
29
30 #include "guestfs-py.h"
31
32 static PyObject **get_all_event_callbacks (guestfs_h *g, size_t *len_rtn);
33
34 PyObject *
35 py_guestfs_create (PyObject *self, PyObject *args)
36 {
37   guestfs_h *g;
38
39   g = guestfs_create ();
40   if (g == NULL) {
41     PyErr_SetString (PyExc_RuntimeError,
42                      "guestfs.create: failed to allocate handle");
43     return NULL;
44   }
45   guestfs_set_error_handler (g, NULL, NULL);
46   /* This can return NULL, but in that case put_handle will have
47    * set the Python error string.
48    */
49   return put_handle (g);
50 }
51
52 PyObject *
53 py_guestfs_close (PyObject *self, PyObject *args)
54 {
55   PyThreadState *py_save = NULL;
56   PyObject *py_g;
57   guestfs_h *g;
58   size_t i, len;
59   PyObject **callbacks;
60
61   if (!PyArg_ParseTuple (args, (char *) "O:guestfs_close", &py_g))
62     return NULL;
63   g = get_handle (py_g);
64
65   /* As in the OCaml bindings, there is a hard to solve case where the
66    * caller can delete a callback from within the callback, resulting
67    * in a double-free here.  XXX
68    */
69   callbacks = get_all_event_callbacks (g, &len);
70
71   if (PyEval_ThreadsInitialized ())
72     py_save = PyEval_SaveThread ();
73   guestfs_close (g);
74   if (PyEval_ThreadsInitialized ())
75     PyEval_RestoreThread (py_save);
76
77   for (i = 0; i < len; ++i)
78     Py_XDECREF (callbacks[i]);
79
80   Py_INCREF (Py_None);
81   return Py_None;
82 }
83
84 /* http://docs.python.org/release/2.5.2/ext/callingPython.html */
85 static void
86 py_guestfs_event_callback_wrapper (guestfs_h *g,
87                                    void *callback,
88                                    uint64_t event,
89                                    int event_handle,
90                                    int flags,
91                                    const char *buf, size_t buf_len,
92                                    const uint64_t *array, size_t array_len)
93 {
94   PyObject *py_callback = callback;
95   PyObject *py_array;
96   PyObject *args;
97   PyObject *a;
98   size_t i;
99   PyObject *py_r;
100
101   py_array = PyList_New (array_len);
102   for (i = 0; i < array_len; ++i) {
103     a = PyLong_FromLongLong (array[i]);
104     PyList_SET_ITEM (py_array, i, a);
105   }
106
107   /* XXX As with Perl we don't pass the guestfs_h handle here. */
108   args = Py_BuildValue ("(Kis#O)",
109                         (unsigned PY_LONG_LONG) event, event_handle,
110                         buf, buf_len, py_array);
111
112   py_r = PyEval_CallObject (py_callback, args);
113   Py_DECREF (args);
114
115   if (py_r != NULL)
116     Py_DECREF (py_r);
117   else
118     /* Callback threw an exception: print it. */
119     PyErr_PrintEx (0);
120 }
121
122 PyObject *
123 py_guestfs_set_event_callback (PyObject *self, PyObject *args)
124 {
125   PyObject *py_g;
126   guestfs_h *g;
127   PyObject *py_callback;
128   unsigned PY_LONG_LONG events;
129   int eh;
130   PyObject *py_eh;
131   char key[64];
132
133   if (!PyArg_ParseTuple (args, (char *) "OOK:guestfs_set_event_callback",
134                          &py_g, &py_callback, &events))
135     return NULL;
136
137   if (!PyCallable_Check (py_callback)) {
138     PyErr_SetString (PyExc_TypeError,
139                      "callback parameter is not callable "
140                      "(eg. lambda or function)");
141     return NULL;
142   }
143
144   g = get_handle (py_g);
145
146   eh = guestfs_set_event_callback (g, py_guestfs_event_callback_wrapper,
147                                    events, 0, py_callback);
148   if (eh == -1) {
149     PyErr_SetString (PyExc_RuntimeError, guestfs_last_error (g));
150     return NULL;
151   }
152
153   /* Increase the refcount for this callback since we are storing it
154    * in the opaque C libguestfs handle.  We need to remember that we
155    * did this, so we can decrease the refcount for all undeleted
156    * callbacks left around at close time (see py_guestfs_close).
157    */
158   Py_XINCREF (py_callback);
159
160   snprintf (key, sizeof key, "_python_event_%d", eh);
161   guestfs_set_private (g, key, py_callback);
162
163   py_eh = PyInt_FromLong ((long) eh);
164   return py_eh;
165 }
166
167 PyObject *
168 py_guestfs_delete_event_callback (PyObject *self, PyObject *args)
169 {
170   PyObject *py_g;
171   guestfs_h *g;
172   int eh;
173   PyObject *py_callback;
174   char key[64];
175
176   if (!PyArg_ParseTuple (args, (char *) "Oi:guestfs_delete_event_callback",
177                          &py_g, &eh))
178     return NULL;
179   g = get_handle (py_g);
180
181   snprintf (key, sizeof key, "_python_event_%d", eh);
182   py_callback = guestfs_get_private (g, key);
183   if (py_callback) {
184     Py_XDECREF (py_callback);
185     guestfs_set_private (g, key, NULL);
186     guestfs_delete_event_callback (g, eh);
187   }
188
189   Py_INCREF (Py_None);
190   return Py_None;
191 }
192
193 static PyObject **
194 get_all_event_callbacks (guestfs_h *g, size_t *len_rtn)
195 {
196   PyObject **r;
197   size_t i;
198   const char *key;
199   PyObject *cb;
200
201   /* Count the length of the array that will be needed. */
202   *len_rtn = 0;
203   cb = guestfs_first_private (g, &key);
204   while (cb != NULL) {
205     if (strncmp (key, "_python_event_", strlen ("_python_event_")) == 0)
206       (*len_rtn)++;
207     cb = guestfs_next_private (g, &key);
208   }
209
210   /* Copy them into the return array. */
211   r = guestfs_safe_malloc (g, sizeof (PyObject *) * (*len_rtn));
212
213   i = 0;
214   cb = guestfs_first_private (g, &key);
215   while (cb != NULL) {
216     if (strncmp (key, "_python_event_", strlen ("_python_event_")) == 0) {
217       r[i] = cb;
218       i++;
219     }
220     cb = guestfs_next_private (g, &key);
221   }
222
223   return r;
224 }