2 * Copyright (c) 2007, 2008, 2009, 2010 Zmanda, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published
6 * by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18 * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
21 %module "Amanda::Device"
22 %include "amglue/amglue.swg"
23 %include "exception.i"
24 %import "Amanda/Header.swg";
26 %include "Amanda/Device.pod"
31 #include "fileheader.h"
32 #include "glib-util.h"
33 #include "simpleprng.h"
35 #include "sockaddr-util.h"
39 /* Initialize the Device API on load */
45 /* Utility functions for typemaps, below */
47 /* return a new, mortal SV corresponding to the given GValue
49 * @param value: the value to convert
50 * @returns: a new, mortal SV
53 set_sv_from_gvalue(GValue *value)
55 GType fundamental = G_TYPE_FUNDAMENTAL(G_VALUE_TYPE(value));
58 /* complex reference types */
59 switch (fundamental) {
61 return sv_2mortal(amglue_newSVi64(g_value_get_long(value)));
64 return sv_2mortal(amglue_newSVu64(g_value_get_ulong(value)));
67 return sv_2mortal(amglue_newSVi64(g_value_get_int64(value)));
70 return sv_2mortal(amglue_newSVu64(g_value_get_uint64(value)));
73 /* simple types that can be constructed with sv_set*v */
75 switch (fundamental) {
77 sv_setiv(sv, g_value_get_char(value));
81 sv_setuv(sv, g_value_get_uchar(value));
85 sv_setiv(sv, g_value_get_boolean(value));
89 sv_setiv(sv, g_value_get_int(value));
93 sv_setuv(sv, g_value_get_uint(value));
97 sv_setnv(sv, g_value_get_float(value));
101 sv_setnv(sv, g_value_get_double(value));
105 sv_setpv(sv, g_value_get_string(value));
109 sv_setiv(sv, g_value_get_enum(value));
113 sv_setiv(sv, g_value_get_flags(value));
119 case G_TYPE_INTERFACE:
122 warn("Unsupported fundamental property type #%d", (int)fundamental);
123 sv_setsv(sv, &PL_sv_undef);
130 /* Given an SV and an initialized GValue, set the GValue to the value
131 * represented by the SV. The GValue's type must already be set.
133 * For basic corresponding types (string -> string, integer -> integer),
134 * the translation is straightforward. However, if the GValue is not a
135 * string, but the SV has a string value, then g_value_set_from_string will
136 * be used to parse the string.
138 * @param sv: SV to convert
139 * @param value: (input/output) destination
140 * @returns: TRUE on success
143 set_gvalue_from_sv(SV *sv, GValue *value)
145 GType fundamental = G_TYPE_FUNDAMENTAL(G_VALUE_TYPE(value));
147 /* if we got a string, use g_value_set_from_string to parse any funny
148 * values or suffixes */
150 if (g_value_set_from_string(value, SvPV_nolen(sv)))
154 /* otherwise, handle numeric types with SvIV, SvNV, or the amglue_* functions */
155 switch (fundamental) {
157 g_value_set_boolean(value, SvIV(sv));
161 g_value_set_char(value, amglue_SvI8(sv));
165 g_value_set_uchar(value, amglue_SvU8(sv));
169 g_value_set_int(value, amglue_SvI32(sv));
173 g_value_set_uint(value, amglue_SvU32(sv));
177 g_value_set_int64(value, amglue_SvI64(sv));
181 g_value_set_uint64(value, amglue_SvU64(sv));
185 g_value_set_int64(value, amglue_SvI64(sv));
189 g_value_set_uint64(value, amglue_SvU64(sv));
193 g_value_set_float(value, SvNV(sv));
197 g_value_set_double(value, SvNV(sv));
201 g_value_set_enum(value, SvIV(sv));
205 g_value_set_flags(value, SvIV(sv));
209 /* for anything else, let perl stringify it for us and try parsing it */
210 return g_value_set_from_string(value, SvPV_nolen(sv));
217 * DirectTCPConnection object
220 typedef struct DirectTCPConnection {
222 ~DirectTCPConnection() {
223 g_object_unref(self);
228 return directtcp_connection_close(self);
231 } DirectTCPConnection;
234 * Device struct, %extend-ed into a Perl class
237 %name(unaliased_name) extern char *device_unaliased_name(char *);
239 typedef struct Device {
244 Device(char *device_name) {
245 return device_open(device_name);
249 g_object_unref(self);
253 configure(gboolean use_global_config) {
254 return device_configure(self, use_global_config);
259 return device_error(self);
264 return device_status_error(self);
269 return device_error_or_status(self);
274 return device_read_label(self);
278 start(DeviceAccessMode mode, char *label, char *timestamp) {
279 return device_start(self, mode, label, timestamp);
284 return device_finish(self);
288 start_file(dumpfile_t *jobInfo) {
289 return device_start_file(self, jobInfo);
293 write_block(guint size, gpointer data) {
294 return device_write_block(self, size, data);
299 return device_finish_file(self);
303 seek_file(guint file) {
304 return device_seek_file(self, file);
308 seek_block(guint64 block) {
309 return device_seek_block(self, block);
313 read_block(gpointer buffer, int *size) {
314 return device_read_block(self, buffer, size);
319 return device_erase(self);
324 return device_eject(self);
328 directtcp_supported() {
329 return device_directtcp_supported(self);
333 listen(gboolean for_writing, DirectTCPAddr **addrs) {
334 /* ensure that the addresses are empty if there was an error */
335 if (!device_listen(self, for_writing, addrs))
339 %newobject accept; /* connection is already ref'd, so we own it */
340 DirectTCPConnection *
342 DirectTCPConnection *conn = NULL;
345 rv = device_accept(self, &conn, NULL, NULL);
347 /* conn is ref'd for our convenience, but we don't want it */
348 g_object_unref(conn);
354 %newobject connect; /* connection is already ref'd, so we own it */
355 DirectTCPConnection *
356 connect(gboolean for_writing, DirectTCPAddr *addrs) {
357 DirectTCPConnection *conn = NULL;
360 rv = device_connect(self, for_writing, addrs, &conn, NULL, NULL);
362 /* conn is ref'd for our convenience, but we don't want it */
363 g_object_unref(conn);
370 use_connection(DirectTCPConnection *conn) {
371 return device_use_connection(self, conn);
374 %typemap(in,numinputs=0) guint64 *actual_size (guint64 sz) {
378 %typemap(argout) guint64 *actual_size {
379 SP += argvi; PUTBACK;
380 $result = sv_2mortal(amglue_newSVu64(*$1));
381 SPAGAIN; SP -= argvi; argvi++;
384 write_from_connection(guint64 size, guint64 *actual_size) {
385 return device_write_from_connection(self, size, actual_size);
389 read_to_connection(guint64 size, guint64 *actual_size) {
390 return device_read_to_connection(self, size, actual_size);
393 %typemap(out) const GSList * {
396 /* Count the DeviceProperties */
397 EXTEND(SP, g_slist_length($1)); /* make room for return values */
399 /* Note that we set $result several times. the nature of
400 * SWIG's wrapping is such that incrementing argvi points
401 * $result to the next location in perl's argument stack.
404 for (iter = $1; iter; iter = g_slist_next(iter)) {
405 DeviceProperty *prop = iter->data;
407 SV *rv = newRV_noinc((SV *)hash);
409 hv_store(hash, "name", 4,
410 newSVpv(prop->base->name, 0), 0);
411 hv_store(hash, "description", 11,
412 newSVpv(prop->base->description, 0), 0);
413 hv_store(hash, "access", 6,
414 newSViv(prop->access), 0);
415 $result = sv_2mortal(rv);
419 const GSList * property_list(void) {
420 return device_property_get_list(self);
423 %typemap(out) const GSList *; /* remove typemap */
425 /* A typemap to convert a property name to a DevicePropertyBase. */
426 %typemap(in) DevicePropertyBase * {
430 pname = SvPV_nolen($input);
433 $1 = (DevicePropertyBase *)device_property_get_by_name(pname);
438 /* A typemap to convert the GValue in property_get to a return value. The
439 * (in) typemap sets up storage for the parameters, while the (argout) converts
440 * them to a perl SV. */
441 %typemap(in,numinputs=0) (GValue *out_val, PropertySurety *surety,
442 PropertySource *source, gboolean *val_found)
444 PropertySurety surety,
445 PropertySource source,
447 memset(&val, 0, sizeof(val));
449 if (GIMME_V == G_ARRAY) {
455 %typemap(argout) (GValue *out_val, PropertySurety *surety,
456 PropertySource *source, gboolean *val_found) {
457 /* if the result is valid */
459 /* move data from $1 to $result, somehow, being careful to
460 * save the perl stack while doing so */
461 SP += argvi; PUTBACK;
462 $result = set_sv_from_gvalue($1);
463 SPAGAIN; SP -= argvi; argvi++;
465 /* free any memory for the GValue */
468 if (GIMME_V == G_ARRAY) {
469 $result = newSViv(*$2);
471 $result = newSViv(*$3);
475 /* otherwise, return nothing */
479 property_get(DevicePropertyBase *pbase, GValue *out_val, PropertySurety *surety,
480 PropertySource *source, gboolean *val_found) {
482 *val_found = device_property_get_ex(self, pbase->ID, out_val, surety, source);
488 /* delete typemaps */
489 %typemap(in) (GValue *out_val, gboolean *val_found);
490 %typemap(argout) (GValue *out_val, gboolean *val_found);
492 /* We cheat a little bit here and just pass the native Perl type in to
493 * the function. This is the easiest way to make sure we know the property
494 * information (in particular, its type) before trying to convert the SV. */
495 %typemap(in) SV *sv "$1 = $input;"
498 property_set(DevicePropertyBase *pbase, SV *sv) {
503 memset(&gval, 0, sizeof(gval));
504 g_value_init(&gval, pbase->type);
505 if (!set_gvalue_from_sv(sv, &gval))
508 if (!device_property_set(self, pbase->ID, &gval))
511 g_value_unset(&gval);
514 g_value_unset(&gval);
520 property_set_ex(DevicePropertyBase *pbase, SV *sv,
521 PropertySurety surety, PropertySource source) {
523 memset(&gval, 0, sizeof(gval));
524 g_value_init(&gval, pbase->type);
525 if (!set_gvalue_from_sv(sv, &gval))
528 if (!device_property_set_ex(self, pbase->ID, &gval, surety, source))
531 g_value_unset(&gval);
534 g_value_unset(&gval);
538 gboolean recycle_file(guint filenum) {
539 return device_recycle_file(self, filenum);
542 /* accessor functions */
544 int file(void) { return self->file; }
545 guint64 block(void) { return self->block; }
546 gboolean in_file(void) { return self->in_file; }
547 char * device_name(void) { return self->device_name; }
548 DeviceAccessMode access_mode(void) { return self->access_mode; }
549 gboolean is_eof(void) { return self->is_eof; }
550 gboolean is_eom(void) { return self->is_eom; }
551 char * volume_label(void) { return self->volume_label; }
552 char * volume_time(void) { return self->volume_time; }
553 DeviceStatusFlags status(void) { return self->status; }
554 gsize min_block_size(void) { return self->min_block_size; }
555 gsize max_block_size(void) { return self->max_block_size; }
556 gsize block_size(void) { return self->block_size; }
557 gsize header_block_size(void) { return self->header_block_size; }
558 dumpfile_t *volume_header(void) { return self->volume_header; }
563 /* An alternate constructor for RAIT devices */
564 %typemap(in) GSList *child_devices {
568 if (!SvROK($input) || SvTYPE(SvRV($input)) != SVt_PVAV) {
569 SWIG_exception(SWIG_TypeError, "Expected an arrayref");
571 av = (AV *)SvRV($input);
575 for (i = 0; i <= len; i++) {
576 SV **elt = av_fetch(av, i, 0);
579 if (elt && !SvOK(*elt)) {
580 $1 = g_slist_append($1, NULL); /* 'undef' => NULL */
581 } else if (!elt || SWIG_ConvertPtr(*elt, (void **)&d, $descriptor(Device *), 0) == -1) {
582 SWIG_exception(SWIG_TypeError, "array member is not a Device");
584 $1 = g_slist_append($1, d);
588 %typemap(freearg) GSList *child_devices {
591 %newobject rait_device_open_from_children;
592 Device *rait_device_open_from_children(GSList *child_devices);
594 sub new_rait_from_children {
595 my $class = shift; # strip the $class from the arguments
596 return rait_device_open_from_children([@_]);
601 * Utilities for installchecks (not described in POD)
606 /* write LENGTH bytes of random data to FILENAME, seeded with SEED */
608 write_random_to_device(guint32 seed, size_t length, Device *device) {
609 simpleprng_state_t prng;
611 gsize block_size = device->block_size;
612 g_assert(block_size < G_MAXUINT);
614 buf = g_malloc(block_size);
615 simpleprng_seed(&prng, seed);
618 size_t to_write = min(block_size, length);
620 simpleprng_fill_buffer(&prng, buf, to_write);
621 if (!device_write_block(device, (guint)block_size, buf)) {
632 /* read LENGTH bytes of random data from FILENAME verifying it against
633 * a PRNG seeded with SEED. Sends any error messages to stderr.
636 verify_random_from_device(guint32 seed, size_t length, Device *device) {
637 simpleprng_state_t prng;
638 char *buf = NULL; /* first device_read_block will get the size */
641 simpleprng_seed(&prng, seed);
645 int size = block_size;
647 bytes_read = device_read_block(device, buf, &size);
648 if (bytes_read == 0 && size > block_size) {
651 buf = g_malloc(block_size);
654 if (bytes_read == -1) {
655 if (device->status == DEVICE_STATUS_SUCCESS) {
656 g_assert(device->is_eof);
657 g_debug("verify_random_from_device got unexpected EOF");
663 bytes_read = min(bytes_read, length);
665 if (!simpleprng_verify_buffer(&prng, buf, bytes_read))
668 length -= bytes_read;
684 amglue_add_flag_tag_fns(DeviceAccessMode);
685 amglue_add_constant_short(ACCESS_NULL, "NULL", DeviceAccessMode);
686 amglue_add_constant_short(ACCESS_READ, "READ", DeviceAccessMode);
687 amglue_add_constant_short(ACCESS_WRITE, "WRITE", DeviceAccessMode);
688 amglue_add_constant_short(ACCESS_APPEND, "APPEND", DeviceAccessMode);
690 /* (this is really a macro, but SWIG will Do The Right Thing */
691 gboolean IS_WRITABLE_ACCESS_MODE(DeviceAccessMode mode);
692 amglue_export_tag(DeviceAccessMode, IS_WRITABLE_ACCESS_MODE);
693 amglue_copy_to_tag(DeviceAccessMode, constants);
695 amglue_add_flag_tag_fns(DeviceStatusFlags);
696 amglue_add_constant_short(DEVICE_STATUS_SUCCESS, "SUCCESS", DeviceStatusFlags);
697 amglue_add_constant_short(DEVICE_STATUS_DEVICE_ERROR, "DEVICE_ERROR", DeviceStatusFlags);
698 amglue_add_constant_short(DEVICE_STATUS_DEVICE_BUSY, "DEVICE_BUSY", DeviceStatusFlags);
699 amglue_add_constant_short(DEVICE_STATUS_VOLUME_MISSING, "VOLUME_MISSING", DeviceStatusFlags);
700 amglue_add_constant_short(DEVICE_STATUS_VOLUME_UNLABELED, "VOLUME_UNLABELED", DeviceStatusFlags);
701 amglue_add_constant_short(DEVICE_STATUS_VOLUME_ERROR, "VOLUME_ERROR", DeviceStatusFlags);
702 amglue_add_constant_noshort(DEVICE_STATUS_FLAGS_MAX, DeviceStatusFlags);
703 amglue_copy_to_tag(DeviceStatusFlags, constants);
705 amglue_add_flag_tag_fns(PropertyPhaseFlags);
706 amglue_add_constant_short(PROPERTY_PHASE_BEFORE_START, "BEFORE_START", PropertyPhaseFlags);
707 amglue_add_constant_short(PROPERTY_PHASE_BETWEEN_FILE_WRITE, "BETWEEN_FILE_WRITE", PropertyPhaseFlags);
708 amglue_add_constant_short(PROPERTY_PHASE_INSIDE_FILE_WRITE, "INSIDE_FILE_WRITE", PropertyPhaseFlags);
709 amglue_add_constant_short(PROPERTY_PHASE_BETWEEN_FILE_READ, "BETWEEN_FILE_READ", PropertyPhaseFlags);
710 amglue_add_constant_short(PROPERTY_PHASE_INSIDE_FILE_READ, "INSIDE_FILE_READ", PropertyPhaseFlags);
711 amglue_add_constant_noshort(PROPERTY_PHASE_MAX, PropertyPhaseFlags);
712 amglue_add_constant_noshort(PROPERTY_PHASE_MASK, PropertyPhaseFlags);
713 amglue_add_constant_noshort(PROPERTY_PHASE_SHIFT, PropertyPhaseFlags);
714 amglue_copy_to_tag(PropertyPhaseFlags, constants);
716 amglue_add_flag_tag_fns(PropertyAccessFlags);
717 amglue_add_constant_short(PROPERTY_ACCESS_GET_BEFORE_START,
718 "GET_BEFORE_START", PropertyAccessFlags);
719 amglue_add_constant_short(PROPERTY_ACCESS_GET_BETWEEN_FILE_WRITE,
720 "GET_BETWEEN_FILE_WRITE", PropertyAccessFlags);
721 amglue_add_constant_short(PROPERTY_ACCESS_GET_INSIDE_FILE_WRITE,
722 "GET_INSIDE_FILE_WRITE", PropertyAccessFlags);
723 amglue_add_constant_short(PROPERTY_ACCESS_GET_BETWEEN_FILE_READ,
724 "GET_BETWEEN_FILE_READ", PropertyAccessFlags);
725 amglue_add_constant_short(PROPERTY_ACCESS_GET_INSIDE_FILE_READ,
726 "GET_INSIDE_FILE_READ", PropertyAccessFlags);
727 amglue_add_constant_short(PROPERTY_ACCESS_SET_BEFORE_START,
728 "SET_BEFORE_START", PropertyAccessFlags);
729 amglue_add_constant_short(PROPERTY_ACCESS_SET_BETWEEN_FILE_WRITE,
730 "SET_BETWEEN_FILE_WRITE", PropertyAccessFlags);
731 amglue_add_constant_short(PROPERTY_ACCESS_SET_INSIDE_FILE_WRITE,
732 "SET_INSIDE_FILE_WRITE", PropertyAccessFlags);
733 amglue_add_constant_short(PROPERTY_ACCESS_SET_BETWEEN_FILE_READ,
734 "SET_BETWEEN_FILE_READ", PropertyAccessFlags);
735 amglue_add_constant_short(PROPERTY_ACCESS_SET_INSIDE_FILE_READ,
736 "SET_INSIDE_FILE_READ", PropertyAccessFlags);
737 amglue_add_constant_noshort(PROPERTY_ACCESS_GET_MASK, PropertyAccessFlags);
738 amglue_add_constant_noshort(PROPERTY_ACCESS_SET_MASK, PropertyAccessFlags);
739 amglue_copy_to_tag(PropertyAccessFlags, constants);
741 amglue_add_enum_tag_fns(ConcurrencyParadigm);
742 amglue_add_constant_short(CONCURRENCY_PARADIGM_EXCLUSIVE, "EXCLUSIVE", ConcurrencyParadigm);
743 amglue_add_constant_short(CONCURRENCY_PARADIGM_SHARED_READ, "SHARED_READ", ConcurrencyParadigm);
744 amglue_add_constant_short(CONCURRENCY_PARADIGM_RANDOM_ACCESS, "RANDOM_ACCESS", ConcurrencyParadigm);
745 amglue_copy_to_tag(ConcurrencyParadigm, constants);
747 amglue_add_enum_tag_fns(StreamingRequirement);
748 amglue_add_constant_short(STREAMING_REQUIREMENT_NONE, "NONE", StreamingRequirement);
749 amglue_add_constant_short(STREAMING_REQUIREMENT_DESIRED, "DESIRED", StreamingRequirement);
750 amglue_add_constant_short(STREAMING_REQUIREMENT_REQUIRED, "REQUIRED", StreamingRequirement);
751 amglue_copy_to_tag(StreamingRequirement, constants);
753 amglue_add_enum_tag_fns(MediaAccessMode);
754 amglue_add_constant_short(MEDIA_ACCESS_MODE_READ_ONLY, "READ_ONLY", MediaAccessMode);
755 amglue_add_constant_short(MEDIA_ACCESS_MODE_WORM, "WORM", MediaAccessMode);
756 amglue_add_constant_short(MEDIA_ACCESS_MODE_READ_WRITE, "READ_WRITE", MediaAccessMode);
757 amglue_add_constant_short(MEDIA_ACCESS_MODE_WRITE_ONLY, "WRITE_ONLY", MediaAccessMode);
758 amglue_copy_to_tag(MediaAccessMode, constants);
760 amglue_add_flag_tag_fns(PropertySurety);
761 amglue_add_constant_short(PROPERTY_SURETY_BAD, "SURETY_BAD", PropertySurety);
762 amglue_add_constant_short(PROPERTY_SURETY_GOOD, "SURETY_GOOD", PropertySurety);
763 amglue_copy_to_tag(PropertySurety, constants);
765 amglue_add_flag_tag_fns(PropertySource);
766 amglue_add_constant_short(PROPERTY_SOURCE_DEFAULT, "SOURCE_DEFAULT", PropertySource);
767 amglue_add_constant_short(PROPERTY_SOURCE_DETECTED, "SOURCE_DETECTED", PropertySource);
768 amglue_add_constant_short(PROPERTY_SOURCE_USER, "SOURCE_USER", PropertySource);
769 amglue_copy_to_tag(PropertySource, constants);
773 # SWIG produces a sub-package for the Device "class", in this case named
774 # Amanda::Device::Device. For user convenience, we allow Amanda::Device->new(..) to
775 # do the same thing. This is a wrapper function, and not just a typeglob assignment,
776 # because we want to get the right blessing.
779 Amanda::Device::Device->new(@_);