Debian Codegraph @

device-src/dvdrw-device.c

raw
1 /*
2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 2009 University of Maryland at College Park
4 * Copyright (c) 2007-2012 Zmanda, Inc. All Rights Reserved.
5 * Copyright (c) 2013-2016 Carbonite, Inc. All Rights Reserved.
6 * All Rights Reserved.
7 *
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that
11 * copyright notice and this permission notice appear in supporting
12 * documentation, and that the name of U.M. not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. U.M. makes no representations about the
15 * suitability of this software for any purpose. It is provided "as is"
16 * without express or implied warranty.
17 *
18 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
20 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
22 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
23 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 *
25 * Author: Sam Couter <[email protected]>
26 */
27
28 #include "amanda.h"
29 #include "vfs-device.h"
30
31 /*
32 * Type checking and casting macros
33 */
34 #define TYPE_DVDRW_DEVICE (dvdrw_device_get_type())
35 #define DVDRW_DEVICE(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), TYPE_DVDRW_DEVICE, DvdRwDevice)
36 #define DVDRW_DEVICE_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), TYPE_DVDRW_DEVICE, DvdRwDevice const)
37 #define DVDRW_DEVICE_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), TYPE_DVDRW_DEVICE, DvdRwDeviceClass)
38 #define IS_DVDRW_DEVICE(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), TYPE_DVDRW_DEVICE)
39 #define DVDRW_DEVICE_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), TYPE_DVDRW_DEVICE, DvdRwDeviceClass)
40
41 /* Forward declaration */
42 static GType dvdrw_device_get_type(void);
43
44 /*
45 * Main object structure
46 */
47 typedef struct _DvdRwDevice DvdRwDevice;
48 struct _DvdRwDevice {
49 VfsDevice __parent__;
50
51 gchar *dvdrw_device;
52 gchar *cache_dir;
53 gchar *cache_data;
54 gchar *mount_point;
55 gchar *mount_data;
56 gboolean mounted;
57 gboolean keep_cache;
58 gboolean unlabelled_when_unmountable;
59 gchar *growisofs_command;
60 gchar *mount_command;
61 gchar *umount_command;
62 };
63
64 /*
65 * Class definition
66 */
67 typedef struct _DvdRwDeviceClass DvdRwDeviceClass;
68 struct _DvdRwDeviceClass {
69 VfsDeviceClass __parent__;
70 };
71
72 /* Where the DVD-RW can be mounted */
73 static DevicePropertyBase device_property_dvdrw_mount_point;
74 #define PROPERTY_DVDRW_MOUNT_POINT (device_property_dvdrw_mount_point.ID)
75
76 /* Should the on-disk version be kept after the optical disc has been written? */
77 static DevicePropertyBase device_property_dvdrw_keep_cache;
78 #define PROPERTY_DVDRW_KEEP_CACHE (device_property_dvdrw_keep_cache.ID)
79
80 /* Should a mount failure (eg, a freshly formatted disc) when reading a label be treated like an unlabelled volume? */
81 static DevicePropertyBase device_property_dvdrw_unlabelled_when_unmountable;
82 #define PROPERTY_DVDRW_UNLABELLED_WHEN_UNMOUNTABLE (device_property_dvdrw_unlabelled_when_unmountable.ID)
83
84 /* Where to find the growisofs command */
85 static DevicePropertyBase device_property_dvdrw_growisofs_command;
86 #define PROPERTY_DVDRW_GROWISOFS_COMMAND (device_property_dvdrw_growisofs_command.ID)
87
88 /* Where to find the filesystem mount command */
89 static DevicePropertyBase device_property_dvdrw_mount_command;
90 #define PROPERTY_DVDRW_MOUNT_COMMAND (device_property_dvdrw_mount_command.ID)
91
92 /* Where to find the filesystem unmount command */
93 static DevicePropertyBase device_property_dvdrw_umount_command;
94 #define PROPERTY_DVDRW_UMOUNT_COMMAND (device_property_dvdrw_umount_command.ID)
95
96 /* GObject housekeeping */
97 void
98 dvdrw_device_register(void);
99
100 static Device*
101 dvdrw_device_factory(char *device_name, char *device_type, char *device_node);
102
103 static void
104 dvdrw_device_class_init (DvdRwDeviceClass *c);
105
106 static void
107 dvdrw_device_init (DvdRwDevice *self);
108
109 /* Properties */
110 static gboolean
111 dvdrw_device_set_mount_point_fn(Device *self,
112 DevicePropertyBase *base, GValue *val,
113 PropertySurety surety, PropertySource source);
114
115 static gboolean
116 dvdrw_device_set_keep_cache_fn(Device *self,
117 DevicePropertyBase *base, GValue *val,
118 PropertySurety surety, PropertySource source);
119
120 static gboolean
121 dvdrw_device_set_unlabelled_when_unmountable_fn(Device *self,
122 DevicePropertyBase *base, GValue *val,
123 PropertySurety surety, PropertySource source);
124
125 static gboolean
126 dvdrw_device_set_growisofs_command_fn(Device *self,
127 DevicePropertyBase *base, GValue *val,
128 PropertySurety surety, PropertySource source);
129
130 static gboolean
131 dvdrw_device_set_mount_command_fn(Device *self,
132 DevicePropertyBase *base, GValue *val,
133 PropertySurety surety, PropertySource source);
134
135 static gboolean
136 dvdrw_device_set_umount_command_fn(Device *self,
137 DevicePropertyBase *base, GValue *val,
138 PropertySurety surety, PropertySource source);
139
140 /* Methods */
141 static void
142 dvdrw_device_open_device(Device *dself, char *device_name, char *device_type, char *device_node);
143
144 static DeviceStatusFlags
145 dvdrw_device_read_label(Device *dself);
146
147 static gboolean
148 dvdrw_device_start(Device *dself, DeviceAccessMode mode, char *label, char *timestamp);
149
150 static gboolean
151 dvdrw_device_finish(Device *dself);
152
153 static void
154 dvdrw_device_finalize(GObject *gself);
155
156 /* Helper functions */
157 static gboolean
158 check_access_mode(DvdRwDevice *self, DeviceAccessMode mode);
159
160 static gboolean
161 check_readable(DvdRwDevice *self);
162
163 static DeviceStatusFlags
164 mount_disc(DvdRwDevice *self, gboolean report_error);
165
166 static void
167 unmount_disc(DvdRwDevice *self);
168
169 static gboolean
170 burn_disc(DvdRwDevice *self);
171
172 static DeviceStatusFlags
173 execute_command(DvdRwDevice *self, gchar **argv, gint *status);
174
175 static GType
176 dvdrw_device_get_type (void)
177 {
178 static GType type = 0;
179
180 if (G_UNLIKELY(type == 0)) {
181 static const GTypeInfo info = {
182 sizeof (DvdRwDeviceClass),
183 (GBaseInitFunc) NULL,
184 (GBaseFinalizeFunc) NULL,
185 (GClassInitFunc) dvdrw_device_class_init,
186 (GClassFinalizeFunc) NULL,
187 NULL /* class_data */,
188 sizeof (DvdRwDevice),
189 0 /* n_preallocs */,
190 (GInstanceInitFunc) dvdrw_device_init,
191 NULL
192 };
193
194 type = g_type_register_static (TYPE_VFS_DEVICE, "DvdRwDevice",
195 &info, (GTypeFlags)0);
196 }
197
198 return type;
199 }
200
201 void
202 dvdrw_device_register(void)
203 {
204 const char *device_prefix_list[] = { "dvdrw", NULL };
205
206 device_property_fill_and_register(&device_property_dvdrw_mount_point,
207 G_TYPE_STRING, "dvdrw_mount_point",
208 "Directory to mount DVD-RW for reading");
209
210 device_property_fill_and_register(&device_property_dvdrw_keep_cache,
211 G_TYPE_BOOLEAN, "dvdrw_keep_cache",
212 "Keep on-disk cache after DVD-RW has been written");
213
214 device_property_fill_and_register(&device_property_dvdrw_unlabelled_when_unmountable,
215 G_TYPE_BOOLEAN, "dvdrw_unlabelled_when_unmountable",
216 "Treat unmountable volumes as unlabelled when reading label");
217
218 device_property_fill_and_register(&device_property_dvdrw_growisofs_command,
219 G_TYPE_STRING, "dvdrw_growisofs_command",
220 "The location of the growisofs command used to write the DVD-RW");
221
222 device_property_fill_and_register(&device_property_dvdrw_mount_command,
223 G_TYPE_STRING, "dvdrw_mount_command",
224 "The location of the mount command used to mount the DVD-RW filesystem for reading");
225
226 device_property_fill_and_register(&device_property_dvdrw_umount_command,
227 G_TYPE_STRING, "dvdrw_umount_command",
228 "The location of the umount command used to unmount the DVD-RW filesystem after reading");
229
230 register_device(dvdrw_device_factory, device_prefix_list);
231 }
232
233 static Device *
234 dvdrw_device_factory(char *device_name, char *device_type, char *device_node)
235 {
236 Device *device;
237
238 g_assert(g_str_has_prefix(device_type, "dvdrw"));
239
240 device = DEVICE(g_object_new(TYPE_DVDRW_DEVICE, NULL));
241 device_open_device(device, device_name, device_type, device_node);
242
243 return device;
244 }
245
246 static void
247 dvdrw_device_class_init (DvdRwDeviceClass *c)
248 {
249 DeviceClass *device_class = DEVICE_CLASS(c);
250 GObjectClass *g_object_class = G_OBJECT_CLASS(c);
251
252 device_class->open_device = dvdrw_device_open_device;
253 device_class->read_label = dvdrw_device_read_label;
254 device_class->start = dvdrw_device_start;
255 device_class->finish = dvdrw_device_finish;
256
257 g_object_class->finalize = dvdrw_device_finalize;
258
259 device_class_register_property(device_class, PROPERTY_DVDRW_MOUNT_POINT,
260 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
261 device_simple_property_get_fn,
262 dvdrw_device_set_mount_point_fn);
263
264 device_class_register_property(device_class, PROPERTY_DVDRW_KEEP_CACHE,
265 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
266 device_simple_property_get_fn,
267 dvdrw_device_set_keep_cache_fn);
268
269 device_class_register_property(device_class, PROPERTY_DVDRW_UNLABELLED_WHEN_UNMOUNTABLE,
270 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
271 device_simple_property_get_fn,
272 dvdrw_device_set_unlabelled_when_unmountable_fn);
273
274 device_class_register_property(device_class, PROPERTY_DVDRW_GROWISOFS_COMMAND,
275 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
276 device_simple_property_get_fn,
277 dvdrw_device_set_growisofs_command_fn);
278
279 device_class_register_property(device_class, PROPERTY_DVDRW_MOUNT_COMMAND,
280 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
281 device_simple_property_get_fn,
282 dvdrw_device_set_mount_command_fn);
283
284 device_class_register_property(device_class, PROPERTY_DVDRW_UMOUNT_COMMAND,
285 PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
286 device_simple_property_get_fn,
287 dvdrw_device_set_umount_command_fn);
288 }
289
290 static void
291 dvdrw_device_init (DvdRwDevice *self)
292 {
293 Device *dself = DEVICE(self);
294 GValue val;
295
296 self->dvdrw_device = NULL;
297 self->cache_dir = NULL;
298 self->cache_data = NULL;
299 self->mount_point = NULL;
300 self->mount_data = NULL;
301 self->mounted = FALSE;
302 self->keep_cache = FALSE;
303 self->growisofs_command = NULL;
304 self->mount_command = NULL;
305 self->umount_command = NULL;
306
307 bzero(&val, sizeof(val));
308
309 g_value_init(&val, G_TYPE_BOOLEAN);
310 g_value_set_boolean(&val, FALSE);
311 device_set_simple_property(dself, PROPERTY_APPENDABLE,
312 &val, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
313 g_value_unset(&val);
314
315 g_value_init(&val, G_TYPE_BOOLEAN);
316 g_value_set_boolean(&val, FALSE);
317 device_set_simple_property(dself, PROPERTY_PARTIAL_DELETION,
318 &val, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
319 g_value_unset(&val);
320
321 g_value_init(&val, G_TYPE_BOOLEAN);
322 g_value_set_boolean(&val, FALSE);
323 device_set_simple_property(dself, PROPERTY_FULL_DELETION,
324 &val, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
325 g_value_unset(&val);
326
327 g_value_init(&val, G_TYPE_BOOLEAN);
328 g_value_set_boolean(&val, TRUE);
329 device_set_simple_property(dself, PROPERTY_LEOM,
330 &val, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
331 g_value_unset(&val);
332 }
333
334 static gboolean
335 dvdrw_device_set_mount_point_fn(Device *dself, DevicePropertyBase *base,
336 GValue *val, PropertySurety surety, PropertySource source)
337 {
338 DvdRwDevice *self = DVDRW_DEVICE(dself);
339
340 amfree(self->mount_point);
341 amfree(self->mount_data);
342
343 self->mount_point = g_value_dup_string(val);
344 self->mount_data = g_strconcat(self->mount_point, "/data/", NULL);
345
346 device_clear_volume_details(dself);
347
348 return device_simple_property_set_fn(dself, base, val, surety, source);
349 }
350
351 static gboolean
352 dvdrw_device_set_keep_cache_fn(Device *dself, DevicePropertyBase *base,
353 GValue *val, PropertySurety surety, PropertySource source)
354 {
355 DvdRwDevice *self = DVDRW_DEVICE(dself);
356
357 self->keep_cache = g_value_get_boolean(val);
358
359 return device_simple_property_set_fn(dself, base, val, surety, source);
360 }
361
362 static gboolean
363 dvdrw_device_set_unlabelled_when_unmountable_fn(Device *dself, DevicePropertyBase *base,
364 GValue *val, PropertySurety surety, PropertySource source)
365 {
366 DvdRwDevice *self = DVDRW_DEVICE(dself);
367
368 self->unlabelled_when_unmountable = g_value_get_boolean(val);
369
370 return device_simple_property_set_fn(dself, base, val, surety, source);
371 }
372
373 static gboolean
374 dvdrw_device_set_growisofs_command_fn(Device *dself, DevicePropertyBase *base,
375 GValue *val, PropertySurety surety, PropertySource source)
376 {
377 DvdRwDevice *self = DVDRW_DEVICE(dself);
378
379 self->growisofs_command = g_value_dup_string(val);
380
381 return device_simple_property_set_fn(dself, base, val, surety, source);
382 }
383
384 static gboolean
385 dvdrw_device_set_mount_command_fn(Device *dself, DevicePropertyBase *base,
386 GValue *val, PropertySurety surety, PropertySource source)
387 {
388 DvdRwDevice *self = DVDRW_DEVICE(dself);
389
390 self->mount_command = g_value_dup_string(val);
391
392 return device_simple_property_set_fn(dself, base, val, surety, source);
393 }
394
395 static gboolean
396 dvdrw_device_set_umount_command_fn(Device *dself, DevicePropertyBase *base,
397 GValue *val, PropertySurety surety, PropertySource source)
398 {
399 DvdRwDevice *self = DVDRW_DEVICE(dself);
400
401 self->umount_command = g_value_dup_string(val);
402
403 return device_simple_property_set_fn(dself, base, val, surety, source);
404 }
405
406 static void
407 dvdrw_device_open_device(Device *dself, char *device_name, char *device_type, char *device_node)
408 {
409 DvdRwDevice *self = DVDRW_DEVICE(dself);
410 DeviceClass *parent_class = DEVICE_CLASS(g_type_class_peek_parent(DVDRW_DEVICE_GET_CLASS(dself)));
411 GValue val;
412 char *colon;
413
414 g_debug("Opening device: %s", device_node);
415
416 bzero(&val, sizeof(val));
417
418 colon = index(device_node, ':');
419 if (!colon) {
420 device_set_error(dself,
421 g_strdup(_("DVDRW device requires cache directory and DVD-RW device separated by a colon (:) in tapedev")),
422 DEVICE_STATUS_DEVICE_ERROR);
423 return;
424 }
425
426 self->cache_dir = g_strndup(device_node, colon - device_node);
427 self->cache_data = g_strconcat(self->cache_dir, "/data/", NULL);
428 self->dvdrw_device = g_strdup(colon + 1);
429
430 parent_class->open_device(dself, device_name, device_type, device_node);
431 }
432
433 static DeviceStatusFlags
434 dvdrw_device_read_label(Device *dself)
435 {
436 DvdRwDevice *self = DVDRW_DEVICE(dself);
437 VfsDevice *vself = VFS_DEVICE(dself);
438 gboolean mounted = FALSE;
439 DeviceStatusFlags status;
440 struct stat dir_status;
441 DeviceClass *parent_class = DEVICE_CLASS(g_type_class_peek_parent(DVDRW_DEVICE_GET_CLASS(dself)));
442
443 g_debug("Reading label from media at %s", self->mount_point);
444
445 if (device_in_error(dself)) return DEVICE_STATUS_DEVICE_ERROR;
446 if (!check_readable(self)) return DEVICE_STATUS_DEVICE_ERROR;
447
448 if (!self->mounted) {
449 status = mount_disc(self, !self->unlabelled_when_unmountable);
450 if (status != DEVICE_STATUS_SUCCESS) {
451 /* Not mountable. May be freshly formatted or corrupted, drive may be empty. */
452 return (self->unlabelled_when_unmountable)
453 ? DEVICE_STATUS_VOLUME_UNLABELED
454 : status;
455 }
456 mounted = TRUE;
457 }
458
459 if ((stat(self->mount_data, &dir_status) < 0) && (errno == ENOENT)) {
460 /* No data directory, consider the DVD unlabelled */
461 g_debug("Media contains no data directory and therefore no label");
462 unmount_disc(self);
463
464 return DEVICE_STATUS_VOLUME_UNLABELED;
465 }
466
467 amfree(vself->dir_name);
468 vself->dir_name = g_strdup(self->mount_data);
469 status = parent_class->read_label(dself);
470
471 if (mounted) {
472 unmount_disc(self);
473 }
474
475 return status;
476 }
477
478 static gboolean
479 dvdrw_device_start(Device *dself, DeviceAccessMode mode, char *label, char *timestamp)
480 {
481 DvdRwDevice *self = DVDRW_DEVICE(dself);
482 VfsDevice *vself = VFS_DEVICE(dself);
483 DeviceClass *parent_class = DEVICE_CLASS(g_type_class_peek_parent(DVDRW_DEVICE_GET_CLASS(dself)));
484
485 g_debug("Start DVDRW device");
486
487 if (device_in_error(dself)) return FALSE;
488 if (!check_access_mode(self, mode)) return FALSE;
489
490 dself->access_mode = mode;
491
492 /* We'll replace this with our own value */
493 amfree(vself->dir_name);
494
495 if (mode == ACCESS_READ) {
496 if (mount_disc(self, TRUE) != DEVICE_STATUS_SUCCESS) {
497 return FALSE;
498 }
499
500 vself->dir_name = g_strdup(self->mount_data);
501 } else if (mode == ACCESS_WRITE) {
502 vself->dir_name = g_strdup(self->cache_data);
503 }
504
505 return parent_class->start(dself, mode, label, timestamp);
506 }
507
508 static gboolean
509 dvdrw_device_finish(Device *dself)
510 {
511 DvdRwDevice *self = DVDRW_DEVICE(dself);
512 VfsDevice *vself = VFS_DEVICE(dself);
513 gboolean result;
514 DeviceClass *parent_class = DEVICE_CLASS(g_type_class_peek_parent(DVDRW_DEVICE_GET_CLASS(dself)));
515 DeviceAccessMode mode;
516
517 g_debug("Finish DVDRW device");
518
519 /* Save access mode before parent class messes with it */
520 mode = dself->access_mode;
521
522 result = parent_class->finish(dself);
523
524 if (mode == ACCESS_READ) {
525 unmount_disc(self);
526 }
527
528 if (!result || device_in_error(dself)) {
529 return FALSE;
530 }
531
532 if (mode == ACCESS_WRITE) {
533 result = burn_disc(self);
534
535 if (result && !self->keep_cache) {
536 delete_vfs_files(vself);
537 }
538
539 return result;
540 }
541
542 return TRUE;
543 }
544
545 static gboolean
546 burn_disc(DvdRwDevice *self)
547 {
548 gint status;
549
550 char *burn_argv[] = {NULL, "-use-the-force-luke",
551 "-Z", self->dvdrw_device,
552 "-J", "-R", "-pad", "-quiet",
553 self->cache_dir, NULL};
554
555 if (self->growisofs_command == NULL) {
556 burn_argv[0] = "growisofs";
557 } else {
558 burn_argv[0] = self->growisofs_command;
559 }
560
561 g_debug("Burning media in %s", self->dvdrw_device);
562 if (execute_command(self, burn_argv, &status) != DEVICE_STATUS_SUCCESS) {
563 return FALSE;
564 }
565 g_debug("Burn completed successfully");
566
567 return TRUE;
568 }
569
570 static void
571 dvdrw_device_finalize(GObject *gself)
572 {
573 DvdRwDevice *self = DVDRW_DEVICE(gself);
574 GObjectClass *parent_class = G_OBJECT_CLASS(g_type_class_peek_parent(DVDRW_DEVICE_GET_CLASS(gself)));
575
576 if (parent_class->finalize) {
577 parent_class->finalize(gself);
578 }
579
580 amfree(self->dvdrw_device);
581
582 amfree(self->cache_dir);
583 amfree(self->cache_data);
584 amfree(self->mount_point);
585 amfree(self->mount_data);
586 amfree(self->growisofs_command);
587 amfree(self->mount_command);
588 amfree(self->umount_command);
589 }
590
591 static gboolean
592 check_access_mode(DvdRwDevice *self, DeviceAccessMode mode)
593 {
594 Device *dself = DEVICE(self);
595
596 if (mode == ACCESS_READ) {
597 return check_readable(self);
598 } else if (mode == ACCESS_WRITE) {
599 return TRUE;
600 }
601
602 device_set_error(dself,
603 g_strdup(_("DVDRW device can only be opened in READ or WRITE mode")),
604 DEVICE_STATUS_DEVICE_ERROR);
605
606 return FALSE;
607 }
608
609 static gboolean
610 check_readable(DvdRwDevice *self)
611 {
612 Device *dself = DEVICE(self);
613 GValue value;
614 bzero(&value, sizeof(value));
615
616 if (! device_get_simple_property(dself, PROPERTY_DVDRW_MOUNT_POINT, &value, NULL, NULL)) {
617 device_set_error(dself,
618 g_strdup(_("DVDRW device requires DVDRW_MOUNT_POINT to open device for reading")),
619 DEVICE_STATUS_DEVICE_ERROR);
620
621 return FALSE;
622 }
623
624 return TRUE;
625 }
626
627 static DeviceStatusFlags
628 mount_disc(DvdRwDevice *self, gboolean report_error)
629 {
630 Device *dself = DEVICE(self);
631 gchar *mount_argv[] = { NULL, self->mount_point, NULL };
632 DeviceStatusFlags status;
633
634 if (self->mounted) {
635 return DEVICE_STATUS_SUCCESS;
636 }
637
638 if (self->mount_command == NULL) {
639 mount_argv[0] = "mount";
640 } else {
641 mount_argv[0] = self->mount_command;
642 }
643
644 g_debug("Mounting media at %s", self->mount_point);
645 status = execute_command(report_error ? self : NULL, mount_argv, NULL);
646 if (status != DEVICE_STATUS_SUCCESS) {
647 /* Wait a few seconds and try again - The tray may still be out after burning */
648 sleep(3);
649 if (execute_command(report_error ? self : NULL, mount_argv, NULL) == DEVICE_STATUS_SUCCESS) {
650 /* Clear error */
651 device_set_error(dself, NULL, DEVICE_STATUS_SUCCESS);
652 self->mounted = TRUE;
653 return DEVICE_STATUS_SUCCESS;
654 } else {
655 return status;
656 }
657 }
658
659 self->mounted = TRUE;
660 return DEVICE_STATUS_SUCCESS;
661 }
662
663 static void
664 unmount_disc(DvdRwDevice *self)
665 {
666 gchar *unmount_argv[] = { NULL, self->mount_point, NULL };
667 DeviceStatusFlags status;
668
669 if (! self->mounted) {
670 return;
671 }
672
673 if (self->umount_command == NULL) {
674 unmount_argv[0] = "umount";
675 } else {
676 unmount_argv[0] = self->umount_command;
677 }
678
679 g_debug("Unmounting media at %s", self->mount_point);
680 status = execute_command(NULL, unmount_argv, NULL);
681 if (status == DEVICE_STATUS_SUCCESS) {
682 self->mounted = FALSE;
683 }
684 }
685
686 static DeviceStatusFlags
687 execute_command(DvdRwDevice *self, gchar **argv, gint *result)
688 {
689 Device *dself = DEVICE(self);
690 gchar *std_output = NULL;
691 gchar *std_error = NULL;
692 gint errnum = 0;
693 GError *error = NULL;
694 gboolean success;
695
696 /* g_debug("Executing: %s", argv[0]); */
697
698 g_spawn_sync(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
699 &std_output, &std_error, &errnum, &error);
700
701 /* g_debug("Execution complete"); */
702
703 if (WIFSIGNALED(errnum)) {
704 success = FALSE;
705 } else if (WIFEXITED(errnum)) {
706 success = (WEXITSTATUS(errnum) == 0);
707 } else {
708 success = FALSE;
709 }
710
711 if (!success) {
712 gchar *error_message = g_strdup_printf(_("DVDRW device cannot execute '%s': %s (status: %d) (stderr: %s)"),
713 argv[0], error ? error->message : _("Unknown error"), errnum, std_error ? std_error: "No stderr");
714
715 if (dself != NULL) {
716 device_set_error(dself, error_message, DEVICE_STATUS_DEVICE_ERROR);
717 }
718
719 g_free(error_message);
720
721 if (std_output) {
722 g_free(std_output);
723 }
724
725 if (std_error) {
726 g_free(std_error);
727 }
728
729 if (error) {
730 g_error_free(error);
731 }
732
733 if (result != NULL) {
734 *result = errnum;
735 }
736
737 return DEVICE_STATUS_DEVICE_ERROR;
738 }
739
740 return DEVICE_STATUS_SUCCESS;
741 }