Debian Codegraph @

device-src/device.c

raw
1 /*
2 * Copyright (c) 2007-2012 Zmanda, Inc. All Rights Reserved.
3 * Copyright (c) 2013-2016 Carbonite, Inc. All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * Contact information: Carbonite Inc., 756 N Pastoria Ave
20 * Sunnyvale, CA 94085, or: http://www.zmanda.com
21 */
22
23 /* The Device API abstracts device workings, interaction, properties, and
24 * capabilities from the rest of the Amanda code base. It supports
25 * pluggable modules for different kinds of devices. */
26
27 #include "amanda.h"
28 #include "conffile.h"
29
30 #include <regex.h>
31 #include <dlfcn.h>
32
33 #include "device.h"
34 #include "property.h"
35
36 #include "timestamp.h"
37 #include "amutil.h"
38
39 /*
40 * Prototypes for subclass registration functions
41 */
42
43 void null_device_register (void);
44 void rait_device_register (void);
45 #ifdef WANT_S3_DEVICE
46 void s3_device_register (void);
47 #endif
48 #ifdef WANT_TAPE_DEVICE
49 void tape_device_register (void);
50 #endif
51 void vfs_device_register (void);
52 void diskflat_device_register (void);
53 #ifdef WANT_DVDRW_DEVICE
54 void dvdrw_device_register (void);
55 #endif
56 #ifdef WANT_NDMP_DEVICE
57 void ndmp_device_register (void);
58 #endif
59
60 /*
61 * Registration infrastructure
62 */
63
64 static GHashTable* driverList = NULL;
65
66 void device_api_init(void) {
67 glib_init();
68 device_property_init();
69 driverList = g_hash_table_new(g_str_hash, g_str_equal);
70
71 device_status_flags_get_type(); /* to register it */
72 device_get_type(); /* to register it */
73
74 /* register other types and devices. */
75 null_device_register();
76 vfs_device_register();
77 diskflat_device_register();
78 #ifdef WANT_TAPE_DEVICE
79 tape_device_register();
80 #endif
81 rait_device_register();
82 #ifdef WANT_S3_DEVICE
83 s3_device_register();
84 #endif
85 #ifdef WANT_DVDRW_DEVICE
86 dvdrw_device_register();
87 #endif
88 #ifdef WANT_NDMP_DEVICE
89 ndmp_device_register();
90 #endif
91 }
92
93 void
94 register_device(
95 DeviceFactory factory,
96 const char ** device_prefix_list)
97 {
98 char ** tmp;
99
100 g_assert(driverList != NULL);
101 g_assert(factory != NULL);
102 g_return_if_fail(device_prefix_list != NULL);
103 g_return_if_fail(*device_prefix_list != NULL);
104
105 tmp = (char**)device_prefix_list;
106 while (*tmp != NULL) {
107 g_hash_table_insert(driverList, *tmp, (gpointer)factory);
108 tmp ++;
109 }
110 }
111
112 static DeviceFactory lookup_device_factory(const char *device_type) {
113 gpointer key, value;
114 char *libfilename;
115 char *fn_name;
116 char *error;
117 void *lib_handle;
118 void (*fn)(void);
119 char *dev_type;
120
121 g_assert(driverList != NULL);
122
123 if (g_hash_table_lookup_extended(driverList, device_type, &key, &value)) {
124 return (DeviceFactory)value;
125 }
126
127 // Try to load the library
128 dev_type = g_ascii_strdown(device_type, -1);
129 libfilename = g_strdup_printf("%s/libam%s.so", amlibdir, dev_type);
130
131 lib_handle = dlopen(libfilename, RTLD_LAZY);
132 g_free(libfilename);
133 if (!lib_handle) {
134 g_debug("%s", dlerror());
135 g_free(dev_type);
136 return NULL;
137 }
138
139 fn_name = g_strdup_printf("%s_device_register", dev_type);
140 dlerror();
141 g_free(dev_type);
142 fn = dlsym(lib_handle, fn_name);
143 g_free(fn_name);
144 if ((error = dlerror()) != NULL) {
145 g_debug("%s", error);
146 return NULL;
147 }
148
149 (*fn)();
150
151 if (g_hash_table_lookup_extended(driverList, device_type, &key, &value)) {
152 return (DeviceFactory)value;
153 }
154
155 return NULL;
156 }
157
158 static const GFlagsValue device_status_flags_values[] = {
159 { DEVICE_STATUS_SUCCESS,
160 "DEVICE_STATUS_SUCCESS",
161 "Success" },
162 { DEVICE_STATUS_DEVICE_ERROR,
163 "DEVICE_STATUS_DEVICE_ERROR",
164 "Device error" },
165 { DEVICE_STATUS_DEVICE_BUSY,
166 "DEVICE_STATUS_DEVICE_BUSY",
167 "Device busy" },
168 { DEVICE_STATUS_VOLUME_MISSING,
169 "DEVICE_STATUS_VOLUME_MISSING",
170 "Volume not found" },
171 { DEVICE_STATUS_VOLUME_UNLABELED,
172 "DEVICE_STATUS_VOLUME_UNLABELED",
173 "Volume not labeled" },
174 { DEVICE_STATUS_VOLUME_ERROR,
175 "DEVICE_STATUS_VOLUME_ERROR",
176 "Volume error" },
177 { 0, NULL, NULL }
178 };
179
180 GType device_status_flags_get_type(void) {
181 static GType type = 0;
182 if (G_UNLIKELY(type == 0)) {
183 type = g_flags_register_static("DeviceStatusFlags",
184 device_status_flags_values);
185 }
186 return type;
187 }
188
189 /* Device class definition starts here. */
190
191 struct DevicePrivate_s {
192 /* hash table mapping ID to SimpleProperty object */
193 GHashTable * simple_properties;
194
195 /* In writing mode, after a short block is written, no additional blocks
196 * are allowed the file is finished and a new file started. This is only
197 * used for assertions. */
198 gboolean wrote_short_block;
199
200 /* Holds an error message if the function returned an error. */
201 char * errmsg;
202
203 /* temporary holding place for device_status_error() */
204 char * statusmsg;
205 DeviceStatusFlags last_status;
206 };
207
208 /* This holds the default response to a particular property. */
209 typedef struct {
210 DeviceProperty *prop;
211 GValue response;
212 PropertySurety surety;
213 PropertySource source;
214 } SimpleProperty;
215
216 #define selfp (self->private)
217
218 /* here are local prototypes, so we can make function pointers. */
219 static void device_init (Device * o);
220 static void device_class_init (DeviceClass * c);
221 static void device_base_init (DeviceClass * c);
222
223 static void simple_property_free(SimpleProperty *o);
224
225 static void default_device_open_device(Device * self, char * device_name,
226 char * device_type, char * device_node);
227 static gboolean default_device_configure(Device *self, gboolean use_global_config);
228 static gboolean default_device_property_get_ex(Device * self, DevicePropertyId id,
229 GValue * val,
230 PropertySurety *surety,
231 PropertySource *source);
232 static char * default_device_property_set_ex(Device *self,
233 DevicePropertyId id,
234 GValue * val,
235 PropertySurety surety,
236 PropertySource source);
237 static void set_properties_from_global_config(Device * device);
238 static void set_properties_from_device_config(Device * device, device_config_t *dc);
239
240 static gboolean property_get_block_size_fn(Device *self,
241 DevicePropertyBase *base, GValue *val,
242 PropertySurety *surety, PropertySource *source);
243
244 static gboolean property_set_block_size_fn(Device *self,
245 DevicePropertyBase *base, GValue *val,
246 PropertySurety surety, PropertySource source);
247
248 static gboolean property_get_min_block_size_fn(Device *self,
249 DevicePropertyBase *base, GValue *val,
250 PropertySurety *surety, PropertySource *source);
251
252 static gboolean property_get_max_block_size_fn(Device *self,
253 DevicePropertyBase *base, GValue *val,
254 PropertySurety *surety, PropertySource *source);
255
256 static gboolean property_get_canonical_name_fn(Device *self,
257 DevicePropertyBase *base, GValue *val,
258 PropertySurety *surety, PropertySource *source);
259
260 /* pointer to the class of our parent */
261 static GObjectClass *parent_class = NULL;
262
263 GType
264 device_get_type (void)
265 {
266 static GType type = 0;
267
268 if (G_UNLIKELY(type == 0)) {
269 static const GTypeInfo info = {
270 sizeof (DeviceClass),
271 (GBaseInitFunc) device_base_init,
272 (GBaseFinalizeFunc) NULL,
273 (GClassInitFunc) device_class_init,
274 (GClassFinalizeFunc) NULL,
275 NULL /* class_data */,
276 sizeof (Device),
277 0 /* n_preallocs */,
278 (GInstanceInitFunc) device_init,
279 NULL
280 };
281
282 type = g_type_register_static (G_TYPE_OBJECT, "Device", &info,
283 (GTypeFlags)G_TYPE_FLAG_ABSTRACT);
284 }
285
286 return type;
287 }
288
289 static void device_finalize(GObject *obj_self) {
290 Device *self G_GNUC_UNUSED = DEVICE (obj_self);
291 if(G_OBJECT_CLASS(parent_class)->finalize)
292 (* G_OBJECT_CLASS(parent_class)->finalize)(obj_self);
293
294 /* Here we call device_finish() if it hasn't been done
295 yet. Subclasses may need to do this same check earlier. */
296 if (self->access_mode != ACCESS_NULL) {
297 device_finish(self);
298 }
299
300 amfree(self->device_name);
301 amfree(self->volume_label);
302 amfree(self->volume_time);
303 amfree(self->volume_header);
304 if (self->device_mutex) {
305 g_mutex_free(self->device_mutex);
306 self->device_mutex = NULL;
307 }
308 amfree(selfp->errmsg);
309 amfree(selfp->statusmsg);
310 g_hash_table_destroy(selfp->simple_properties);
311 amfree(self->private);
312 }
313
314 static void
315 device_init (Device * self)
316 {
317 self->private = malloc(sizeof(DevicePrivate));
318 self->device_name = NULL;
319 self->access_mode = ACCESS_NULL;
320 self->is_eof = FALSE;
321 self->is_eom = FALSE;
322 self->file = -1;
323 self->block = 0;
324 self->in_file = FALSE;
325 self->volume_label = NULL;
326 self->volume_time = NULL;
327 self->status = DEVICE_STATUS_SUCCESS;
328 self->min_block_size = 1;
329 self->max_block_size = SIZE_MAX; /* subclasses *really* should choose something smaller */
330 self->block_size = DISK_BLOCK_BYTES;
331 self->allow_take_scribe_from = TRUE;
332 selfp->errmsg = NULL;
333 selfp->statusmsg = NULL;
334 selfp->last_status = 0;
335 selfp->simple_properties =
336 g_hash_table_new_full(g_direct_hash,
337 g_direct_equal,
338 NULL,
339 (GDestroyNotify) simple_property_free);
340 }
341
342 static void
343 device_class_init (DeviceClass * device_class)
344 {
345 GObjectClass *g_object_class = (GObjectClass*) device_class;
346
347 parent_class = g_type_class_ref (G_TYPE_OBJECT);
348
349 device_class->directtcp_supported = FALSE;
350
351 device_class->open_device = default_device_open_device;
352 device_class->configure = default_device_configure;
353 device_class->property_get_ex = default_device_property_get_ex;
354 device_class->property_set_ex = default_device_property_set_ex;
355 g_object_class->finalize = device_finalize;
356 }
357
358 static void
359 device_base_init (DeviceClass * device_class)
360 {
361 /* The base_init function is called once each time a child class is
362 * created, before the class_init functions (even our own) are called. */
363
364 device_class->class_properties = g_array_new(FALSE, TRUE, sizeof(DeviceProperty));
365 device_class->class_properties_list = NULL;
366
367 device_class_register_property(device_class, PROPERTY_BLOCK_SIZE,
368 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
369 property_get_block_size_fn,
370 property_set_block_size_fn);
371
372 device_class_register_property(device_class, PROPERTY_MIN_BLOCK_SIZE,
373 PROPERTY_ACCESS_GET_MASK,
374 property_get_min_block_size_fn,
375 NULL);
376
377 device_class_register_property(device_class, PROPERTY_MAX_BLOCK_SIZE,
378 PROPERTY_ACCESS_GET_MASK,
379 property_get_max_block_size_fn,
380 NULL);
381
382 device_class_register_property(device_class, PROPERTY_CANONICAL_NAME,
383 PROPERTY_ACCESS_GET_MASK,
384 property_get_canonical_name_fn,
385 NULL);
386
387 device_class_register_property(device_class, PROPERTY_CONCURRENCY,
388 PROPERTY_ACCESS_GET_MASK,
389 device_simple_property_get_fn,
390 device_simple_property_set_fn);
391
392 device_class_register_property(device_class, PROPERTY_STREAMING,
393 PROPERTY_ACCESS_GET_MASK,
394 device_simple_property_get_fn,
395 device_simple_property_set_fn);
396
397 device_class_register_property(device_class, PROPERTY_APPENDABLE,
398 PROPERTY_ACCESS_GET_MASK,
399 device_simple_property_get_fn,
400 device_simple_property_set_fn);
401
402 device_class_register_property(device_class, PROPERTY_PARTIAL_DELETION,
403 PROPERTY_ACCESS_GET_MASK,
404 device_simple_property_get_fn,
405 device_simple_property_set_fn);
406
407 device_class_register_property(device_class, PROPERTY_FULL_DELETION,
408 PROPERTY_ACCESS_GET_MASK,
409 device_simple_property_get_fn,
410 device_simple_property_set_fn);
411
412 device_class_register_property(device_class, PROPERTY_MEDIUM_ACCESS_TYPE,
413 PROPERTY_ACCESS_GET_MASK,
414 device_simple_property_get_fn,
415 device_simple_property_set_fn);
416
417 device_class_register_property(device_class, PROPERTY_COMMENT,
418 PROPERTY_ACCESS_GET_MASK|PROPERTY_ACCESS_SET_MASK,
419 device_simple_property_get_fn,
420 device_simple_property_set_fn);
421
422 device_class_register_property(device_class, PROPERTY_LEOM,
423 PROPERTY_ACCESS_GET_MASK,
424 device_simple_property_get_fn,
425 device_simple_property_set_fn);
426 }
427
428 static void simple_property_free(SimpleProperty * resp) {
429 g_value_unset(&(resp->response));
430 amfree(resp);
431 }
432
433 /*
434 * A full device name (the user_name argument here) must match (case
435 * insensitive) ^([a-z0-9]+):(.*), with the device driver being $1 and device
436 * name being $2. However, for legacy reasons, ^[^:](.*) is also accepted, in
437 * which case the device driver is "tape" and the device name is $1.
438 *
439 * As this is pretty much a split over a single colon, we use g_strsplit() to
440 * split the string against ":", with a maximum of two tokens. This function
441 * returns a dynamicaly allocated gchar **. Given r0 the first array member and
442 * r1 the second, the results are as such, according to the input string:
443 *
444 * INPUT: "x:y"; OUTPUT: r0 -> "x", r1 -> "y"
445 * INPUT: "x"; OUTPUT: r0 -> "x", r1 -> NULL
446 * INPUT: "x:"; OUTPUT: r0 -> "x", r1 -> ""
447 * INPUT: ":y"; OUTPUT: r0 -> "", r1 -> "y"
448 * INPUT: ":"; OUTPUT: r0 -> "", r1 -> ""
449 *
450 * This leads to the following validation algorithm (note that r0 can never be
451 * NULL):
452 *
453 * - if r0 is the empty string, device name is invalid;
454 * - if r1 is NULL, the device name is invalid if we don't WANT_TAPE_DEVICE,
455 * otherwise it is "tape" as the driver and r0 as the device;
456 * - otherwise, r0 is the device type and r1 the device name.
457 */
458
459 static gboolean
460 validate_device_name(const char *user_name, char **driver_name, char **device)
461 {
462 gboolean ret = FALSE;
463 gchar **split_result = g_strsplit(user_name, ":", 2);
464 gchar *split_driver_name = split_result[0], *split_device = split_result[1];
465
466 if (!*split_driver_name)
467 goto out;
468
469 if (!split_device) {
470 *driver_name = g_strdup("tape");
471 *device = g_strdup(split_driver_name);
472 #ifdef WANT_TAPE_DEVICE
473 g_warning("\"%s\" uses deprecated device naming convention; \n"
474 "using \"tape:%s\" instead.\n", user_name, user_name);
475 #endif /* WANT_TAPE_DEVICE */
476 } else {
477
478 *driver_name = g_strdup(split_driver_name);
479 *device = g_strdup(split_device);
480 }
481 ret = TRUE;
482
483 out:
484 g_strfreev(split_result);
485 return ret;
486 }
487
488 /* helper function for device_open */
489 static Device *
490 make_null_error(char *errmsg, DeviceStatusFlags status)
491 {
492 DeviceFactory factory;
493 Device *device;
494
495 factory = lookup_device_factory("null");
496 g_assert(factory != NULL);
497
498 device = factory("null:", "null", "");
499 device_set_error(device, errmsg, status);
500
501 return device;
502 }
503
504 char *
505 device_unaliased_name(
506 char *device_name)
507 {
508 device_config_t *dc;
509 char *unaliased_name;
510
511 /* look up the unaliased device name in the configuration */
512 if ((dc = lookup_device_config(device_name))) {
513 if (!(unaliased_name = device_config_get_tapedev(dc))
514 || unaliased_name[0] == '\0') {
515 return NULL;
516 }
517 } else {
518 unaliased_name = device_name;
519 }
520
521 return unaliased_name;
522 }
523
524 Device*
525 device_open (char * device_name)
526 {
527 char *device_type = NULL;
528 char *device_node = NULL;
529 char *unaliased_name = NULL;
530 DeviceFactory factory;
531 Device *device;
532
533 g_assert(device_name != NULL);
534
535 if (driverList == NULL) {
536 g_critical("device_open() called without device_api_init()!");
537 g_assert_not_reached();
538 }
539
540 if (device_name == NULL)
541 return make_null_error(g_strdup("No device name specified"), DEVICE_STATUS_DEVICE_ERROR);
542
543 unaliased_name = device_unaliased_name(device_name);
544 if (!unaliased_name) {
545 return make_null_error(
546 g_strdup_printf("Device \"%s\" has no tapedev", device_name),
547 DEVICE_STATUS_DEVICE_ERROR);
548 }
549
550 if (!validate_device_name(unaliased_name, &device_type, &device_node)) {
551 amfree(device_type);
552 amfree(device_node);
553 return make_null_error(
554 g_strdup_printf("\"%s\" is not a valid device name", unaliased_name),
555 DEVICE_STATUS_DEVICE_ERROR);
556 }
557
558 factory = lookup_device_factory(device_type);
559
560 if (factory == NULL) {
561 Device *nulldev = make_null_error(
562 g_strdup_printf("Device type \"%s\" is not known", device_type),
563 DEVICE_STATUS_DEVICE_ERROR);
564 amfree(device_type);
565 amfree(device_node);
566 return nulldev;
567 }
568
569 device = factory(device_name, device_type, device_node);
570 g_assert(device != NULL); /* factories must always return a device */
571
572 device->device_mutex = g_mutex_new();
573 amfree(device_type);
574 amfree(device_node);
575
576 return device;
577 }
578
579 void
580 device_reset(
581 Device *self)
582 {
583 self->status = DEVICE_STATUS_SUCCESS;
584 amfree(selfp->errmsg);
585 selfp->last_status = DEVICE_STATUS_SUCCESS;
586 amfree(selfp->statusmsg);
587 self->is_eom = FALSE;
588 }
589
590 char *
591 device_error(Device * self)
592 {
593 if (self == NULL) {
594 return device_error_or_status(self);
595 } else if (selfp->errmsg) {
596 return selfp->errmsg;
597 } else {
598 return "Unknown Device error";
599 }
600 }
601
602 char *
603 device_status_error(Device * self)
604 {
605 char **status_strv;
606 char *statusmsg;
607
608 if (self == NULL) {
609 return device_error_or_status(self);
610 }
611
612 /* reuse a previous statusmsg, if it was for the same status */
613 if (selfp->statusmsg && selfp->last_status == self->status)
614 return selfp->statusmsg;
615
616 amfree(selfp->statusmsg);
617
618 status_strv = g_flags_nick_to_strv(self->status, DEVICE_STATUS_FLAGS_TYPE);
619 g_assert(g_strv_length(status_strv) > 0);
620 if (g_strv_length(status_strv) == 1) {
621 statusmsg = g_strdup(*status_strv);
622 } else {
623 char * status_list = g_english_strjoinv(status_strv, "or");
624 statusmsg = g_strdup_printf("one of %s", status_list);
625 amfree(status_list);
626 }
627 g_strfreev(status_strv);
628
629 selfp->statusmsg = statusmsg;
630 selfp->last_status = self->status;
631 return statusmsg;
632 }
633
634 char *
635 device_error_or_status(Device * self)
636 {
637 if (self == NULL) {
638 return "Device is NULL";
639 } else if (selfp->errmsg) {
640 return selfp->errmsg;
641 } else {
642 return device_status_error(self);
643 }
644 }
645
646 void
647 device_set_error(Device *self, char *errmsg, DeviceStatusFlags new_flags)
648 {
649 char **flags_strv;
650 char *flags_str;
651 char *device_name;
652
653 if (!self) {
654 g_warning("device_set_error called with a NULL device: '%s'", errmsg? errmsg:"(NULL)");
655 amfree(errmsg);
656 return;
657 }
658
659 device_name = self->device_name? self->device_name : "(unknown device)";
660
661 if (errmsg && (!selfp->errmsg || !g_str_equal(errmsg, selfp->errmsg)))
662 g_debug("Device %s error = '%s'", device_name, errmsg);
663
664 amfree(selfp->errmsg);
665 selfp->errmsg = errmsg;
666
667 if (new_flags != DEVICE_STATUS_SUCCESS) {
668 flags_strv = g_flags_name_to_strv(new_flags, DEVICE_STATUS_FLAGS_TYPE);
669 g_assert(g_strv_length(flags_strv) > 0);
670 flags_str = g_english_strjoinv(flags_strv, "and");
671 g_debug("Device %s setting status flag(s): %s", device_name, flags_str);
672 amfree(flags_str);
673 g_strfreev(flags_strv);
674 }
675
676 self->status = new_flags;
677 }
678
679 char * device_build_amanda_header(Device * self, const dumpfile_t * info,
680 size_t *size) {
681 return build_header(info, size, self->block_size);
682 }
683
684 dumpfile_t * make_tapestart_header(Device * self, char * label,
685 char * timestamp) {
686 dumpfile_t * rval;
687 GValue val;
688 bzero(&val, sizeof(val));
689
690 g_assert(label != NULL);
691
692 rval = malloc(sizeof(*rval));
693 fh_init(rval);
694 rval->type = F_TAPESTART;
695 if (device_property_get(self, PROPERTY_BLOCK_SIZE, &val)) {
696 rval->blocksize = g_value_get_int(&val);
697 g_value_unset(&val);
698 }
699
700 amfree(self->volume_time);
701 if (get_timestamp_state(timestamp) == TIME_STATE_REPLACE) {
702 self->volume_time = get_proper_stamp_from_time(time(NULL));
703 } else {
704 self->volume_time = g_strdup(timestamp);
705 }
706 g_strlcpy(rval->datestamp, self->volume_time, sizeof(rval->datestamp));
707 g_strlcpy(rval->name, label, sizeof(rval->name));
708
709 return rval;
710 }
711
712 dumpfile_t * make_tapeend_header(void) {
713 dumpfile_t * rval;
714 char * timestamp;
715
716 rval = malloc(sizeof(*rval));
717 rval->type = F_TAPEEND;
718 timestamp = get_timestamp_from_time(time(NULL));
719 g_strlcpy(rval->datestamp, timestamp, sizeof(rval->datestamp));
720 amfree(timestamp);
721 return rval;
722 }
723
724 /* Try setting the blocksize on a device. Check results, fallback, and
725 * set error status for problems. */
726 static gboolean
727 try_set_blocksize(Device * device, guint blocksize) {
728 GValue val;
729 char *r;
730
731 bzero(&val, sizeof(val));
732
733 g_value_init(&val, G_TYPE_INT);
734 g_value_set_int(&val, blocksize);
735 r = device_property_set(device, PROPERTY_BLOCK_SIZE, &val);
736 g_value_unset(&val);
737
738 if (r) {
739 device_set_error(device,
740 g_strdup_printf(_("Setting BLOCK_SIZE to %u "
741 "not supported for device %s: %s\n"),
742 blocksize, device->device_name, r),
743 DEVICE_STATUS_DEVICE_ERROR);
744 g_free(r);
745 return FALSE;
746 }
747
748 return TRUE;
749 }
750
751 /* A GHFunc (callback for g_hash_table_foreach) */
752 static void set_device_property(gpointer key_p, gpointer value_p,
753 gpointer user_data_p) {
754 char * property_s = key_p;
755 property_t * property = value_p;
756 Device * device = user_data_p;
757 const DevicePropertyBase* property_base;
758 GValue property_value;
759 char * value;
760 char * r;
761
762 g_return_if_fail(IS_DEVICE(device));
763 g_return_if_fail(property_s != NULL);
764 g_return_if_fail(property != NULL);
765 g_return_if_fail(property->values != NULL);
766
767 /* don't continue beating on a device that's already erroring */
768 if (device_in_error(device)) return;
769
770 property_base = device_property_get_by_name(property_s);
771 if (property_base == NULL) {
772 /* Nonexistant property name. */
773 device_set_error(device,
774 g_strdup_printf(_("unknown device property name '%s'"), property_s),
775 DEVICE_STATUS_DEVICE_ERROR);
776 return;
777 }
778 if (g_slist_length(property->values) > 1) {
779 device_set_error(device,
780 g_strdup_printf(_("multiple values for device property '%s'"), property_s),
781 DEVICE_STATUS_DEVICE_ERROR);
782 return;
783 }
784
785 bzero(&property_value, sizeof(property_value));
786 g_value_init(&property_value, property_base->type);
787 value = property->values->data;
788 if (!g_value_set_from_string(&property_value, value)) {
789 /* Value type could not be interpreted. */
790 device_set_error(device,
791 g_strdup_printf(_("Could not parse property value '%s' for property '%s' (property type %s)"),
792 value, property_base->name, g_type_name(property_base->type)),
793 DEVICE_STATUS_DEVICE_ERROR);
794 return;
795 } else {
796 g_assert (G_VALUE_HOLDS(&property_value, property_base->type));
797 }
798
799 r = device_property_set(device, property_base->ID, &property_value);
800 if (r) {
801 /* Device rejects property. */
802 if (!device_in_error(device)) {
803 device_set_error(device,
804 g_strdup_printf(_("Could not set property '%s' to '%s' on %s: %s"),
805 property_base->name, value, device->device_name, r),
806 DEVICE_STATUS_DEVICE_ERROR);
807 }
808 return;
809 }
810 }
811
812 /* Set up properties based on various taper-related configuration parameters
813 * and from the tapetype.
814 */
815 static void
816 set_properties_from_global_config(Device * device) {
817 char * tapetype_name = getconf_str(CNF_TAPETYPE);
818 if (tapetype_name != NULL) {
819 tapetype_t * tapetype = lookup_tapetype(tapetype_name);
820 if (tapetype != NULL) {
821 GValue val;
822 guint64 length;
823 guint blocksize_kb;
824 char *r;
825
826 bzero(&val, sizeof(GValue));
827
828 if (tapetype_seen(tapetype, TAPETYPE_LENGTH)) {
829 length = tapetype_get_length(tapetype);
830 g_value_init(&val, G_TYPE_UINT64);
831 g_value_set_uint64(&val, length * 1024);
832 /* If this fails, it's not really an error. */
833 device_property_set(device, PROPERTY_MAX_VOLUME_USAGE, &val);
834 g_value_unset(&val);
835 }
836
837 if (tapetype_seen(tapetype, TAPETYPE_READBLOCKSIZE)) {
838 blocksize_kb = tapetype_get_readblocksize(tapetype);
839 g_value_init(&val, G_TYPE_UINT);
840 g_value_set_uint(&val, blocksize_kb * 1024);
841 r = device_property_set(device,
842 PROPERTY_READ_BLOCK_SIZE,
843 &val);
844 g_value_unset(&val);
845 if (r) {
846 /* a non-fatal error */
847 g_warning("Setting READ_BLOCK_SIZE to %ju not supported for device %s: %s.",
848 1024*(uintmax_t)blocksize_kb, device->device_name, r);
849 }
850 }
851
852 if (tapetype_seen(tapetype, TAPETYPE_BLOCKSIZE)) {
853 blocksize_kb = tapetype_get_blocksize(tapetype);
854 /* TODO: handle errors */
855 (void)try_set_blocksize(device, blocksize_kb * 1024);
856 }
857 }
858 }
859
860 g_hash_table_foreach(getconf_proplist(CNF_DEVICE_PROPERTY),
861 set_device_property, device);
862 }
863
864 /* Set properties specified within a device definition */
865 static void
866 set_properties_from_device_config(Device * device, device_config_t *dc) {
867 g_hash_table_foreach(device_config_get_property(dc),
868 set_device_property, device);
869 }
870
871 static gboolean
872 default_device_configure(Device *self, gboolean use_global_config)
873 {
874 device_config_t *dc;
875
876 if (device_in_error(self))
877 return FALSE;
878
879 if (use_global_config)
880 set_properties_from_global_config(self);
881
882 if (device_in_error(self))
883 return FALSE;
884
885 if ((dc = lookup_device_config(self->device_name)))
886 set_properties_from_device_config(self, dc);
887
888 return !device_in_error(self);
889 }
890
891 void device_clear_volume_details(Device * device) {
892 if (device == NULL || device->access_mode != ACCESS_NULL) {
893 return;
894 }
895
896 amfree(device->volume_label);
897 amfree(device->volume_time);
898 }
899
900 /* Here we put default implementations of virtual functions. Since
901 this class is virtual, many of these functions offer at best
902 incomplete functionality. But they do offer the useful commonality
903 that all devices can expect to need. */
904
905 static void default_device_open_device(Device * self, char * device_name,
906 char * device_type G_GNUC_UNUSED, char * device_node G_GNUC_UNUSED) {
907 /* Set the device_name property */
908 self->device_name = g_strdup(device_name);
909 }
910
911 static gboolean
912 property_get_block_size_fn(
913 Device *self,
914 DevicePropertyBase *base G_GNUC_UNUSED,
915 GValue *val,
916 PropertySurety *surety,
917 PropertySource *source)
918 {
919 g_value_unset_init(val, G_TYPE_INT);
920 g_assert(self->block_size < G_MAXINT); /* gsize -> gint */
921 g_value_set_int(val, (gint)self->block_size);
922
923 if (surety)
924 *surety = self->block_size_surety;
925
926 if (source)
927 *source = self->block_size_source;
928
929 return TRUE;
930 }
931
932 static gboolean
933 property_set_block_size_fn(
934 Device *self,
935 DevicePropertyBase *base G_GNUC_UNUSED,
936 GValue *val,
937 PropertySurety surety,
938 PropertySource source)
939 {
940 gint block_size = g_value_get_int(val);
941
942 g_assert(block_size >= 0); /* int -> gsize (unsigned) */
943 if ((gsize)block_size < self->min_block_size
944 || (gsize)block_size > self->max_block_size) {
945 device_set_error(self,
946 g_strdup_printf("Error setting BLOCK-SIZE property to '%zu', it must be between %zu and %zu", (gsize)block_size, self->min_block_size, self->max_block_size),
947 DEVICE_STATUS_DEVICE_ERROR);
948 return FALSE;
949 }
950
951 self->block_size = block_size;
952 self->block_size_surety = surety;
953 self->block_size_source = source;
954
955 return TRUE;
956 }
957
958 static gboolean
959 property_get_min_block_size_fn(
960 Device *self,
961 DevicePropertyBase *base G_GNUC_UNUSED,
962 GValue *val,
963 PropertySurety *surety,
964 PropertySource *source)
965 {
966 g_value_unset_init(val, G_TYPE_UINT);
967 g_assert(self->block_size < G_MAXUINT); /* gsize -> guint */
968 g_value_set_uint(val, (guint)self->min_block_size);
969
970 if (surety)
971 *surety = PROPERTY_SURETY_GOOD;
972
973 if (source)
974 *source = PROPERTY_SOURCE_DEFAULT;
975
976 return TRUE;
977 }
978
979 static gboolean
980 property_get_max_block_size_fn(
981 Device *self,
982 DevicePropertyBase *base G_GNUC_UNUSED,
983 GValue *val,
984 PropertySurety *surety,
985 PropertySource *source)
986 {
987 g_value_unset_init(val, G_TYPE_UINT);
988 g_assert(self->block_size < G_MAXUINT); /* gsize -> guint */
989 g_value_set_uint(val, (guint)self->max_block_size);
990
991 if (surety)
992 *surety = PROPERTY_SURETY_GOOD;
993
994 if (source)
995 *source = PROPERTY_SOURCE_DEFAULT;
996
997 return TRUE;
998 }
999
1000 static gboolean
1001 property_get_canonical_name_fn(
1002 Device *self,
1003 DevicePropertyBase *base G_GNUC_UNUSED,
1004 GValue *val,
1005 PropertySurety *surety,
1006 PropertySource *source)
1007 {
1008 g_value_unset_init(val, G_TYPE_STRING);
1009 g_value_set_string(val, self->device_name);
1010
1011 if (surety)
1012 *surety = PROPERTY_SURETY_GOOD;
1013
1014 if (source)
1015 *source = PROPERTY_SOURCE_DEFAULT;
1016
1017 return TRUE;
1018 }
1019
1020 /* util function */
1021 static PropertyPhaseFlags
1022 state_to_phase(
1023 Device *self)
1024 {
1025 if (self->access_mode == ACCESS_NULL) {
1026 return PROPERTY_PHASE_BEFORE_START;
1027 } else if (IS_WRITABLE_ACCESS_MODE(self->access_mode)) {
1028 if (self->in_file) {
1029 return PROPERTY_PHASE_INSIDE_FILE_WRITE;
1030 } else {
1031 return PROPERTY_PHASE_BETWEEN_FILE_WRITE;
1032 }
1033 } else { /* read mode */
1034 if (self->in_file) {
1035 return PROPERTY_PHASE_INSIDE_FILE_READ;
1036 } else {
1037 return PROPERTY_PHASE_BETWEEN_FILE_READ;
1038 }
1039 }
1040 }
1041
1042 /* This default implementation serves up static responses, and
1043 implements a few default responses based on values from the Device
1044 struct. */
1045 static gboolean
1046 default_device_property_get_ex(
1047 Device * self,
1048 DevicePropertyId id,
1049 GValue * val,
1050 PropertySurety *surety,
1051 PropertySource *source)
1052 {
1053 DeviceProperty *prop;
1054 GArray *class_properties;
1055 PropertyPhaseFlags cur_phase;
1056
1057 /* Most of this function's job is to sanity-check everything, then
1058 * call the relevant getter. */
1059
1060 class_properties = DEVICE_GET_CLASS(self)->class_properties;
1061 if (id >= class_properties->len)
1062 return FALSE;
1063
1064 prop = &g_array_index(class_properties, DeviceProperty, id);
1065 if (prop->base == NULL)
1066 return FALSE;
1067
1068 if (val || surety || source) {
1069 /* check the phase */
1070 cur_phase = state_to_phase(self);
1071 if (!(prop->access & cur_phase))
1072 return FALSE;
1073
1074 if (prop->getter == NULL)
1075 return FALSE;
1076
1077 if (!prop->getter(self, prop->base, val, surety, source))
1078 return FALSE;
1079 }
1080
1081 return TRUE;
1082 }
1083
1084 static char *
1085 default_device_property_set_ex(
1086 Device *self,
1087 DevicePropertyId id,
1088 GValue * val,
1089 PropertySurety surety,
1090 PropertySource source)
1091 {
1092 DeviceProperty *prop;
1093 GArray *class_properties;
1094 PropertyPhaseFlags cur_phase;
1095
1096 /* Most of this function's job is to sanity-check everything, then
1097 * call the relevant setter. */
1098
1099 if (device_in_error(self))
1100 return g_strdup("device already in error");
1101
1102 class_properties = DEVICE_GET_CLASS(self)->class_properties;
1103 if (id >= class_properties->len)
1104 return g_strdup("unknwon device-property");
1105
1106 prop = &g_array_index(class_properties, DeviceProperty, id);
1107 if (prop->base == NULL)
1108 return g_strdup("unknwon device-property");
1109
1110 /* check that the type matches */
1111 if (!G_VALUE_HOLDS(val, prop->base->type))
1112 return g_strdup("property can't hold that value");
1113
1114 /* check the phase */
1115 cur_phase = state_to_phase(self) << PROPERTY_PHASE_SHIFT;
1116 if (!(prop->access & cur_phase))
1117 return g_strdup_printf("Not allowed to set property");
1118
1119 if (prop->setter == NULL)
1120 return g_strdup("no prop-setter FF");
1121
1122 if (!prop->setter(self, prop->base, val, surety, source)) {
1123 if (device_in_error(self)) {
1124 return g_strdup(device_error_or_status(self));
1125 } else {
1126 return g_strdup("prop-setter failed");
1127 }
1128 }
1129
1130 return NULL;
1131 }
1132
1133 const GSList *
1134 device_property_get_list (Device * self)
1135 {
1136 g_assert(IS_DEVICE(self));
1137
1138 return DEVICE_GET_CLASS(self)->class_properties_list;
1139 }
1140
1141 /* XXX WARNING XXX
1142 * All the functions below this comment are stub functions that do nothing
1143 * but implement the virtual function table. Call these functions and they
1144 * will do what you expect vis-a-vis virtual functions. But don't put code
1145 * in them beyond error checking and VFT lookup. */
1146
1147 void
1148 device_open_device (Device * self, char * device_name,
1149 char * device_type, char * device_node)
1150 {
1151 DeviceClass *klass;
1152
1153 g_assert(IS_DEVICE(self));
1154 g_assert(device_name != NULL);
1155
1156 klass = DEVICE_GET_CLASS(self);
1157 g_assert(klass->open_device);
1158 (klass->open_device)(self, device_name, device_type, device_node);
1159 }
1160
1161 DeviceStatusFlags device_read_label(Device * self) {
1162 DeviceClass * klass;
1163
1164 g_assert(self != NULL);
1165 g_assert(IS_DEVICE(self));
1166 g_assert(self->access_mode == ACCESS_NULL);
1167
1168 klass = DEVICE_GET_CLASS(self);
1169 g_assert(klass->read_label);
1170 return (klass->read_label)(self);
1171 }
1172
1173 gboolean
1174 device_finish (Device * self) {
1175 DeviceClass *klass;
1176
1177 g_assert(IS_DEVICE (self));
1178
1179 klass = DEVICE_GET_CLASS(self);
1180 g_assert(klass->finish);
1181 return (klass->finish)(self);
1182 }
1183
1184 void
1185 device_clear_bytes_read (Device * self) {
1186 DeviceClass *klass;
1187
1188 g_assert(IS_DEVICE (self));
1189
1190 g_mutex_lock(self->device_mutex);
1191 if (self->in_file) {
1192 klass = DEVICE_GET_CLASS(self);
1193 if (klass->clear_bytes_read) {
1194 (klass->clear_bytes_read)(self);
1195 } else {
1196 self->bytes_read = 0;
1197 }
1198 }
1199 g_mutex_unlock(self->device_mutex);
1200 }
1201
1202 guint64
1203 device_get_bytes_read (Device * self) {
1204 DeviceClass *klass;
1205 guint64 bytes = 0;
1206
1207 g_assert(IS_DEVICE (self));
1208
1209 g_mutex_lock(self->device_mutex);
1210 if (self->in_file) {
1211 klass = DEVICE_GET_CLASS(self);
1212 if (klass->get_bytes_read) {
1213 bytes = (klass->get_bytes_read)(self);
1214 } else {
1215 bytes = self->bytes_read;
1216 }
1217 }
1218 g_mutex_unlock(self->device_mutex);
1219 return bytes;
1220 }
1221
1222 guint64
1223 device_get_bytes_written (Device * self) {
1224 DeviceClass *klass;
1225 guint64 bytes = 0;
1226
1227 g_assert(IS_DEVICE (self));
1228
1229 g_mutex_lock(self->device_mutex);
1230 if (self->in_file) {
1231 klass = DEVICE_GET_CLASS(self);
1232 if (klass->get_bytes_written) {
1233 bytes = (klass->get_bytes_written)(self);
1234 } else {
1235 bytes = self->bytes_written;
1236 }
1237 }
1238 g_mutex_unlock(self->device_mutex);
1239 return bytes;
1240 }
1241
1242 gboolean
1243 device_configure (Device * self, gboolean use_global_config)
1244 {
1245 DeviceClass *klass;
1246
1247 g_assert(IS_DEVICE (self));
1248 g_assert(self->access_mode == ACCESS_NULL);
1249
1250 klass = DEVICE_GET_CLASS(self);
1251 if(klass->configure) {
1252 return (klass->configure)(self, use_global_config);
1253 } else {
1254 device_set_error(self,
1255 g_strdup(_("Unimplemented method")),
1256 DEVICE_STATUS_DEVICE_ERROR);
1257 return FALSE;
1258 }
1259 }
1260
1261 gboolean
1262 device_start (Device * self, DeviceAccessMode mode,
1263 char * label, char * timestamp)
1264 {
1265 DeviceClass *klass;
1266 char * local_timestamp = NULL;
1267 gboolean rv;
1268
1269 g_assert(IS_DEVICE (self));
1270 g_assert(mode != ACCESS_NULL);
1271 g_assert(mode != ACCESS_WRITE || label != NULL);
1272
1273 klass = DEVICE_GET_CLASS(self);
1274 g_assert(klass->start);
1275
1276 /* For a good combination of synchronization and public simplicity,
1277 this stub function does not require a timestamp, but the actual
1278 implementation function does. We generate the timestamp here with
1279 time(). */
1280 if (mode == ACCESS_WRITE &&
1281 get_timestamp_state(timestamp) == TIME_STATE_REPLACE) {
1282 local_timestamp = timestamp =
1283 get_proper_stamp_from_time(time(NULL));
1284 }
1285
1286 rv = (klass->start)(self, mode, label, timestamp);
1287 amfree(local_timestamp);
1288 return rv;
1289 }
1290
1291 DeviceWriteResult
1292 device_write_block (Device * self, guint size, gpointer block)
1293 {
1294 DeviceClass *klass;
1295
1296 g_assert(IS_DEVICE (self));
1297 g_assert(size > 0);
1298
1299 /* these are all things that the caller should take care to
1300 * guarantee, so we just assert them here */
1301 g_assert(size <= self->block_size);
1302 g_assert(self->in_file);
1303 g_assert(!selfp->wrote_short_block);
1304 g_assert(block != NULL);
1305 g_assert(IS_WRITABLE_ACCESS_MODE(self->access_mode));
1306
1307 if (size < self->block_size)
1308 selfp->wrote_short_block = TRUE;
1309
1310 klass = DEVICE_GET_CLASS(self);
1311 g_assert(klass->write_block);
1312 return (*klass->write_block)(self,size, block);
1313 }
1314
1315 gboolean
1316 device_start_file (Device * self, dumpfile_t * jobInfo) {
1317 DeviceClass * klass;
1318
1319 g_assert(IS_DEVICE (self));
1320 g_assert(!(self->in_file));
1321 g_assert(jobInfo != NULL);
1322
1323 selfp->wrote_short_block = FALSE;
1324
1325 klass = DEVICE_GET_CLASS(self);
1326 g_assert(klass->start_file);
1327 return (klass->start_file)(self, jobInfo );
1328 }
1329
1330 gboolean
1331 device_finish_file (Device * self)
1332 {
1333 DeviceClass *klass;
1334
1335 g_assert(IS_DEVICE (self));
1336 g_assert(IS_WRITABLE_ACCESS_MODE(self->access_mode));
1337 g_assert(self->in_file);
1338
1339 klass = DEVICE_GET_CLASS(self);
1340 g_assert(klass->finish_file);
1341 return (klass->finish_file)(self);
1342 }
1343
1344 gboolean
1345 device_init_seek_file (Device * self, guint file)
1346 {
1347 DeviceClass *klass;
1348
1349 g_assert(IS_DEVICE (self));
1350 g_assert(self->access_mode == ACCESS_READ);
1351
1352 klass = DEVICE_GET_CLASS(self);
1353 if (klass->init_seek_file) {
1354 return (klass->init_seek_file)(self,file);
1355 } else {
1356 return TRUE;
1357 }
1358 }
1359
1360 dumpfile_t*
1361 device_seek_file (Device * self, guint file)
1362 {
1363 DeviceClass *klass;
1364
1365 g_assert(IS_DEVICE (self));
1366 g_assert(file == 0 || self->access_mode == ACCESS_READ);
1367
1368 klass = DEVICE_GET_CLASS(self);
1369 g_assert(klass->seek_file);
1370 return (klass->seek_file)(self,file);
1371 }
1372
1373 gboolean
1374 device_seek_block (Device * self, guint64 block)
1375 {
1376 DeviceClass *klass;
1377
1378 g_assert(IS_DEVICE (self));
1379 g_assert(self->access_mode == ACCESS_READ);
1380 g_assert(self->in_file);
1381
1382 klass = DEVICE_GET_CLASS(self);
1383 g_assert(klass->seek_block);
1384 return (klass->seek_block)(self,block);
1385 }
1386
1387 int
1388 device_read_block (Device * self, gpointer buffer, int * size, int max_block)
1389 {
1390 DeviceClass *klass;
1391
1392 g_assert(IS_DEVICE (self));
1393 g_assert(size != NULL);
1394 g_assert(self->access_mode == ACCESS_READ);
1395
1396 if (*size != 0) {
1397 g_assert(buffer != NULL);
1398 }
1399
1400 klass = DEVICE_GET_CLASS(self);
1401 g_assert(klass->read_block);
1402 return (klass->read_block)(self,buffer,size,max_block);
1403 }
1404
1405 gboolean
1406 device_property_get_ex(
1407 Device * self,
1408 DevicePropertyId id,
1409 GValue * val,
1410 PropertySurety *surety,
1411 PropertySource *source)
1412 {
1413 DeviceClass *klass;
1414
1415 g_assert(IS_DEVICE (self));
1416 g_assert(device_property_get_by_id(id) != NULL);
1417
1418 klass = DEVICE_GET_CLASS(self);
1419
1420 g_assert(klass->property_get_ex);
1421 return (klass->property_get_ex)(self, id, val, surety, source);
1422 }
1423
1424 char *
1425 device_property_set_ex(
1426 Device * self,
1427 DevicePropertyId id,
1428 GValue * val,
1429 PropertySurety surety,
1430 PropertySource source)
1431 {
1432 DeviceClass *klass;
1433
1434 g_assert(IS_DEVICE (self));
1435
1436 klass = DEVICE_GET_CLASS(self);
1437
1438 g_assert(klass->property_set_ex);
1439 return (klass->property_set_ex)(self, id, val, surety, source);
1440 }
1441
1442 gboolean
1443 device_recycle_file (Device * self, guint filenum)
1444 {
1445 DeviceClass *klass;
1446
1447 g_assert(self != NULL);
1448 g_assert(IS_DEVICE (self));
1449 g_assert(self->access_mode == ACCESS_APPEND);
1450 g_assert(!self->in_file);
1451
1452 klass = DEVICE_GET_CLASS(self);
1453
1454 g_assert(klass->recycle_file);
1455 return (klass->recycle_file)(self,filenum);
1456 }
1457
1458 gboolean
1459 device_erase (Device * self)
1460 {
1461 DeviceClass *klass;
1462
1463 g_assert(IS_DEVICE (self));
1464 g_assert(self->access_mode == ACCESS_NULL);
1465 g_assert(!self->in_file);
1466
1467 klass = DEVICE_GET_CLASS(self);
1468 if(klass->erase) {
1469 return (klass->erase)(self);
1470 } else {
1471 device_set_error(self,
1472 g_strdup(_("Unimplemented method")),
1473 DEVICE_STATUS_DEVICE_ERROR);
1474 return FALSE;
1475 }
1476 }
1477
1478 gboolean
1479 device_eject (Device * self)
1480 {
1481 DeviceClass *klass;
1482
1483 g_assert(IS_DEVICE (self));
1484 g_assert(self->access_mode == ACCESS_NULL);
1485 g_assert(!self->in_file);
1486
1487 klass = DEVICE_GET_CLASS(self);
1488 if (klass->eject) {
1489 return (klass->eject)(self);
1490 } else {
1491 return TRUE;
1492 }
1493 }
1494
1495 gboolean
1496 device_listen(
1497 Device *self,
1498 gboolean for_writing,
1499 DirectTCPAddr **addrs)
1500 {
1501 DeviceClass *klass;
1502
1503 klass = DEVICE_GET_CLASS(self);
1504 if(klass->listen) {
1505 return (klass->listen)(self, for_writing, addrs);
1506 } else {
1507 device_set_error(self,
1508 g_strdup(_("Unimplemented method")),
1509 DEVICE_STATUS_DEVICE_ERROR);
1510 return FALSE;
1511 }
1512 }
1513
1514
1515 int
1516 device_accept(
1517 Device *self,
1518 DirectTCPConnection **conn,
1519 int *cancelled,
1520 GMutex *abort_mutex,
1521 GCond *abort_cond)
1522 {
1523 DeviceClass *klass;
1524
1525 klass = DEVICE_GET_CLASS(self);
1526 if(klass->accept) {
1527 return (klass->accept)(self, conn, cancelled, abort_mutex,
1528 abort_cond);
1529 } else {
1530 device_set_error(self,
1531 g_strdup(_("Unimplemented method")),
1532 DEVICE_STATUS_DEVICE_ERROR);
1533 return 1;
1534 }
1535 }
1536
1537 int
1538 device_connect(
1539 Device *self,
1540 gboolean for_writing,
1541 DirectTCPAddr *addrs,
1542 DirectTCPConnection **conn,
1543 int *cancelled,
1544 GMutex *abort_mutex,
1545 GCond *abort_cond)
1546 {
1547 DeviceClass *klass;
1548
1549 klass = DEVICE_GET_CLASS(self);
1550 if(klass->connect) {
1551 return (klass->connect)(self, for_writing, addrs, conn, cancelled,
1552 abort_mutex, abort_cond);
1553 } else {
1554 device_set_error(self,
1555 g_strdup(_("Unimplemented method")),
1556 DEVICE_STATUS_DEVICE_ERROR);
1557 return 1;
1558 }
1559 }
1560
1561 int
1562 device_write_from_connection(
1563 Device *self,
1564 guint64 size,
1565 guint64 *actual_size,
1566 int *cancelled,
1567 GMutex *abort_mutex,
1568 GCond *abort_cond)
1569 {
1570 DeviceClass *klass;
1571
1572 klass = DEVICE_GET_CLASS(self);
1573
1574 g_assert(self->in_file);
1575 g_assert(IS_WRITABLE_ACCESS_MODE(self->access_mode));
1576
1577 if(klass->write_from_connection) {
1578 return (klass->write_from_connection)(self, size, actual_size,
1579 cancelled,
1580 abort_mutex, abort_cond);
1581 } else {
1582 device_set_error(self,
1583 g_strdup(_("Unimplemented method")),
1584 DEVICE_STATUS_DEVICE_ERROR);
1585 return 1;
1586 }
1587 }
1588
1589 int
1590 device_read_to_connection(
1591 Device *self,
1592 guint64 size,
1593 guint64 *actual_size,
1594 int *cancelled,
1595 GMutex *abort_mutex,
1596 GCond *abort_cond)
1597 {
1598 DeviceClass *klass;
1599
1600 g_assert(self->in_file);
1601 g_assert(self->access_mode == ACCESS_READ);
1602
1603 klass = DEVICE_GET_CLASS(self);
1604 if(klass->read_to_connection) {
1605 return (klass->read_to_connection)(self, size, actual_size, cancelled,
1606 abort_mutex, abort_cond);
1607 } else {
1608 device_set_error(self,
1609 g_strdup(_("Unimplemented method")),
1610 DEVICE_STATUS_DEVICE_ERROR);
1611 return 1;
1612 }
1613 }
1614
1615 gboolean
1616 device_use_connection(
1617 Device *self,
1618 DirectTCPConnection *conn)
1619 {
1620 DeviceClass *klass;
1621
1622 g_assert(self->access_mode == ACCESS_NULL);
1623
1624 klass = DEVICE_GET_CLASS(self);
1625 if(klass->use_connection) {
1626 return (klass->use_connection)(self, conn);
1627 } else {
1628 device_set_error(self,
1629 g_strdup(_("Unimplemented method")),
1630 DEVICE_STATUS_DEVICE_ERROR);
1631 return FALSE;
1632 }
1633 }
1634
1635 gboolean
1636 device_check_writable(
1637 Device *self)
1638 {
1639 DeviceClass *klass;
1640
1641 klass = DEVICE_GET_CLASS(self);
1642 if (klass->check_writable) {
1643 return (klass->check_writable)(self);
1644 } else {
1645 return TRUE;
1646 }
1647 }
1648
1649 gboolean
1650 device_have_set_reuse(
1651 Device *self)
1652 {
1653 DeviceClass *klass;
1654
1655 klass = DEVICE_GET_CLASS(self);
1656 if (klass->set_reuse) {
1657 return TRUE;
1658 } else {
1659 return FALSE;
1660 }
1661 }
1662
1663 gboolean
1664 device_set_reuse(
1665 Device *self)
1666 {
1667 DeviceClass *klass;
1668
1669 klass = DEVICE_GET_CLASS(self);
1670 if (klass->set_reuse) {
1671 return (klass->set_reuse)(self);
1672 } else {
1673 return TRUE;
1674 }
1675 }
1676
1677 gboolean
1678 device_set_no_reuse(
1679 Device *self,
1680 char *label,
1681 char *datestamp)
1682 {
1683 DeviceClass *klass;
1684
1685 klass = DEVICE_GET_CLASS(self);
1686 if (klass->set_no_reuse) {
1687 return (klass->set_no_reuse)(self, label, datestamp);
1688 } else {
1689 return TRUE;
1690 }
1691 }
1692
1693 gboolean
1694 device_sync_catalog(
1695 Device *self,
1696 int request,
1697 int wait,
1698 char **slot_names)
1699 {
1700 DeviceClass *klass;
1701
1702 g_assert(self->access_mode == ACCESS_NULL);
1703
1704 klass = DEVICE_GET_CLASS(self);
1705 if(klass->sync_catalog) {
1706 return (klass->sync_catalog)(self, request, wait, slot_names);
1707 } else {
1708 device_set_error(self,
1709 g_strdup(_("Unimplemented method")),
1710 DEVICE_STATUS_DEVICE_ERROR);
1711 return FALSE;
1712 }
1713 }
1714
1715 gboolean
1716 device_create(
1717 Device *self)
1718 {
1719 DeviceClass *klass;
1720
1721 g_assert(self->access_mode == ACCESS_NULL);
1722
1723 klass = DEVICE_GET_CLASS(self);
1724 if(klass->create) {
1725 return (klass->create)(self);
1726 } else {
1727 device_set_error(self,
1728 g_strdup(_("Unimplemented method")),
1729 DEVICE_STATUS_DEVICE_ERROR);
1730 return FALSE;
1731 }
1732 }
1733
1734 gboolean
1735 device_allow_take_scribe_from(
1736 Device *self)
1737 {
1738 return self->allow_take_scribe_from;
1739 }
1740
1741 /* Property handling */
1742
1743 void
1744 device_class_register_property(
1745 DeviceClass *klass,
1746 DevicePropertyId id,
1747 PropertyAccessFlags access,
1748 PropertyGetFn getter,
1749 PropertySetFn setter)
1750 {
1751 DevicePropertyBase *base;
1752 DeviceProperty *prop;
1753 GSList *proplist;
1754 guint i;
1755
1756 g_assert(klass != NULL);
1757
1758 base = device_property_get_by_id(id);
1759 g_assert(base != NULL);
1760
1761 if (klass->class_properties->len <= id) {
1762 g_array_set_size(klass->class_properties, id+1);
1763 }
1764
1765 prop = &g_array_index(klass->class_properties, DeviceProperty, id);
1766 prop->base = base;
1767 prop->access = access;
1768 prop->getter = getter;
1769 prop->setter = setter;
1770
1771 /* completely rewrite the list of prop pointers, as they may have changed,
1772 * or we may have replaced an existing property*/
1773
1774 if (klass->class_properties_list) {
1775 g_slist_free(klass->class_properties_list);
1776 }
1777
1778 proplist = NULL;
1779 for (i = 0; i < klass->class_properties->len; i++) {
1780 prop = &g_array_index(klass->class_properties, DeviceProperty, i);
1781 if (!prop->base)
1782 continue;
1783 proplist = g_slist_prepend(proplist, prop);
1784 }
1785
1786 klass->class_properties_list = proplist;
1787 }
1788
1789 gboolean
1790 device_set_simple_property(
1791 Device *self,
1792 DevicePropertyId id,
1793 GValue *val,
1794 PropertySurety surety,
1795 PropertySource source)
1796 {
1797 SimpleProperty *simp;
1798 DeviceProperty *prop;
1799
1800 prop = &g_array_index(DEVICE_GET_CLASS(self)->class_properties,
1801 DeviceProperty, id);
1802
1803 /* these assertions should already be checked, but let's be sure */
1804 g_assert(prop->base != NULL); /* prop must be registered with device */
1805 g_assert(G_VALUE_HOLDS(val, prop->base->type));
1806
1807 simp = g_new0(SimpleProperty, 1);
1808 simp->prop = prop;
1809 g_value_unset_copy(val, &(simp->response));
1810 simp->surety = surety;
1811 simp->source = source;
1812
1813 g_hash_table_insert(selfp->simple_properties,
1814 GINT_TO_POINTER(id),
1815 simp);
1816
1817 return TRUE;
1818 }
1819
1820 gboolean
1821 device_simple_property_set_fn(
1822 Device *self,
1823 DevicePropertyBase *base,
1824 GValue *val,
1825 PropertySurety surety,
1826 PropertySource source)
1827 {
1828 return device_set_simple_property(self, base->ID, val, surety, source);
1829 }
1830
1831 gboolean
1832 device_get_simple_property(
1833 Device *self,
1834 DevicePropertyId id,
1835 GValue *val,
1836 PropertySurety *surety,
1837 PropertySource *source)
1838 {
1839 SimpleProperty *simp =
1840 g_hash_table_lookup(selfp->simple_properties,
1841 GINT_TO_POINTER(id));
1842
1843 if (!simp)
1844 return FALSE;
1845
1846 if (val)
1847 g_value_unset_copy(&(simp->response), val);
1848
1849 if (surety)
1850 *surety = simp->surety;
1851
1852 if (source)
1853 *source = simp->source;
1854
1855 return TRUE;
1856 }
1857
1858 gboolean
1859 device_simple_property_get_fn(
1860 Device *self,
1861 DevicePropertyBase *base,
1862 GValue *val,
1863 PropertySurety *surety,
1864 PropertySource *source)
1865 {
1866 return device_get_simple_property(self, base->ID, val, surety, source);
1867 }