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
|
package Hal::Cdroms;
our $VERSION = 0.04;
# Copyright (C) 2008 Mandriva
#
# This program is free software; You can redistribute it and/or modify
# it under the same terms as Perl itself. Either:
#
# a) the GNU General Public License as published by the Free
# Software Foundation; either version 2, or (at your option) any
# later version,
#
# or
#
# b) the "Artistic License"
#
# The file "COPYING" distributed along with this file provides full
# details of the terms and conditions of the two licenses.
=head1 NAME
Hal::Cdroms - access cdroms through HAL and D-Bus
=head1 SYNOPSIS
use Hal::Cdroms;
my $cdroms = Hal::Cdroms->new;
foreach my $udisks_path ($cdroms->list) {
my $m = $cdroms->get_mount_point($udisks_path);
print "$udisks_path ", $m ? "is mounted in $m" : "is not mounted", "\n";
}
my $udisks_path = $cdroms->wait_for_insert;
my $m = $cdroms->mount($udisks_path);
print "$udisks_path is now mounted in $m\n";
=head1 DESCRIPTION
Access cdroms through HAL and D-Bus.
=cut
# internal constant
my $dn = 'org.freedesktop.UDisks2';
=head2 Hal::Cdroms->new
Creates the object
=cut
sub new {
my ($class) = @_;
require Net::DBus;
require Net::DBus::Reactor; # must be done before line below:
my $dbus = Net::DBus->system;
my $service = $dbus->get_service($dn);
bless { dbus => $dbus, service => $service }, $class;
}
=head2 $cdroms->list
Returns the list of C<udisks_path> of the cdroms (mounted or not).
=cut
sub list {
my ($o) = @_;
my $manager = $o->{service}->get_object('/org/freedesktop/UDisks2/Manager');
grep { _is_cdrom($o, $_); } @{$manager->GetBlockDevices(undef)};
}
=head2 $cdroms->get_mount_point($udisks_path)
Return the mount point associated to the C<udisks_path>, or undef it is not mounted.
=cut
sub _is_cdrom {
my ($o, $udisks_path) = @_;
my $device = _get_device($o, $udisks_path);
my $drive = _get_drive($o, $device);
return unless $drive && _get_property($drive, 'Drive', 'Optical');
eval { _get_property($device, 'Filesystem', 'MountPoints') };
}
sub _get_device {
my ($o, $udisks_path, $o_interface_name) = @_;
$o->{service}->get_object($udisks_path, $o_interface_name);
}
sub _get_drive {
my ($o, $device) = @_;
my $drive_path = _get_property($device, 'Block', 'Drive');
return if $drive_path eq '/';
$o->{service}->get_object($drive_path);
}
sub _get_property {
my ($device, $interface_name, $property_name) = @_;
$device->Get("$dn.$interface_name", $property_name);
}
sub get_mount_point {
my ($o, $udisks_path) = @_;
my $mounts = _get_mount_points($o, $udisks_path);
_int_array_to_string($$mounts[0]) if @{$mounts};
}
sub _get_mount_points {
my ($o, $udisks_path) = @_;
my $device = _get_device($o, $udisks_path);
eval { _get_property($device, 'Filesystem', 'MountPoints') } || [];
}
sub _int_array_to_string {
my ($array) = @_;
join('', map { $_ ? chr($_) : '' } @{$array});
}
sub _try {
my ($o, $f) = @_;
if (eval { $f->(); 1 }) {
1;
} else {
$o->{error} = $@;
undef;
}
}
=head2 $cdroms->ensure_mounted($udisks_path)
Mount the C<udisks_path> if not already mounted.
Return the mount point associated to the C<udisks_path>, or undef it cannot be mounted successfully (see $cdroms->{error}).
=cut
sub ensure_mounted {
my ($o, $udisks_path) = @_;
$o->get_mount_point($udisks_path) # check if it is already mounted
|| $o->mount($udisks_path) # otherwise try to mount
|| $o->get_mount_point($udisks_path); # checking wether a volume manager did it for us
}
=head2 $cdroms->mount_through_hal($udisks_path)
Mount the C<udisks_path> through HAL
Return the mount point associated to the C<udisks_path>, or undef it cannot be mounted successfully (see $cdroms->{error}).
If the cdrom is listed in fstab, HAL will refuse to mount it.
=cut
sub mount_hal {
my ($o, $udisks_path) = @_;
my $device = _get_device($o, $udisks_path, "$dn.Filesystem");
my $mountpoint;
_try($o, sub { $mountpoint = $device->Mount(undef) }) or return;
$mountpoint;
}
=head2 $cdroms->mount($udisks_path)
Mount the C<udisks_path> through HAL or fallback to plain mount(8).
Return the mount point associated to the C<udisks_path>, or undef it cannot be mounted successfully (see $cdroms->{error})
=cut
sub mount {
my ($o, $udisks_path) = @_;
my $mntpoint = mount_hal($o, $udisks_path);
if (!$mntpoint) {
# this usually means HAL refused to mount a cdrom listed in fstab
my $device = _get_device($o, $udisks_path);
my $dev = _int_array_to_string(_get_property($device, 'Block', 'Device'));
if (my $wanted = $dev && _rdev($dev)) {
my ($fstab_dev) = grep { $wanted == _rdev($_) } _fstab_devices();
system("mount", $fstab_dev) == 0
and $mntpoint = get_mount_point($o, $udisks_path);
}
}
$mntpoint;
}
sub _rdev {
my ($dev) = @_;
(stat($dev))[6];
}
sub _fstab_devices() {
open(my $F, '<', '/etc/fstab') or return;
map { /(\S+)/ } <$F>;
}
=head2 $cdroms->unmount($udisks_path)
Unmount the C<udisks_path>. Return true on success (see $cdroms->{error} on failure)
If the cdrom is listed in not mounted by HAL, HAL will refuse to unmount it.
=cut
sub unmount_hal {
my ($o, $udisks_path) = @_;
my $device = _get_device($o, $udisks_path, "$dn.Filesystem");
_try($o, sub { $device->Unmount(undef) });
}
=head2 $cdroms->unmount($udisks_path)
Unmount the C<udisks_path> through HAL or fallback on umount(8).
Return true on success (see $cdroms->{error} on failure)
=cut
sub unmount {
my ($o, $udisks_path) = @_;
unmount_hal($o, $udisks_path) and return 1;
system('umount', get_mount_point($o, $udisks_path)) == 0;
}
=head2 $cdroms->eject($udisks_path)
Ejects the C<udisks_path>. Return true on success (see $cdroms->{error} on failure)
=cut
sub eject {
my ($o, $udisks_path) = @_;
my $device = _get_device($o, $udisks_path);
my $drive = _get_drive($o, $device);
_try($o, sub { $device->as_interface("$dn.Filesystem")->Unmount(undef); $drive->Eject(undef) });
}
=head2 $cdroms->wait_for_insert([$timeout])
Waits until a cdrom is inserted.
Returns the inserted C<udisks_path> on success. Otherwise returns undef.
You can give an optional timeout in milliseconds.
=cut
sub wait_for_insert {
my ($o, $o_timeout) = @_;
return if $o->list;
_reactor_wait($o->{dbus}, $o_timeout, sub {
my ($msg) = @_;
return unless $msg->get_member eq 'InterfacesAdded';
my $udisks_path = ($msg->get_args_list)[0];
return unless $udisks_path =~ /block_devices/;
return unless _is_cdrom($o, $udisks_path);
$udisks_path;
});
}
=head2 $cdroms->wait_for_mounted([$timeout])
Waits until a cdrom is inserted and mounted by a volume manager (eg: gnome-volume-manager).
Returns the mounted C<udisks_path> on success. Otherwise returns undef.
You can give an optional timeout in milliseconds.
=cut
sub wait_for_mounted {
my ($o, $o_timeout) = @_;
_reactor_wait($o->{dbus}, $o_timeout, sub {
my ($msg) = @_;
return unless member($msg->get_member, 'InterfacesAdded', 'PropertiesChanged');
my $udisks_path = $msg->get_member eq 'InterfacesAdded' ? ($msg->get_args_list)[0] : $msg->get_path;
return unless $udisks_path =~ /block_devices/;
return unless _is_cdrom($o, $udisks_path);
return unless @{_get_mount_points($o, $udisks_path)} > 0;
$udisks_path;
});
}
sub _reactor_wait {
my ($dbus, $timeout, $check_found) = @_;
my $found_val;
my $reactor = Net::DBus::Reactor->main;
my $con = $dbus->get_connection;
$con->add_match("type='signal',sender='$dn'");
$con->add_filter(sub {
my ($_con, $msg) = @_;
if (my $val = $check_found->($msg)) {
$found_val = $val;
_reactor_shutdown($reactor);
}
});
if ($timeout) {
$reactor->add_timeout($timeout, Net::DBus::Callback->new(method => sub {
_reactor_shutdown($reactor);
}));
}
$reactor->run;
$found_val;
}
sub _reactor_shutdown {
my ($reactor) = @_;
$reactor->shutdown;
# ugly, but needed for shutdown to work...
$reactor->add_timeout(1, Net::DBus::Callback->new(method => sub {}));
}
sub member { my $e = shift; foreach (@_) { $e eq $_ and return 1 } 0 }
=head1 AUTHOR
Pascal Rigaux <pixel@mandriva.com>
=cut
|