1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* gvfs - extensions for gio
*
* Copyright (C) 2006-2008 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Author: David Zeuthen <davidz@redhat.com>
*/
#include <config.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include <glib.h>
#include <glib/gi18n-lib.h>
#include <gio/gio.h>
#include <gvfsdbusutils.h>
#include "gproxyvolumemonitor.h"
#include "gproxymount.h"
#include "gproxyvolume.h"
#include "gproxymountoperation.h"
/* Protects all fields of GProxyMount that can change */
G_LOCK_DEFINE_STATIC(proxy_mount);
struct _GProxyMount {
GObject parent;
GProxyVolumeMonitor *volume_monitor;
char *id;
char *name;
char *uuid;
char *volume_id;
gboolean can_unmount;
char **x_content_types;
GFile *root;
GIcon *icon;
gchar *sort_key;
};
static void g_proxy_mount_mount_iface_init (GMountIface *iface);
G_DEFINE_DYNAMIC_TYPE_EXTENDED (GProxyMount, g_proxy_mount, G_TYPE_OBJECT, 0,
G_IMPLEMENT_INTERFACE_DYNAMIC (G_TYPE_MOUNT,
g_proxy_mount_mount_iface_init))
static void
g_proxy_mount_finalize (GObject *object)
{
GProxyMount *mount;
mount = G_PROXY_MOUNT (object);
g_free (mount->id);
g_free (mount->name);
g_free (mount->uuid);
g_free (mount->volume_id);
g_strfreev (mount->x_content_types);
if (mount->icon != NULL)
g_object_unref (mount->icon);
if (mount->root != NULL)
g_object_unref (mount->root);
if (mount->volume_monitor != NULL)
g_object_unref (mount->volume_monitor);
g_free (mount->sort_key);
if (G_OBJECT_CLASS (g_proxy_mount_parent_class)->finalize)
(*G_OBJECT_CLASS (g_proxy_mount_parent_class)->finalize) (object);
}
static void
g_proxy_mount_class_init (GProxyMountClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->finalize = g_proxy_mount_finalize;
}
static void
g_proxy_mount_class_finalize (GProxyMountClass *klass)
{
}
static void
g_proxy_mount_init (GProxyMount *proxy_mount)
{
}
GProxyMount *
g_proxy_mount_new (GProxyVolumeMonitor *volume_monitor)
{
GProxyMount *mount;
mount = g_object_new (G_TYPE_PROXY_MOUNT, NULL);
mount->volume_monitor = g_object_ref (volume_monitor);
g_object_set_data (G_OBJECT (mount),
"g-proxy-mount-volume-monitor-name",
(gpointer) g_type_name (G_TYPE_FROM_INSTANCE (volume_monitor)));
return mount;
}
gboolean
g_proxy_mount_has_mount_path (GProxyMount *mount, const char *mount_path)
{
char *path;
gboolean result;
result = FALSE;
path = g_file_get_path (mount->root);
if (path != NULL)
{
if (strcmp (path, mount_path) == 0)
result = TRUE;
g_free (path);
}
return result;
}
/* string id
* string name
* string gicon_data
* string uuid
* string root_uri
* boolean can-unmount
* string volume-id
* array:string x-content-types
*/
void
g_proxy_mount_update (GProxyMount *mount,
DBusMessageIter *iter)
{
DBusMessageIter iter_struct;
DBusMessageIter iter_x_content_types;
const char *id;
const char *name;
const char *gicon_data;
const char *uuid;
const char *root_uri;
dbus_bool_t can_unmount;
const char *volume_id;
GPtrArray *x_content_types;
const gchar *sort_key;
dbus_message_iter_recurse (iter, &iter_struct);
dbus_message_iter_get_basic (&iter_struct, &id);
dbus_message_iter_next (&iter_struct);
dbus_message_iter_get_basic (&iter_struct, &name);
dbus_message_iter_next (&iter_struct);
dbus_message_iter_get_basic (&iter_struct, &gicon_data);
dbus_message_iter_next (&iter_struct);
dbus_message_iter_get_basic (&iter_struct, &uuid);
dbus_message_iter_next (&iter_struct);
dbus_message_iter_get_basic (&iter_struct, &root_uri);
dbus_message_iter_next (&iter_struct);
dbus_message_iter_get_basic (&iter_struct, &can_unmount);
dbus_message_iter_next (&iter_struct);
dbus_message_iter_get_basic (&iter_struct, &volume_id);
dbus_message_iter_next (&iter_struct);
x_content_types = g_ptr_array_new ();
dbus_message_iter_recurse (&iter_struct, &iter_x_content_types);
while (dbus_message_iter_get_arg_type (&iter_x_content_types) != DBUS_TYPE_INVALID)
{
const char *x_content_type;
dbus_message_iter_get_basic (&iter_x_content_types, &x_content_type);
dbus_message_iter_next (&iter_x_content_types);
g_ptr_array_add (x_content_types, (gpointer) x_content_type);
}
g_ptr_array_add (x_content_types, NULL);
dbus_message_iter_next (&iter_struct);
/* make sure we are backwards compat with old daemon instance */
sort_key = NULL;
if (dbus_message_iter_has_next (&iter_struct))
{
dbus_message_iter_get_basic (&iter_struct, &sort_key);
dbus_message_iter_next (&iter_struct);
/* TODO: decode expansion, once used */
}
if (mount->id != NULL && strcmp (mount->id, id) != 0)
{
g_warning ("id mismatch during update of mount");
goto out;
}
if (strlen (name) == 0)
name = NULL;
if (strlen (uuid) == 0)
uuid = NULL;
if (sort_key != NULL && strlen (sort_key) == 0)
sort_key = NULL;
/* out with the old */
g_free (mount->id);
g_free (mount->name);
g_free (mount->uuid);
g_free (mount->volume_id);
if (mount->icon != NULL)
g_object_unref (mount->icon);
g_strfreev (mount->x_content_types);
if (mount->root != NULL)
g_object_unref (mount->root);
g_free (mount->sort_key);
/* in with the new */
mount->id = g_strdup (id);
mount->name = g_strdup (name);
if (*gicon_data == 0)
mount->icon = NULL;
else
mount->icon = g_icon_new_for_string (gicon_data, NULL);
mount->uuid = g_strdup (uuid);
mount->root = g_file_new_for_uri (root_uri);
mount->can_unmount = can_unmount;
mount->volume_id = g_strdup (volume_id);
mount->x_content_types = g_strdupv ((char **) x_content_types->pdata);
mount->sort_key = g_strdup (sort_key);
out:
g_ptr_array_free (x_content_types, TRUE);
}
const char *
g_proxy_mount_get_id (GProxyMount *mount)
{
return mount->id;
}
static GFile *
g_proxy_mount_get_root (GMount *mount)
{
GProxyMount *proxy_mount = G_PROXY_MOUNT (mount);
GFile *root;
G_LOCK (proxy_mount);
root = proxy_mount->root != NULL ? g_object_ref (proxy_mount->root) : NULL;
G_UNLOCK (proxy_mount);
return root;
}
static GIcon *
g_proxy_mount_get_icon (GMount *mount)
{
GProxyMount *proxy_mount = G_PROXY_MOUNT (mount);
GIcon *icon;
G_LOCK (proxy_mount);
icon = proxy_mount->icon != NULL ? g_object_ref (proxy_mount->icon) : NULL;
G_UNLOCK (proxy_mount);
return icon;
}
static char *
g_proxy_mount_get_uuid (GMount *mount)
{
GProxyMount *proxy_mount = G_PROXY_MOUNT (mount);
char *uuid;
G_LOCK (proxy_mount);
uuid = g_strdup (proxy_mount->uuid);
G_UNLOCK (proxy_mount);
return uuid;
}
static char *
g_proxy_mount_get_name (GMount *mount)
{
GProxyMount *proxy_mount = G_PROXY_MOUNT (mount);
char *name;
G_LOCK (proxy_mount);
name = g_strdup (proxy_mount->name);
G_UNLOCK (proxy_mount);
return name;
}
static GDrive *
g_proxy_mount_get_drive (GMount *mount)
{
GProxyMount *proxy_mount = G_PROXY_MOUNT (mount);
GProxyVolume *volume;
GDrive *drive;
G_LOCK (proxy_mount);
volume = NULL;
if (proxy_mount->volume_id != NULL && strlen (proxy_mount->volume_id) > 0)
volume = g_proxy_volume_monitor_get_volume_for_id (proxy_mount->volume_monitor,
proxy_mount->volume_id);
G_UNLOCK (proxy_mount);
drive = NULL;
if (volume != NULL)
{
drive = g_volume_get_drive (G_VOLUME (volume));
g_object_unref (volume);
}
return drive;
}
static GVolume *
g_proxy_mount_get_volume (GMount *mount)
{
GProxyMount *proxy_mount = G_PROXY_MOUNT (mount);
GProxyVolume *volume;
G_LOCK (proxy_mount);
volume = NULL;
if (proxy_mount->volume_id != NULL && strlen (proxy_mount->volume_id) > 0)
volume = g_proxy_volume_monitor_get_volume_for_id (proxy_mount->volume_monitor,
proxy_mount->volume_id);
G_UNLOCK (proxy_mount);
return volume != NULL ? G_VOLUME (volume) : NULL;
}
static gboolean
g_proxy_mount_can_unmount (GMount *mount)
{
GProxyMount *proxy_mount = G_PROXY_MOUNT (mount);
gboolean res;
G_LOCK (proxy_mount);
res = proxy_mount->can_unmount;
G_UNLOCK (proxy_mount);
return res;
}
static gboolean
g_proxy_mount_can_eject (GMount *mount)
{
GDrive *drive;
gboolean can_eject;
can_eject = FALSE;
drive = g_proxy_mount_get_drive (mount);
if (drive != NULL)
{
can_eject = g_drive_can_eject (drive);
g_object_unref (drive);
}
return can_eject;
}
typedef struct {
GObject *object;
GAsyncReadyCallback callback;
gpointer user_data;
} EjectWrapperOp;
static void
eject_wrapper_callback (GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
EjectWrapperOp *data = user_data;
data->callback (data->object, res, data->user_data);
g_object_unref (data->object);
g_free (data);
}
static void
g_proxy_mount_eject_with_operation (GMount *mount,
GMountUnmountFlags flags,
GMountOperation *mount_operation,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GDrive *drive;
drive = g_proxy_mount_get_drive (mount);
if (drive != NULL)
{
EjectWrapperOp *data;
data = g_new0 (EjectWrapperOp, 1);
data->object = g_object_ref (mount);
data->callback = callback;
data->user_data = user_data;
g_drive_eject_with_operation (drive, flags, mount_operation, cancellable, eject_wrapper_callback, data);
g_object_unref (drive);
}
}
static gboolean
g_proxy_mount_eject_with_operation_finish (GMount *mount,
GAsyncResult *result,
GError **error)
{
GDrive *drive;
gboolean res;
res = TRUE;
drive = g_proxy_mount_get_drive (mount);
if (drive != NULL)
{
res = g_drive_eject_with_operation_finish (drive, result, error);
g_object_unref (drive);
}
return res;
}
static void
g_proxy_mount_eject (GMount *mount,
GMountUnmountFlags flags,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
g_proxy_mount_eject_with_operation (mount, flags, NULL, cancellable, callback, user_data);
}
static gboolean
g_proxy_mount_eject_finish (GMount *mount,
GAsyncResult *result,
GError **error)
{
return g_proxy_mount_eject_with_operation_finish (mount, result, error);
}
typedef struct {
GProxyMount *mount;
GAsyncReadyCallback callback;
gpointer user_data;
gchar *cancellation_id;
GCancellable *cancellable;
gulong cancelled_handler_id;
const gchar *mount_op_id;
} DBusOp;
static void
cancel_operation_reply_cb (DBusMessage *reply,
GError *error,
gpointer user_data)
{
if (error != NULL)
{
g_warning ("Error from CancelOperation(): %s", error->message);
}
}
static void
operation_cancelled (GCancellable *cancellable,
gpointer user_data)
{
DBusOp *data = user_data;
GSimpleAsyncResult *simple;
DBusConnection *connection;
DBusMessage *message;
const char *name;
G_LOCK (proxy_mount);
simple = g_simple_async_result_new_error (G_OBJECT (data->mount),
data->callback,
data->user_data,
G_IO_ERROR,
G_IO_ERROR_CANCELLED,
_("Operation was cancelled"));
g_simple_async_result_complete_in_idle (simple);
g_object_unref (simple);
/* Now tell the remote volume monitor that the op has been cancelled */
connection = g_proxy_volume_monitor_get_dbus_connection (data->mount->volume_monitor);
name = g_proxy_volume_monitor_get_dbus_name (data->mount->volume_monitor);
message = dbus_message_new_method_call (name,
"/org/gtk/Private/RemoteVolumeMonitor",
"org.gtk.Private.RemoteVolumeMonitor",
"CancelOperation");
dbus_message_append_args (message,
DBUS_TYPE_STRING,
&(data->cancellation_id),
DBUS_TYPE_INVALID);
G_UNLOCK (proxy_mount);
_g_dbus_connection_call_async (connection,
message,
-1,
(GAsyncDBusCallback) cancel_operation_reply_cb,
NULL);
dbus_message_unref (message);
dbus_connection_unref (connection);
}
static void
unmount_cb (DBusMessage *reply,
GError *error,
DBusOp *data)
{
if (data->cancelled_handler_id > 0)
g_signal_handler_disconnect (data->cancellable, data->cancelled_handler_id);
if (!g_cancellable_is_cancelled (data->cancellable))
{
GSimpleAsyncResult *simple;
if (error != NULL)
simple = g_simple_async_result_new_from_error (G_OBJECT (data->mount),
data->callback,
data->user_data,
error);
else
simple = g_simple_async_result_new (G_OBJECT (data->mount),
data->callback,
data->user_data,
NULL);
g_simple_async_result_complete (simple);
g_object_unref (simple);
}
g_proxy_mount_operation_destroy (data->mount_op_id);
g_object_unref (data->mount);
g_free (data->cancellation_id);
if (data->cancellable != NULL)
g_object_unref (data->cancellable);
g_free (data);
}
static void
g_proxy_mount_unmount_with_operation (GMount *mount,
GMountUnmountFlags flags,
GMountOperation *mount_operation,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GProxyMount *proxy_mount = G_PROXY_MOUNT (mount);
DBusConnection *connection;
const char *name;
DBusMessage *message;
DBusOp *data;
dbus_uint32_t _flags = flags;
G_LOCK (proxy_mount);
if (g_cancellable_is_cancelled (cancellable))
{
GSimpleAsyncResult *simple;
simple = g_simple_async_result_new_error (G_OBJECT (mount),
callback,
user_data,
G_IO_ERROR,
G_IO_ERROR_CANCELLED,
_("Operation was cancelled"));
g_simple_async_result_complete_in_idle (simple);
g_object_unref (simple);
G_UNLOCK (proxy_mount);
goto out;
}
data = g_new0 (DBusOp, 1);
data->mount = g_object_ref (mount);
data->callback = callback;
data->user_data = user_data;
data->mount_op_id = g_proxy_mount_operation_wrap (mount_operation, proxy_mount->volume_monitor);
if (cancellable != NULL)
{
data->cancellation_id = g_strdup_printf ("%p", cancellable);
data->cancellable = g_object_ref (cancellable);
data->cancelled_handler_id = g_signal_connect (data->cancellable,
"cancelled",
G_CALLBACK (operation_cancelled),
data);
}
else
{
data->cancellation_id = g_strdup ("");
}
connection = g_proxy_volume_monitor_get_dbus_connection (proxy_mount->volume_monitor);
name = g_proxy_volume_monitor_get_dbus_name (proxy_mount->volume_monitor);
message = dbus_message_new_method_call (name,
"/org/gtk/Private/RemoteVolumeMonitor",
"org.gtk.Private.RemoteVolumeMonitor",
"MountUnmount");
dbus_message_append_args (message,
DBUS_TYPE_STRING,
&(proxy_mount->id),
DBUS_TYPE_STRING,
&(data->cancellation_id),
DBUS_TYPE_UINT32,
&_flags,
DBUS_TYPE_STRING,
&(data->mount_op_id),
DBUS_TYPE_INVALID);
G_UNLOCK (proxy_mount);
_g_dbus_connection_call_async (connection,
message,
G_PROXY_VOLUME_MONITOR_DBUS_TIMEOUT, /* 30 minute timeout */
(GAsyncDBusCallback) unmount_cb,
data);
dbus_message_unref (message);
dbus_connection_unref (connection);
out:
;
}
static gboolean
g_proxy_mount_unmount_with_operation_finish (GMount *mount,
GAsyncResult *result,
GError **error)
{
if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result), error))
return FALSE;
return TRUE;
}
static void
g_proxy_mount_unmount (GMount *mount,
GMountUnmountFlags flags,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
g_proxy_mount_unmount_with_operation (mount, flags, NULL, cancellable, callback, user_data);
}
static gboolean
g_proxy_mount_unmount_finish (GMount *mount,
GAsyncResult *result,
GError **error)
{
return g_proxy_mount_unmount_with_operation_finish (mount, result, error);
}
static void
g_proxy_mount_guess_content_type (GMount *mount,
gboolean force_rescan,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GSimpleAsyncResult *simple;
/* TODO: handle force_rescan */
simple = g_simple_async_result_new (G_OBJECT (mount),
callback,
user_data,
NULL);
g_simple_async_result_complete_in_idle (simple);
g_object_unref (simple);
}
static char **
g_proxy_mount_guess_content_type_finish (GMount *mount,
GAsyncResult *result,
GError **error)
{
GProxyMount *proxy_mount = G_PROXY_MOUNT (mount);
return g_strdupv (proxy_mount->x_content_types);
}
static char **
g_proxy_mount_guess_content_type_sync (GMount *mount,
gboolean force_rescan,
GCancellable *cancellable,
GError **error)
{
GProxyMount *proxy_mount = G_PROXY_MOUNT (mount);
/* TODO: handle force_rescan */
return g_strdupv (proxy_mount->x_content_types);
}
static const gchar *
g_proxy_mount_get_sort_key (GMount *_mount)
{
GProxyMount *mount = G_PROXY_MOUNT (_mount);
return mount->sort_key;
}
static void
g_proxy_mount_mount_iface_init (GMountIface *iface)
{
iface->get_root = g_proxy_mount_get_root;
iface->get_name = g_proxy_mount_get_name;
iface->get_icon = g_proxy_mount_get_icon;
iface->get_uuid = g_proxy_mount_get_uuid;
iface->get_drive = g_proxy_mount_get_drive;
iface->get_volume = g_proxy_mount_get_volume;
iface->can_unmount = g_proxy_mount_can_unmount;
iface->can_eject = g_proxy_mount_can_eject;
iface->unmount = g_proxy_mount_unmount;
iface->unmount_finish = g_proxy_mount_unmount_finish;
iface->unmount_with_operation = g_proxy_mount_unmount_with_operation;
iface->unmount_with_operation_finish = g_proxy_mount_unmount_with_operation_finish;
iface->eject = g_proxy_mount_eject;
iface->eject_finish = g_proxy_mount_eject_finish;
iface->eject_with_operation = g_proxy_mount_eject_with_operation;
iface->eject_with_operation_finish = g_proxy_mount_eject_with_operation_finish;
iface->guess_content_type = g_proxy_mount_guess_content_type;
iface->guess_content_type_finish = g_proxy_mount_guess_content_type_finish;
iface->guess_content_type_sync = g_proxy_mount_guess_content_type_sync;
iface->get_sort_key = g_proxy_mount_get_sort_key;
}
void
g_proxy_mount_register (GIOModule *module)
{
g_proxy_mount_register_type (G_TYPE_MODULE (module));
}
|