ruby-libvirt 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  #ifndef STORAGE_H
2
2
  #define STORAGE_H
3
3
 
4
- void init_storage();
4
+ void ruby_libvirt_storage_init(void);
5
5
 
6
6
  #endif
data/ext/libvirt/stream.c CHANGED
@@ -2,6 +2,7 @@
2
2
  * stream.c: virStream methods
3
3
  *
4
4
  * Copyright (C) 2007,2010 Red Hat Inc.
5
+ * Copyright (C) 2013 Chris Lalancette <clalancette@gmail.com>
5
6
  *
6
7
  * This library is free software; you can redistribute it and/or
7
8
  * modify it under the terms of the GNU Lesser General Public
@@ -28,120 +29,105 @@
28
29
  #if HAVE_TYPE_VIRSTREAMPTR
29
30
  static VALUE c_stream;
30
31
 
31
- static void stream_free(void *s) {
32
- generic_free(Stream, s);
32
+ static void stream_free(void *s)
33
+ {
34
+ ruby_libvirt_free_struct(Stream, s);
33
35
  }
34
36
 
35
- virStreamPtr stream_get(VALUE s) {
36
- generic_get(Stream, s);
37
+ virStreamPtr ruby_libvirt_stream_get(VALUE s)
38
+ {
39
+ ruby_libvirt_get_struct(Stream, s);
37
40
  }
38
41
 
39
- VALUE stream_new(virStreamPtr s, VALUE conn) {
40
- return generic_new(c_stream, s, conn, stream_free);
42
+ VALUE ruby_libvirt_stream_new(virStreamPtr s, VALUE conn)
43
+ {
44
+ return ruby_libvirt_new_class(c_stream, s, conn, stream_free);
41
45
  }
42
46
 
43
47
  /*
44
48
  * call-seq:
45
49
  * stream.send(buffer) -> Fixnum
46
50
  *
47
- * Call +virStreamSend+[http://www.libvirt.org/html/libvirt-libvirt.html#virStreamSend]
51
+ * Call virStreamSend[http://www.libvirt.org/html/libvirt-libvirt.html#virStreamSend]
48
52
  * to send the data in buffer out to the stream. The return value is the
49
53
  * number of bytes sent, which may be less than the size of the buffer. If
50
54
  * an error occurred, -1 is returned. If the transmit buffers are full and the
51
55
  * stream is marked non-blocking, returns -2.
52
56
  */
53
- static VALUE libvirt_stream_send(VALUE s, VALUE buffer) {
57
+ static VALUE libvirt_stream_send(VALUE s, VALUE buffer)
58
+ {
54
59
  int ret;
55
60
 
56
61
  StringValue(buffer);
57
62
 
58
- ret = virStreamSend(stream_get(s), RSTRING_PTR(buffer),
63
+ ret = virStreamSend(ruby_libvirt_stream_get(s), RSTRING_PTR(buffer),
59
64
  RSTRING_LEN(buffer));
60
- _E(ret == -1, create_error(e_RetrieveError, "virStreamSend", conn(s)));
65
+ ruby_libvirt_raise_error_if(ret == -1, e_RetrieveError, "virStreamSend",
66
+ ruby_libvirt_connect_get(s));
61
67
 
62
68
  return INT2NUM(ret);
63
69
  }
64
70
 
65
- struct stream_recv_args {
66
- int ret;
67
- char *data;
68
- };
69
-
70
- static VALUE stream_recv_array(VALUE input) {
71
- VALUE result;
72
- struct stream_recv_args *args = (struct stream_recv_args *)input;
73
-
74
- result = rb_ary_new();
75
-
76
- rb_ary_push(result, INT2NUM(args->ret));
77
- rb_ary_push(result, rb_str_new(args->data, args->ret));
78
-
79
- return result;
80
- }
81
-
82
71
  /*
83
72
  * call-seq:
84
73
  * stream.recv(bytes) -> [return_value, data]
85
74
  *
86
- * Call +virStreamRecv+[http://www.libvirt.org/html/libvirt-libvirt.html#virStreamRecv]
75
+ * Call virStreamRecv[http://www.libvirt.org/html/libvirt-libvirt.html#virStreamRecv]
87
76
  * to receive up to bytes amount of data from the stream. The return is an
88
77
  * array with two elements; the return code from the virStreamRecv call and
89
78
  * the data (as a String) read from the stream. If an error occurred, the
90
79
  * return_value is set to -1. If there is no data pending and the stream is
91
80
  * marked as non-blocking, return_value is set to -2.
92
81
  */
93
- static VALUE libvirt_stream_recv(VALUE s, VALUE bytes) {
82
+ static VALUE libvirt_stream_recv(VALUE s, VALUE bytes)
83
+ {
94
84
  char *data;
95
85
  int ret;
96
- int exception = 0;
97
86
  VALUE result;
98
- struct stream_recv_args args;
99
87
 
100
- data = ALLOC_N(char, NUM2INT(bytes));
88
+ data = alloca(sizeof(char) * NUM2INT(bytes));
101
89
 
102
- ret = virStreamRecv(stream_get(s), data, NUM2INT(bytes));
103
- if (ret == -1) {
104
- xfree(data);
105
- rb_exc_raise(create_error(e_RetrieveError, "virStreamRecv", conn(s)));
106
- }
90
+ ret = virStreamRecv(ruby_libvirt_stream_get(s), data, NUM2INT(bytes));
91
+ ruby_libvirt_raise_error_if(ret < 0, e_RetrieveError, "virStreamRecv",
92
+ ruby_libvirt_connect_get(s));
107
93
 
108
- args.ret = ret;
109
- args.data = data;
110
- result = rb_protect(stream_recv_array, (VALUE)&args, &exception);
111
- if (exception) {
112
- xfree(data);
113
- rb_jump_tag(exception);
114
- }
94
+ result = rb_ary_new2(2);
95
+
96
+ rb_ary_store(result, 0, INT2NUM(ret));
97
+ rb_ary_store(result, 1, rb_str_new(data, ret));
115
98
 
116
- xfree(data);
117
99
  return result;
118
100
  }
119
101
 
120
102
  static int internal_sendall(virStreamPtr st, char *data, size_t nbytes,
121
- void *opaque) {
122
- VALUE result;
123
- VALUE retcode, buffer;
103
+ void *opaque)
104
+ {
105
+ VALUE result, retcode, buffer;
124
106
 
125
107
  result = rb_yield_values(2, (VALUE)opaque, INT2NUM(nbytes));
126
108
 
127
- if (TYPE(result) != T_ARRAY)
109
+ if (TYPE(result) != T_ARRAY) {
128
110
  rb_raise(rb_eTypeError, "wrong type (expected Array)");
111
+ }
129
112
 
130
- if (RARRAY_LEN(result) != 2)
131
- rb_raise(rb_eArgError, "wrong number of arguments (%d for 2)",
113
+ if (RARRAY_LEN(result) != 2) {
114
+ rb_raise(rb_eArgError, "wrong number of arguments (%ld for 2)",
132
115
  RARRAY_LEN(result));
116
+ }
133
117
 
134
118
  retcode = rb_ary_entry(result, 0);
135
119
  buffer = rb_ary_entry(result, 1);
136
120
 
137
- if (NUM2INT(retcode) < 0)
121
+ if (NUM2INT(retcode) < 0) {
138
122
  return NUM2INT(retcode);
123
+ }
139
124
 
140
125
  StringValue(buffer);
141
126
 
142
- if (RSTRING_LEN(buffer) > nbytes)
143
- rb_raise(rb_eArgError, "asked for %d bytes, block returned %d", nbytes,
144
- RSTRING_LEN(buffer));
127
+ if (RSTRING_LEN(buffer) > nbytes) {
128
+ rb_raise(rb_eArgError, "asked for %zd bytes, block returned %ld",
129
+ nbytes, RSTRING_LEN(buffer));
130
+ }
145
131
 
146
132
  memcpy(data, RSTRING_PTR(buffer), RSTRING_LEN(buffer));
147
133
 
@@ -152,7 +138,7 @@ static int internal_sendall(virStreamPtr st, char *data, size_t nbytes,
152
138
  * call-seq:
153
139
  * stream.sendall(opaque=nil){|opaque, nbytes| send block} -> nil
154
140
  *
155
- * Call +virStreamSendAll+[http://www.libvirt.org/html/libvirt-libvirt.html#virStreamSendAll]
141
+ * Call virStreamSendAll[http://www.libvirt.org/html/libvirt-libvirt.html#virStreamSendAll]
156
142
  * to send the entire data stream. The send block is required and is executed
157
143
  * one or more times to send data. Each invocation of the send block yields
158
144
  * the opaque data passed into the initial call and the number of bytes this
@@ -161,29 +147,35 @@ static int internal_sendall(virStreamPtr st, char *data, size_t nbytes,
161
147
  * (-1 for error, 0 otherwise), and the second element should be the data
162
148
  * that the block prepared to send.
163
149
  */
164
- static VALUE libvirt_stream_sendall(int argc, VALUE *argv, VALUE s) {
150
+ static VALUE libvirt_stream_sendall(int argc, VALUE *argv, VALUE s)
151
+ {
165
152
  VALUE opaque;
166
153
  int ret;
167
154
 
168
- if (!rb_block_given_p())
155
+ if (!rb_block_given_p()) {
169
156
  rb_raise(rb_eRuntimeError, "A block must be provided");
157
+ }
170
158
 
171
159
  rb_scan_args(argc, argv, "01", &opaque);
172
160
 
173
- ret = virStreamSendAll(stream_get(s), internal_sendall, (void *)opaque);
174
- _E(ret < 0, create_error(e_RetrieveError, "virStreamSendAll", conn(s)));
161
+ ret = virStreamSendAll(ruby_libvirt_stream_get(s), internal_sendall,
162
+ (void *)opaque);
163
+ ruby_libvirt_raise_error_if(ret < 0, e_RetrieveError, "virStreamSendAll",
164
+ ruby_libvirt_connect_get(s));
175
165
 
176
166
  return Qnil;
177
167
  }
178
168
 
179
169
  static int internal_recvall(virStreamPtr st, const char *buf, size_t nbytes,
180
- void *opaque) {
170
+ void *opaque)
171
+ {
181
172
  VALUE result;
182
173
 
183
174
  result = rb_yield_values(2, rb_str_new(buf, nbytes), (VALUE)opaque);
184
175
 
185
- if (TYPE(result) != T_FIXNUM)
176
+ if (TYPE(result) != T_FIXNUM) {
186
177
  rb_raise(rb_eArgError, "wrong type (expected an integer)");
178
+ }
187
179
 
188
180
  return NUM2INT(result);
189
181
  }
@@ -192,62 +184,69 @@ static int internal_recvall(virStreamPtr st, const char *buf, size_t nbytes,
192
184
  * call-seq:
193
185
  * stream.recvall(opaque){|data, opaque| receive block} -> nil
194
186
  *
195
- * Call +virStreamRecvAll+[http://www.libvirt.org/html/libvirt-libvirt.html#virStreamRecvAll]
187
+ * Call virStreamRecvAll[http://www.libvirt.org/html/libvirt-libvirt.html#virStreamRecvAll]
196
188
  * to receive the entire data stream. The receive block is required and is
197
189
  * called one or more times to receive data. Each invocation of the receive
198
190
  * block yields the data received and the opaque data passed into the initial
199
191
  * call. The block should return -1 if an error occurred and 0 otherwise.
200
192
  */
201
- static VALUE libvirt_stream_recvall(int argc, VALUE *argv, VALUE s) {
193
+ static VALUE libvirt_stream_recvall(int argc, VALUE *argv, VALUE s)
194
+ {
202
195
  VALUE opaque;
203
196
  int ret;
204
197
 
205
- if (!rb_block_given_p())
198
+ if (!rb_block_given_p()) {
206
199
  rb_raise(rb_eRuntimeError, "A block must be provided");
200
+ }
207
201
 
208
202
  rb_scan_args(argc, argv, "01", &opaque);
209
203
 
210
- ret = virStreamRecvAll(stream_get(s), internal_recvall, (void *)opaque);
211
- _E(ret < 0, create_error(e_RetrieveError, "virStreamRecvAll", conn(s)));
204
+ ret = virStreamRecvAll(ruby_libvirt_stream_get(s), internal_recvall,
205
+ (void *)opaque);
206
+ ruby_libvirt_raise_error_if(ret < 0, e_RetrieveError, "virStreamRecvAll",
207
+ ruby_libvirt_connect_get(s));
212
208
 
213
209
  return Qnil;
214
210
  }
215
211
 
216
- static void stream_event_callback(virStreamPtr st, int events, void *opaque) {
212
+ static void stream_event_callback(virStreamPtr st, int events, void *opaque)
213
+ {
217
214
  VALUE passthrough = (VALUE)opaque;
218
- VALUE cb;
219
- VALUE cb_opaque;
220
- VALUE news;
221
- VALUE s;
215
+ VALUE cb, cb_opaque, news, s;
222
216
 
223
- if (TYPE(passthrough) != T_ARRAY)
217
+ if (TYPE(passthrough) != T_ARRAY) {
224
218
  rb_raise(rb_eTypeError,
225
219
  "wrong domain event lifecycle callback argument type (expected Array)");
220
+ }
226
221
 
227
- if (RARRAY_LEN(passthrough) != 3)
228
- rb_raise(rb_eArgError, "wrong number of arguments (%d for 3)",
222
+ if (RARRAY_LEN(passthrough) != 3) {
223
+ rb_raise(rb_eArgError, "wrong number of arguments (%ld for 3)",
229
224
  RARRAY_LEN(passthrough));
225
+ }
230
226
 
231
227
  cb = rb_ary_entry(passthrough, 0);
232
228
  cb_opaque = rb_ary_entry(passthrough, 1);
233
229
  s = rb_ary_entry(passthrough, 2);
234
230
 
235
- news = stream_new(st, conn_attr(s));
236
- if (strcmp(rb_obj_classname(cb), "Symbol") == 0)
231
+ news = ruby_libvirt_stream_new(st, ruby_libvirt_conn_attr(s));
232
+ if (strcmp(rb_obj_classname(cb), "Symbol") == 0) {
237
233
  rb_funcall(rb_class_of(cb), rb_to_id(cb), 3, news, INT2NUM(events),
238
234
  cb_opaque);
239
- else if (strcmp(rb_obj_classname(cb), "Proc") == 0)
235
+ }
236
+ else if (strcmp(rb_obj_classname(cb), "Proc") == 0) {
240
237
  rb_funcall(cb, rb_intern("call"), 3, news, INT2NUM(events), cb_opaque);
241
- else
238
+ }
239
+ else {
242
240
  rb_raise(rb_eTypeError,
243
241
  "wrong stream event callback (expected Symbol or Proc)");
242
+ }
244
243
  }
245
244
 
246
245
  /*
247
246
  * call-seq:
248
247
  * stream.event_add_callback(events, callback, opaque=nil) -> nil
249
248
  *
250
- * Call +virStreamEventAddCallback+[http://www.libvirt.org/html/libvirt-libvirt.html#virStreamEventAddCallback]
249
+ * Call virStreamEventAddCallback[http://www.libvirt.org/html/libvirt-libvirt.html#virStreamEventAddCallback]
251
250
  * to register a callback to be notified when a stream becomes readable or
252
251
  * writeable. The events parameter is an integer representing the events the
253
252
  * user is interested in; it should be one or more of EVENT_READABLE,
@@ -258,28 +257,29 @@ static void stream_event_callback(virStreamPtr st, int events, void *opaque) {
258
257
  * an opaque pointer that was (optionally) passed into
259
258
  * stream.event_add_callback to begin with.
260
259
  */
261
- static VALUE libvirt_stream_event_add_callback(int argc, VALUE *argv, VALUE s) {
262
- VALUE events;
263
- VALUE callback;
264
- VALUE opaque;
265
- VALUE passthrough;
260
+ static VALUE libvirt_stream_event_add_callback(int argc, VALUE *argv, VALUE s)
261
+ {
262
+ VALUE events, callback, opaque, passthrough;
266
263
  int ret;
267
264
 
268
265
  rb_scan_args(argc, argv, "21", &events, &callback, &opaque);
269
266
 
270
- if (!is_symbol_or_proc(callback))
271
- rb_raise(rb_eTypeError, "wrong argument type (expected Symbol or Proc)");
267
+ if (!ruby_libvirt_is_symbol_or_proc(callback)) {
268
+ rb_raise(rb_eTypeError,
269
+ "wrong argument type (expected Symbol or Proc)");
270
+ }
272
271
 
273
- passthrough = rb_ary_new();
272
+ passthrough = rb_ary_new2(3);
274
273
  rb_ary_store(passthrough, 0, callback);
275
274
  rb_ary_store(passthrough, 1, opaque);
276
275
  rb_ary_store(passthrough, 2, s);
277
276
 
278
- ret = virStreamEventAddCallback(stream_get(s), NUM2INT(events),
277
+ ret = virStreamEventAddCallback(ruby_libvirt_stream_get(s), NUM2INT(events),
279
278
  stream_event_callback, (void *)passthrough,
280
279
  NULL);
281
- _E(ret < 0, create_error(e_RetrieveError, "virStreamEventAddCallback",
282
- conn(s)));
280
+ ruby_libvirt_raise_error_if(ret < 0, e_RetrieveError,
281
+ "virStreamEventAddCallback",
282
+ ruby_libvirt_connect_get(s));
283
283
 
284
284
  return Qnil;
285
285
  }
@@ -288,79 +288,78 @@ static VALUE libvirt_stream_event_add_callback(int argc, VALUE *argv, VALUE s) {
288
288
  * call-seq:
289
289
  * stream.event_update_callback(events) -> nil
290
290
  *
291
- * Call +virStreamEventUpdateCallback+[http://www.libvirt.org/html/libvirt-libvirt.html#virStreamEventUpdateCallback]
291
+ * Call virStreamEventUpdateCallback[http://www.libvirt.org/html/libvirt-libvirt.html#virStreamEventUpdateCallback]
292
292
  * to change the events that the event callback is looking for. The events
293
293
  * parameter is an integer representing the events the user is interested in;
294
294
  * it should be one or more of EVENT_READABLE, EVENT_WRITABLE, EVENT_ERROR,
295
295
  * and EVENT_HANGUP, ORed together.
296
296
  */
297
- static VALUE libvirt_stream_event_update_callback(VALUE s, VALUE events) {
298
- int ret;
299
-
300
- ret = virStreamEventUpdateCallback(stream_get(s), NUM2INT(events));
301
- _E(ret < 0, create_error(e_RetrieveError, "virStreamEventUpdateCallback",
302
- conn(s)));
303
-
304
- return Qnil;
297
+ static VALUE libvirt_stream_event_update_callback(VALUE s, VALUE events)
298
+ {
299
+ ruby_libvirt_generate_call_nil(virStreamEventUpdateCallback,
300
+ ruby_libvirt_connect_get(s),
301
+ ruby_libvirt_stream_get(s), NUM2INT(events));
305
302
  }
306
303
 
307
304
  /*
308
305
  * call-seq:
309
306
  * stream.event_remove_callback -> nil
310
307
  *
311
- * Call +virStreamEventRemoveCallback+[http://www.libvirt.org/html/libvirt-libvirt.html#virStreamEventRemoveCallback]
308
+ * Call virStreamEventRemoveCallback[http://www.libvirt.org/html/libvirt-libvirt.html#virStreamEventRemoveCallback]
312
309
  * to remove the event callback currently registered to this stream.
313
310
  */
314
- static VALUE libvirt_stream_event_remove_callback(VALUE s) {
315
- int ret;
316
-
317
- ret = virStreamEventRemoveCallback(stream_get(s));
318
- _E(ret < 0, create_error(e_RetrieveError, "virStreamEventRemoveCallback",
319
- conn(s)));
320
-
321
- return Qnil;
311
+ static VALUE libvirt_stream_event_remove_callback(VALUE s)
312
+ {
313
+ ruby_libvirt_generate_call_nil(virStreamEventRemoveCallback,
314
+ ruby_libvirt_connect_get(s),
315
+ ruby_libvirt_stream_get(s));
322
316
  }
323
317
 
324
318
  /*
325
319
  * call-seq:
326
320
  * stream.finish -> nil
327
321
  *
328
- * Call +virStreamFinish+[http://www.libvirt.org/html/libvirt-libvirt.html#virStreamFinish]
322
+ * Call virStreamFinish[http://www.libvirt.org/html/libvirt-libvirt.html#virStreamFinish]
329
323
  * to finish this stream. Finish is typically used when the stream is no
330
324
  * longer needed and needs to be cleaned up.
331
325
  */
332
- static VALUE libvirt_stream_finish(VALUE s) {
333
- gen_call_void(virStreamFinish, conn(s), stream_get(s));
326
+ static VALUE libvirt_stream_finish(VALUE s)
327
+ {
328
+ ruby_libvirt_generate_call_nil(virStreamFinish, ruby_libvirt_connect_get(s),
329
+ ruby_libvirt_stream_get(s));
334
330
  }
335
331
 
336
332
  /*
337
333
  * call-seq:
338
334
  * stream.abort -> nil
339
335
  *
340
- * Call +virStreamAbort+[http://www.libvirt.org/html/libvirt-libvirt.html#virStreamAbort]
336
+ * Call virStreamAbort[http://www.libvirt.org/html/libvirt-libvirt.html#virStreamAbort]
341
337
  * to abort this stream. Abort is typically used when something on the stream
342
338
  * has failed, and the stream needs to be cleaned up.
343
339
  */
344
- static VALUE libvirt_stream_abort(VALUE s) {
345
- gen_call_void(virStreamAbort, conn(s), stream_get(s));
340
+ static VALUE libvirt_stream_abort(VALUE s)
341
+ {
342
+ ruby_libvirt_generate_call_nil(virStreamAbort, ruby_libvirt_connect_get(s),
343
+ ruby_libvirt_stream_get(s));
346
344
  }
347
345
 
348
346
  /*
349
347
  * call-seq:
350
348
  * stream.free -> nil
351
349
  *
352
- * Call +virStreamFree+[http://www.libvirt.org/html/libvirt-libvirt.html#virStreamFree]
350
+ * Call virStreamFree[http://www.libvirt.org/html/libvirt-libvirt.html#virStreamFree]
353
351
  * to free this stream. The object will no longer be valid after this call.
354
352
  */
355
- static VALUE libvirt_stream_free(VALUE s) {
356
- gen_call_free(Stream, s);
353
+ static VALUE libvirt_stream_free(VALUE s)
354
+ {
355
+ ruby_libvirt_generate_call_free(Stream, s);
357
356
  }
358
357
  #endif
359
358
 
360
359
  /*
361
- * Class Libvirt::Domain
360
+ * Class Libvirt::Stream
362
361
  */
363
- void init_stream()
362
+ void ruby_libvirt_stream_init(void)
364
363
  {
365
364
  #if HAVE_TYPE_VIRSTREAMPTR
366
365
  c_stream = rb_define_class_under(m_libvirt, "Stream", rb_cObject);
@@ -368,7 +367,6 @@ void init_stream()
368
367
  rb_define_attr(c_stream, "connection", 1, 0);
369
368
 
370
369
  rb_define_const(c_stream, "NONBLOCK", INT2NUM(VIR_STREAM_NONBLOCK));
371
-
372
370
  rb_define_const(c_stream, "EVENT_READABLE",
373
371
  INT2NUM(VIR_STREAM_EVENT_READABLE));
374
372
  rb_define_const(c_stream, "EVENT_WRITABLE",
@@ -377,7 +375,7 @@ void init_stream()
377
375
  rb_define_const(c_stream, "EVENT_HANGUP", INT2NUM(VIR_STREAM_EVENT_HANGUP));
378
376
 
379
377
  rb_define_method(c_stream, "send", libvirt_stream_send, 1);
380
- rb_define_method(c_stream, "recv", libvirt_stream_recv, 2);
378
+ rb_define_method(c_stream, "recv", libvirt_stream_recv, 1);
381
379
  rb_define_method(c_stream, "sendall", libvirt_stream_sendall, -1);
382
380
  rb_define_method(c_stream, "recvall", libvirt_stream_recvall, -1);
383
381