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"
37 /* Initialize the Device API on load */
43 /* Utility functions for typemaps, below */
45 /* return a new, mortal SV corresponding to the given GValue
47 * @param value: the value to convert
48 * @returns: a new, mortal SV
51 set_sv_from_gvalue(GValue *value)
53 GType fundamental = G_TYPE_FUNDAMENTAL(G_VALUE_TYPE(value));
56 /* complex reference types */
57 switch (fundamental) {
59 return sv_2mortal(amglue_newSVi64(g_value_get_long(value)));
62 return sv_2mortal(amglue_newSVu64(g_value_get_ulong(value)));
65 return sv_2mortal(amglue_newSVi64(g_value_get_int64(value)));
68 return sv_2mortal(amglue_newSVu64(g_value_get_uint64(value)));
71 /* simple types that can be constructed with sv_set*v */
73 switch (fundamental) {
75 sv_setiv(sv, g_value_get_char(value));
79 sv_setuv(sv, g_value_get_uchar(value));
83 sv_setiv(sv, g_value_get_boolean(value));
87 sv_setiv(sv, g_value_get_int(value));
91 sv_setuv(sv, g_value_get_uint(value));
95 sv_setnv(sv, g_value_get_float(value));
99 sv_setnv(sv, g_value_get_double(value));
103 sv_setpv(sv, g_value_get_string(value));
107 sv_setiv(sv, g_value_get_enum(value));
111 sv_setiv(sv, g_value_get_flags(value));
117 case G_TYPE_INTERFACE:
120 warn("Unsupported fundamental property type #%d", (int)fundamental);
121 sv_setsv(sv, &PL_sv_undef);
128 /* Given an SV and an initialized GValue, set the GValue to the value
129 * represented by the SV. The GValue's type must already be set.
131 * For basic corresponding types (string -> string, integer -> integer),
132 * the translation is straightforward. However, if the GValue is not a
133 * string, but the SV has a string value, then g_value_set_from_string will
134 * be used to parse the string.
136 * @param sv: SV to convert
137 * @param value: (input/output) destination
138 * @returns: TRUE on success
141 set_gvalue_from_sv(SV *sv, GValue *value)
143 GType fundamental = G_TYPE_FUNDAMENTAL(G_VALUE_TYPE(value));
145 /* if we got a string, use g_value_set_from_string to parse any funny
146 * values or suffixes */
148 if (g_value_set_from_string(value, SvPV_nolen(sv)))
152 /* otherwise, handle numeric types with SvIV, SvNV, or the amglue_* functions */
153 switch (fundamental) {
155 g_value_set_boolean(value, SvIV(sv));
159 g_value_set_char(value, amglue_SvI8(sv));
163 g_value_set_uchar(value, amglue_SvU8(sv));
167 g_value_set_int(value, amglue_SvI32(sv));
171 g_value_set_uint(value, amglue_SvU32(sv));
175 g_value_set_int64(value, amglue_SvI64(sv));
179 g_value_set_uint64(value, amglue_SvU64(sv));
183 g_value_set_int64(value, amglue_SvI64(sv));
187 g_value_set_uint64(value, amglue_SvU64(sv));
191 g_value_set_float(value, SvNV(sv));
195 g_value_set_double(value, SvNV(sv));
199 g_value_set_enum(value, SvIV(sv));
203 g_value_set_flags(value, SvIV(sv));
207 /* for anything else, let perl stringify it for us and try parsing it */
208 return g_value_set_from_string(value, SvPV_nolen(sv));
215 * DirectTCPConnection object
218 typedef struct DirectTCPConnection {
220 ~DirectTCPConnection() {
221 g_object_unref(self);
226 return directtcp_connection_close(self);
229 } DirectTCPConnection;
232 * Device struct, %extend-ed into a Perl class
235 %name(unaliased_name) extern char *device_unaliased_name(char *);
237 typedef struct Device {
242 Device(char *device_name) {
243 return device_open(device_name);
247 g_object_unref(self);
251 configure(gboolean use_global_config) {
252 return device_configure(self, use_global_config);
257 return device_error(self);
262 return device_status_error(self);
267 return device_error_or_status(self);
272 return device_read_label(self);
276 start(DeviceAccessMode mode, char *label, char *timestamp) {
277 return device_start(self, mode, label, timestamp);
282 return device_finish(self);
286 start_file(dumpfile_t *jobInfo) {
287 return device_start_file(self, jobInfo);
291 write_block(guint size, gpointer data) {
292 return device_write_block(self, size, data);
297 return device_finish_file(self);
301 seek_file(guint file) {
302 return device_seek_file(self, file);
306 seek_block(guint64 block) {
307 return device_seek_block(self, block);
311 read_block(gpointer buffer, int *size) {
312 return device_read_block(self, buffer, size);
317 return device_erase(self);
322 return device_eject(self);
326 directtcp_supported() {
327 return device_directtcp_supported(self);
331 listen(gboolean for_writing, DirectTCPAddr **addrs) {
332 /* ensure that the addresses are empty if there was an error */
333 if (!device_listen(self, for_writing, addrs))
337 %newobject accept; /* connection is already ref'd, so we own it */
338 DirectTCPConnection *
340 DirectTCPConnection *conn = NULL;
343 rv = device_accept(self, &conn, NULL, NULL);
345 /* conn is ref'd for our convenience, but we don't want it */
346 g_object_unref(conn);
352 %newobject connect; /* connection is already ref'd, so we own it */
353 DirectTCPConnection *
354 connect(gboolean for_writing, DirectTCPAddr *addrs) {
355 DirectTCPConnection *conn = NULL;
358 rv = device_connect(self, for_writing, addrs, &conn, NULL, NULL);
360 /* conn is ref'd for our convenience, but we don't want it */
361 g_object_unref(conn);
368 use_connection(DirectTCPConnection *conn) {
369 return device_use_connection(self, conn);
372 %typemap(in,numinputs=0) guint64 *actual_size (guint64 sz) {
376 %typemap(argout) guint64 *actual_size {
377 SP += argvi; PUTBACK;
378 $result = sv_2mortal(amglue_newSVu64(*$1));
379 SPAGAIN; SP -= argvi; argvi++;
382 write_from_connection(guint64 size, guint64 *actual_size) {
383 return device_write_from_connection(self, size, actual_size);
387 read_to_connection(guint64 size, guint64 *actual_size) {
388 return device_read_to_connection(self, size, actual_size);
391 %typemap(out) const GSList * {
394 /* Count the DeviceProperties */
395 EXTEND(SP, g_slist_length($1)); /* make room for return values */
397 /* Note that we set $result several times. the nature of
398 * SWIG's wrapping is such that incrementing argvi points
399 * $result to the next location in perl's argument stack.
402 for (iter = $1; iter; iter = g_slist_next(iter)) {
403 DeviceProperty *prop = iter->data;
405 SV *rv = newRV_noinc((SV *)hash);
407 hv_store(hash, "name", 4,
408 newSVpv(prop->base->name, 0), 0);
409 hv_store(hash, "description", 11,
410 newSVpv(prop->base->description, 0), 0);
411 hv_store(hash, "access", 6,
412 newSViv(prop->access), 0);
413 $result = sv_2mortal(rv);
417 const GSList * property_list(void) {
418 return device_property_get_list(self);
421 %typemap(out) const GSList *; /* remove typemap */
423 /* A typemap to convert a property name to a DevicePropertyBase. */
424 %typemap(in) DevicePropertyBase * {
428 pname = SvPV_nolen($input);
431 $1 = (DevicePropertyBase *)device_property_get_by_name(pname);
436 /* A typemap to convert the GValue in property_get to a return value. The
437 * (in) typemap sets up storage for the parameters, while the (argout) converts
438 * them to a perl SV. */
439 %typemap(in,numinputs=0) (GValue *out_val, PropertySurety *surety,
440 PropertySource *source, gboolean *val_found)
442 PropertySurety surety,
443 PropertySource source,
445 memset(&val, 0, sizeof(val));
447 if (GIMME_V == G_ARRAY) {
453 %typemap(argout) (GValue *out_val, PropertySurety *surety,
454 PropertySource *source, gboolean *val_found) {
455 /* if the result is valid */
457 /* move data from $1 to $result, somehow, being careful to
458 * save the perl stack while doing so */
459 SP += argvi; PUTBACK;
460 $result = set_sv_from_gvalue($1);
461 SPAGAIN; SP -= argvi; argvi++;
463 /* free any memory for the GValue */
466 if (GIMME_V == G_ARRAY) {
467 $result = newSViv(*$2);
469 $result = newSViv(*$3);
473 /* otherwise, return nothing */
477 property_get(DevicePropertyBase *pbase, GValue *out_val, PropertySurety *surety,
478 PropertySource *source, gboolean *val_found) {
480 *val_found = device_property_get_ex(self, pbase->ID, out_val, surety, source);
486 /* delete typemaps */
487 %typemap(in) (GValue *out_val, gboolean *val_found);
488 %typemap(argout) (GValue *out_val, gboolean *val_found);
490 /* We cheat a little bit here and just pass the native Perl type in to
491 * the function. This is the easiest way to make sure we know the property
492 * information (in particular, its type) before trying to convert the SV. */
493 %typemap(in) SV *sv "$1 = $input;"
496 property_set(DevicePropertyBase *pbase, SV *sv) {
498 memset(&gval, 0, sizeof(gval));
499 g_value_init(&gval, pbase->type);
500 if (!set_gvalue_from_sv(sv, &gval))
503 if (!device_property_set(self, pbase->ID, &gval))
506 g_value_unset(&gval);
509 g_value_unset(&gval);
514 property_set_ex(DevicePropertyBase *pbase, SV *sv,
515 PropertySurety surety, PropertySource source) {
517 memset(&gval, 0, sizeof(gval));
518 g_value_init(&gval, pbase->type);
519 if (!set_gvalue_from_sv(sv, &gval))
522 if (!device_property_set_ex(self, pbase->ID, &gval, surety, source))
525 g_value_unset(&gval);
528 g_value_unset(&gval);
532 gboolean recycle_file(guint filenum) {
533 return device_recycle_file(self, filenum);
536 /* accessor functions */
538 int file(void) { return self->file; }
539 guint64 block(void) { return self->block; }
540 gboolean in_file(void) { return self->in_file; }
541 char * device_name(void) { return self->device_name; }
542 DeviceAccessMode access_mode(void) { return self->access_mode; }
543 gboolean is_eof(void) { return self->is_eof; }
544 gboolean is_eom(void) { return self->is_eom; }
545 char * volume_label(void) { return self->volume_label; }
546 char * volume_time(void) { return self->volume_time; }
547 DeviceStatusFlags status(void) { return self->status; }
548 gsize min_block_size(void) { return self->min_block_size; }
549 gsize max_block_size(void) { return self->max_block_size; }
550 gsize block_size(void) { return self->block_size; }
551 dumpfile_t *volume_header(void) { return self->volume_header; }
556 /* An alternate constructor for RAIT devices */
557 %typemap(in) GSList *child_devices {
561 if (!SvROK($input) || SvTYPE(SvRV($input)) != SVt_PVAV) {
562 SWIG_exception(SWIG_TypeError, "Expected an arrayref");
564 av = (AV *)SvRV($input);
568 for (i = 0; i <= len; i++) {
569 SV **elt = av_fetch(av, i, 0);
572 if (elt && !SvOK(*elt)) {
573 $1 = g_slist_append($1, NULL); /* 'undef' => NULL */
574 } else if (!elt || SWIG_ConvertPtr(*elt, (void **)&d, $descriptor(Device *), 0) == -1) {
575 SWIG_exception(SWIG_TypeError, "array member is not a Device");
577 $1 = g_slist_append($1, d);
581 %typemap(freearg) GSList *child_devices {
584 %newobject rait_device_open_from_children;
585 Device *rait_device_open_from_children(GSList *child_devices);
587 sub new_rait_from_children {
588 my $class = shift; # strip the $class from the arguments
589 return rait_device_open_from_children([@_]);
594 * Utilities for installchecks (not described in POD)
599 /* write LENGTH bytes of random data to FILENAME, seeded with SEED */
601 write_random_to_device(guint32 seed, size_t length, Device *device) {
602 simpleprng_state_t prng;
604 gsize block_size = device->block_size;
605 g_assert(block_size < G_MAXUINT);
607 buf = g_malloc(block_size);
608 simpleprng_seed(&prng, seed);
611 size_t to_write = min(block_size, length);
613 simpleprng_fill_buffer(&prng, buf, to_write);
614 if (!device_write_block(device, (guint)block_size, buf)) {
625 /* read LENGTH bytes of random data from FILENAME verifying it against
626 * a PRNG seeded with SEED. Sends any error messages to stderr.
629 verify_random_from_device(guint32 seed, size_t length, Device *device) {
630 simpleprng_state_t prng;
631 char *buf = NULL; /* first device_read_block will get the size */
634 simpleprng_seed(&prng, seed);
638 int size = block_size;
640 bytes_read = device_read_block(device, buf, &size);
641 if (bytes_read == 0 && size > block_size) {
644 buf = g_malloc(block_size);
647 if (bytes_read == -1) {
648 if (device->status == DEVICE_STATUS_SUCCESS) {
649 g_assert(device->is_eof);
650 g_debug("verify_random_from_device got unexpected EOF");
656 bytes_read = min(bytes_read, length);
658 if (!simpleprng_verify_buffer(&prng, buf, bytes_read))
661 length -= bytes_read;
677 amglue_add_flag_tag_fns(DeviceAccessMode);
678 amglue_add_constant_short(ACCESS_NULL, "NULL", DeviceAccessMode);
679 amglue_add_constant_short(ACCESS_READ, "READ", DeviceAccessMode);
680 amglue_add_constant_short(ACCESS_WRITE, "WRITE", DeviceAccessMode);
681 amglue_add_constant_short(ACCESS_APPEND, "APPEND", DeviceAccessMode);
683 /* (this is really a macro, but SWIG will Do The Right Thing */
684 gboolean IS_WRITABLE_ACCESS_MODE(DeviceAccessMode mode);
685 amglue_export_tag(DeviceAccessMode, IS_WRITABLE_ACCESS_MODE);
686 amglue_copy_to_tag(DeviceAccessMode, constants);
688 amglue_add_flag_tag_fns(DeviceStatusFlags);
689 amglue_add_constant_short(DEVICE_STATUS_SUCCESS, "SUCCESS", DeviceStatusFlags);
690 amglue_add_constant_short(DEVICE_STATUS_DEVICE_ERROR, "DEVICE_ERROR", DeviceStatusFlags);
691 amglue_add_constant_short(DEVICE_STATUS_DEVICE_BUSY, "DEVICE_BUSY", DeviceStatusFlags);
692 amglue_add_constant_short(DEVICE_STATUS_VOLUME_MISSING, "VOLUME_MISSING", DeviceStatusFlags);
693 amglue_add_constant_short(DEVICE_STATUS_VOLUME_UNLABELED, "VOLUME_UNLABELED", DeviceStatusFlags);
694 amglue_add_constant_short(DEVICE_STATUS_VOLUME_ERROR, "VOLUME_ERROR", DeviceStatusFlags);
695 amglue_add_constant_noshort(DEVICE_STATUS_FLAGS_MAX, DeviceStatusFlags);
696 amglue_copy_to_tag(DeviceStatusFlags, constants);
698 amglue_add_flag_tag_fns(PropertyPhaseFlags);
699 amglue_add_constant_short(PROPERTY_PHASE_BEFORE_START, "BEFORE_START", PropertyPhaseFlags);
700 amglue_add_constant_short(PROPERTY_PHASE_BETWEEN_FILE_WRITE, "BETWEEN_FILE_WRITE", PropertyPhaseFlags);
701 amglue_add_constant_short(PROPERTY_PHASE_INSIDE_FILE_WRITE, "INSIDE_FILE_WRITE", PropertyPhaseFlags);
702 amglue_add_constant_short(PROPERTY_PHASE_BETWEEN_FILE_READ, "BETWEEN_FILE_READ", PropertyPhaseFlags);
703 amglue_add_constant_short(PROPERTY_PHASE_INSIDE_FILE_READ, "INSIDE_FILE_READ", PropertyPhaseFlags);
704 amglue_add_constant_noshort(PROPERTY_PHASE_MAX, PropertyPhaseFlags);
705 amglue_add_constant_noshort(PROPERTY_PHASE_MASK, PropertyPhaseFlags);
706 amglue_add_constant_noshort(PROPERTY_PHASE_SHIFT, PropertyPhaseFlags);
707 amglue_copy_to_tag(PropertyPhaseFlags, constants);
709 amglue_add_flag_tag_fns(PropertyAccessFlags);
710 amglue_add_constant_short(PROPERTY_ACCESS_GET_BEFORE_START,
711 "GET_BEFORE_START", PropertyAccessFlags);
712 amglue_add_constant_short(PROPERTY_ACCESS_GET_BETWEEN_FILE_WRITE,
713 "GET_BETWEEN_FILE_WRITE", PropertyAccessFlags);
714 amglue_add_constant_short(PROPERTY_ACCESS_GET_INSIDE_FILE_WRITE,
715 "GET_INSIDE_FILE_WRITE", PropertyAccessFlags);
716 amglue_add_constant_short(PROPERTY_ACCESS_GET_BETWEEN_FILE_READ,
717 "GET_BETWEEN_FILE_READ", PropertyAccessFlags);
718 amglue_add_constant_short(PROPERTY_ACCESS_GET_INSIDE_FILE_READ,
719 "GET_INSIDE_FILE_READ", PropertyAccessFlags);
720 amglue_add_constant_short(PROPERTY_ACCESS_SET_BEFORE_START,
721 "SET_BEFORE_START", PropertyAccessFlags);
722 amglue_add_constant_short(PROPERTY_ACCESS_SET_BETWEEN_FILE_WRITE,
723 "SET_BETWEEN_FILE_WRITE", PropertyAccessFlags);
724 amglue_add_constant_short(PROPERTY_ACCESS_SET_INSIDE_FILE_WRITE,
725 "SET_INSIDE_FILE_WRITE", PropertyAccessFlags);
726 amglue_add_constant_short(PROPERTY_ACCESS_SET_BETWEEN_FILE_READ,
727 "SET_BETWEEN_FILE_READ", PropertyAccessFlags);
728 amglue_add_constant_short(PROPERTY_ACCESS_SET_INSIDE_FILE_READ,
729 "SET_INSIDE_FILE_READ", PropertyAccessFlags);
730 amglue_add_constant_noshort(PROPERTY_ACCESS_GET_MASK, PropertyAccessFlags);
731 amglue_add_constant_noshort(PROPERTY_ACCESS_SET_MASK, PropertyAccessFlags);
732 amglue_copy_to_tag(PropertyAccessFlags, constants);
734 amglue_add_enum_tag_fns(ConcurrencyParadigm);
735 amglue_add_constant_short(CONCURRENCY_PARADIGM_EXCLUSIVE, "EXCLUSIVE", ConcurrencyParadigm);
736 amglue_add_constant_short(CONCURRENCY_PARADIGM_SHARED_READ, "SHARED_READ", ConcurrencyParadigm);
737 amglue_add_constant_short(CONCURRENCY_PARADIGM_RANDOM_ACCESS, "RANDOM_ACCESS", ConcurrencyParadigm);
738 amglue_copy_to_tag(ConcurrencyParadigm, constants);
740 amglue_add_enum_tag_fns(StreamingRequirement);
741 amglue_add_constant_short(STREAMING_REQUIREMENT_NONE, "NONE", StreamingRequirement);
742 amglue_add_constant_short(STREAMING_REQUIREMENT_DESIRED, "DESIRED", StreamingRequirement);
743 amglue_add_constant_short(STREAMING_REQUIREMENT_REQUIRED, "REQUIRED", StreamingRequirement);
744 amglue_copy_to_tag(StreamingRequirement, constants);
746 amglue_add_enum_tag_fns(MediaAccessMode);
747 amglue_add_constant_short(MEDIA_ACCESS_MODE_READ_ONLY, "READ_ONLY", MediaAccessMode);
748 amglue_add_constant_short(MEDIA_ACCESS_MODE_WORM, "WORM", MediaAccessMode);
749 amglue_add_constant_short(MEDIA_ACCESS_MODE_READ_WRITE, "READ_WRITE", MediaAccessMode);
750 amglue_add_constant_short(MEDIA_ACCESS_MODE_WRITE_ONLY, "WRITE_ONLY", MediaAccessMode);
751 amglue_copy_to_tag(MediaAccessMode, constants);
753 amglue_add_flag_tag_fns(PropertySurety);
754 amglue_add_constant_short(PROPERTY_SURETY_BAD, "SURETY_BAD", PropertySurety);
755 amglue_add_constant_short(PROPERTY_SURETY_GOOD, "SURETY_GOOD", PropertySurety);
756 amglue_copy_to_tag(PropertySurety, constants);
758 amglue_add_flag_tag_fns(PropertySource);
759 amglue_add_constant_short(PROPERTY_SOURCE_DEFAULT, "SOURCE_DEFAULT", PropertySource);
760 amglue_add_constant_short(PROPERTY_SOURCE_DETECTED, "SOURCE_DETECTED", PropertySource);
761 amglue_add_constant_short(PROPERTY_SOURCE_USER, "SOURCE_USER", PropertySource);
762 amglue_copy_to_tag(PropertySource, constants);
766 # SWIG produces a sub-package for the Device "class", in this case named
767 # Amanda::Device::Device. For user convenience, we allow Amanda::Device->new(..) to
768 # do the same thing. This is a wrapper function, and not just a typeglob assignment,
769 # because we want to get the right blessing.
772 Amanda::Device::Device->new(@_);