1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2006-2007 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
22 * Author: Alexander Larsson <alexl@redhat.com>
23 * David Zeuthen <davidz@redhat.com>
33 #include "gunixvolume.h"
34 #include "gunixmount.h"
35 #include "gunixmounts.h"
36 #include "gthemedicon.h"
38 #include "gvolumemonitor.h"
39 #include "gsimpleasyncresult.h"
49 GVolumeMonitor
*volume_monitor
;
50 GUnixMount
*mount
; /* owned by volume monitor */
57 char *identifier_type
;
63 static void g_unix_volume_volume_iface_init (GVolumeIface
*iface
);
65 #define g_unix_volume_get_type _g_unix_volume_get_type
66 G_DEFINE_TYPE_WITH_CODE (GUnixVolume
, g_unix_volume
, G_TYPE_OBJECT
,
67 G_IMPLEMENT_INTERFACE (G_TYPE_VOLUME
,
68 g_unix_volume_volume_iface_init
))
71 g_unix_volume_finalize (GObject
*object
)
75 volume
= G_UNIX_VOLUME (object
);
77 if (volume
->volume_monitor
!= NULL
)
78 g_object_unref (volume
->volume_monitor
);
81 _g_unix_mount_unset_volume (volume
->mount
, volume
);
83 g_object_unref (volume
->icon
);
84 g_free (volume
->name
);
85 g_free (volume
->mount_path
);
86 g_free (volume
->device_path
);
87 g_free (volume
->identifier
);
88 g_free (volume
->identifier_type
);
90 G_OBJECT_CLASS (g_unix_volume_parent_class
)->finalize (object
);
94 g_unix_volume_class_init (GUnixVolumeClass
*klass
)
96 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
98 gobject_class
->finalize
= g_unix_volume_finalize
;
102 g_unix_volume_init (GUnixVolume
*unix_volume
)
107 _g_unix_volume_new (GVolumeMonitor
*volume_monitor
,
108 GUnixMountPoint
*mountpoint
)
112 if (!(g_unix_mount_point_is_user_mountable (mountpoint
) ||
113 g_str_has_prefix (g_unix_mount_point_get_device_path (mountpoint
), "/vol/")) ||
114 g_unix_mount_point_is_loopback (mountpoint
))
117 volume
= g_object_new (G_TYPE_UNIX_VOLUME
, NULL
);
118 volume
->volume_monitor
= volume_monitor
!= NULL
? g_object_ref (volume_monitor
) : NULL
;
119 volume
->mount_path
= g_strdup (g_unix_mount_point_get_mount_path (mountpoint
));
120 volume
->device_path
= g_strdup (g_unix_mount_point_get_device_path (mountpoint
));
121 volume
->can_eject
= g_unix_mount_point_guess_can_eject (mountpoint
);
123 volume
->name
= g_unix_mount_point_guess_name (mountpoint
);
124 volume
->icon
= g_unix_mount_point_guess_icon (mountpoint
);
127 if (strcmp (g_unix_mount_point_get_fs_type (mountpoint
), "nfs") == 0)
129 volume
->identifier_type
= g_strdup (G_VOLUME_IDENTIFIER_KIND_NFS_MOUNT
);
130 volume
->identifier
= g_strdup (volume
->device_path
);
132 else if (g_str_has_prefix (volume
->device_path
, "LABEL="))
134 volume
->identifier_type
= g_strdup (G_VOLUME_IDENTIFIER_KIND_LABEL
);
135 volume
->identifier
= g_strdup (volume
->device_path
+ 6);
137 else if (g_str_has_prefix (volume
->device_path
, "UUID="))
139 volume
->identifier_type
= g_strdup (G_VOLUME_IDENTIFIER_KIND_UUID
);
140 volume
->identifier
= g_strdup (volume
->device_path
+ 5);
142 else if (g_path_is_absolute (volume
->device_path
))
144 volume
->identifier_type
= g_strdup (G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE
);
145 volume
->identifier
= g_strdup (volume
->device_path
);
152 _g_unix_volume_disconnected (GUnixVolume
*volume
)
156 _g_unix_mount_unset_volume (volume
->mount
, volume
);
157 volume
->mount
= NULL
;
162 _g_unix_volume_set_mount (GUnixVolume
*volume
,
165 if (volume
->mount
== mount
)
169 _g_unix_mount_unset_volume (volume
->mount
, volume
);
171 volume
->mount
= mount
;
173 /* TODO: Emit changed in idle to avoid locking issues */
174 g_signal_emit_by_name (volume
, "changed");
175 if (volume
->volume_monitor
!= NULL
)
176 g_signal_emit_by_name (volume
->volume_monitor
, "volume-changed", volume
);
180 _g_unix_volume_unset_mount (GUnixVolume
*volume
,
183 if (volume
->mount
== mount
)
185 volume
->mount
= NULL
;
186 /* TODO: Emit changed in idle to avoid locking issues */
187 g_signal_emit_by_name (volume
, "changed");
188 if (volume
->volume_monitor
!= NULL
)
189 g_signal_emit_by_name (volume
->volume_monitor
, "volume-changed", volume
);
194 g_unix_volume_get_icon (GVolume
*volume
)
196 GUnixVolume
*unix_volume
= G_UNIX_VOLUME (volume
);
197 return g_object_ref (unix_volume
->icon
);
201 g_unix_volume_get_name (GVolume
*volume
)
203 GUnixVolume
*unix_volume
= G_UNIX_VOLUME (volume
);
204 return g_strdup (unix_volume
->name
);
208 g_unix_volume_get_uuid (GVolume
*volume
)
214 g_unix_volume_can_mount (GVolume
*volume
)
220 g_unix_volume_can_eject (GVolume
*volume
)
222 GUnixVolume
*unix_volume
= G_UNIX_VOLUME (volume
);
223 return unix_volume
->can_eject
;
227 g_unix_volume_should_automount (GVolume
*volume
)
229 /* We automount all local volumes because we don't even
230 * make the internal stuff visible
236 g_unix_volume_get_drive (GVolume
*volume
)
242 g_unix_volume_get_mount (GVolume
*volume
)
244 GUnixVolume
*unix_volume
= G_UNIX_VOLUME (volume
);
246 if (unix_volume
->mount
!= NULL
)
247 return g_object_ref (unix_volume
->mount
);
254 _g_unix_volume_has_mount_path (GUnixVolume
*volume
,
255 const char *mount_path
)
257 return strcmp (volume
->mount_path
, mount_path
) == 0;
262 GUnixVolume
*unix_volume
;
263 GAsyncReadyCallback callback
;
265 GCancellable
*cancellable
;
267 GIOChannel
*error_channel
;
268 GSource
*error_channel_source
;
269 GString
*error_string
;
273 eject_mount_cb (GPid pid
,
277 EjectMountOp
*data
= user_data
;
278 GSimpleAsyncResult
*simple
;
280 if (WEXITSTATUS (status
) != 0)
283 error
= g_error_new_literal (G_IO_ERROR
,
285 data
->error_string
->str
);
286 simple
= g_simple_async_result_new_from_error (G_OBJECT (data
->unix_volume
),
290 g_error_free (error
);
294 simple
= g_simple_async_result_new (G_OBJECT (data
->unix_volume
),
300 g_simple_async_result_complete (simple
);
301 g_object_unref (simple
);
303 if (data
->error_channel_source
)
305 g_source_destroy (data
->error_channel_source
);
306 g_source_unref (data
->error_channel_source
);
308 g_io_channel_unref (data
->error_channel
);
309 g_string_free (data
->error_string
, TRUE
);
310 close (data
->error_fd
);
311 g_spawn_close_pid (pid
);
316 eject_mount_read_error (GIOChannel
*channel
,
317 GIOCondition condition
,
320 EjectMountOp
*data
= user_data
;
328 status
= g_io_channel_read_chars (channel
, buf
, sizeof (buf
), &bytes_read
, &error
);
329 if (status
== G_IO_STATUS_NORMAL
)
331 g_string_append_len (data
->error_string
, buf
, bytes_read
);
332 if (bytes_read
== sizeof (buf
))
335 else if (status
== G_IO_STATUS_EOF
)
336 g_string_append_len (data
->error_string
, buf
, bytes_read
);
337 else if (status
== G_IO_STATUS_ERROR
)
339 if (data
->error_string
->len
> 0)
340 g_string_append (data
->error_string
, "\n");
342 g_string_append (data
->error_string
, error
->message
);
343 g_error_free (error
);
345 if (data
->error_channel_source
)
347 g_source_unref (data
->error_channel_source
);
348 data
->error_channel_source
= NULL
;
357 eject_mount_do (GVolume
*volume
,
358 GCancellable
*cancellable
,
359 GAsyncReadyCallback callback
,
363 GUnixVolume
*unix_volume
= G_UNIX_VOLUME (volume
);
366 GSource
*child_watch
;
369 data
= g_new0 (EjectMountOp
, 1);
370 data
->unix_volume
= unix_volume
;
371 data
->callback
= callback
;
372 data
->user_data
= user_data
;
373 data
->cancellable
= cancellable
;
376 if (!g_spawn_async_with_pipes (NULL
, /* working dir */
379 G_SPAWN_DO_NOT_REAP_CHILD
|G_SPAWN_SEARCH_PATH
,
380 NULL
, /* child_setup */
381 NULL
, /* user_data for child_setup */
383 NULL
, /* standard_input */
384 NULL
, /* standard_output */
388 g_assert (error
!= NULL
);
392 data
->error_string
= g_string_new ("");
394 data
->error_channel
= g_io_channel_unix_new (data
->error_fd
);
395 g_io_channel_set_flags (data
->error_channel
, G_IO_FLAG_NONBLOCK
, &error
);
399 data
->error_channel_source
= g_io_create_watch (data
->error_channel
, G_IO_IN
);
400 g_source_set_callback (data
->error_channel_source
,
401 (GSourceFunc
) eject_mount_read_error
, data
, NULL
);
402 g_source_attach (data
->error_channel_source
, g_main_context_get_thread_default ());
404 child_watch
= g_child_watch_source_new (child_pid
);
405 g_source_set_callback (child_watch
, (GSourceFunc
) eject_mount_cb
, data
, NULL
);
406 g_source_attach (child_watch
, g_main_context_get_thread_default ());
407 g_source_unref (child_watch
);
412 GSimpleAsyncResult
*simple
;
413 simple
= g_simple_async_result_new_take_error (G_OBJECT (data
->unix_volume
),
417 g_simple_async_result_complete (simple
);
418 g_object_unref (simple
);
420 if (data
->error_string
!= NULL
)
421 g_string_free (data
->error_string
, TRUE
);
423 if (data
->error_channel
!= NULL
)
424 g_io_channel_unref (data
->error_channel
);
432 g_unix_volume_mount (GVolume
*volume
,
433 GMountMountFlags flags
,
434 GMountOperation
*mount_operation
,
435 GCancellable
*cancellable
,
436 GAsyncReadyCallback callback
,
439 GUnixVolume
*unix_volume
= G_UNIX_VOLUME (volume
);
440 char *argv
[] = { "mount", NULL
, NULL
};
442 if (unix_volume
->mount_path
!= NULL
)
443 argv
[1] = unix_volume
->mount_path
;
445 argv
[1] = unix_volume
->device_path
;
447 eject_mount_do (volume
, cancellable
, callback
, user_data
, argv
);
451 g_unix_volume_mount_finish (GVolume
*volume
,
452 GAsyncResult
*result
,
459 g_unix_volume_eject (GVolume
*volume
,
460 GMountUnmountFlags flags
,
461 GCancellable
*cancellable
,
462 GAsyncReadyCallback callback
,
465 GUnixVolume
*unix_volume
= G_UNIX_VOLUME (volume
);
466 char *argv
[] = { "eject", NULL
, NULL
};
468 argv
[1] = unix_volume
->device_path
;
470 eject_mount_do (volume
, cancellable
, callback
, user_data
, argv
);
474 g_unix_volume_eject_finish (GVolume
*volume
,
475 GAsyncResult
*result
,
482 g_unix_volume_get_identifier (GVolume
*volume
,
485 GUnixVolume
*unix_volume
= G_UNIX_VOLUME (volume
);
487 if (unix_volume
->identifier_type
!= NULL
&&
488 strcmp (kind
, unix_volume
->identifier_type
) == 0)
489 return g_strdup (unix_volume
->identifier
);
495 g_unix_volume_enumerate_identifiers (GVolume
*volume
)
497 GUnixVolume
*unix_volume
= G_UNIX_VOLUME (volume
);
500 if (unix_volume
->identifier_type
)
502 res
= g_new (gchar
*, 2);
503 res
[0] = g_strdup (unix_volume
->identifier_type
);
508 res
= g_new (gchar
*, 1);
516 g_unix_volume_volume_iface_init (GVolumeIface
*iface
)
518 iface
->get_name
= g_unix_volume_get_name
;
519 iface
->get_icon
= g_unix_volume_get_icon
;
520 iface
->get_uuid
= g_unix_volume_get_uuid
;
521 iface
->get_drive
= g_unix_volume_get_drive
;
522 iface
->get_mount
= g_unix_volume_get_mount
;
523 iface
->can_mount
= g_unix_volume_can_mount
;
524 iface
->can_eject
= g_unix_volume_can_eject
;
525 iface
->should_automount
= g_unix_volume_should_automount
;
526 iface
->mount_fn
= g_unix_volume_mount
;
527 iface
->mount_finish
= g_unix_volume_mount_finish
;
528 iface
->eject
= g_unix_volume_eject
;
529 iface
->eject_finish
= g_unix_volume_eject_finish
;
530 iface
->get_identifier
= g_unix_volume_get_identifier
;
531 iface
->enumerate_identifiers
= g_unix_volume_enumerate_identifiers
;