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
|
package Hal::Cdroms;
use strict;
our $VERSION = 0.05;
# Copyright (C) 2008 Mandriva
# Copyright (C) 2020 Mageia
#
# 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 removable media containing CD filesystems through UDisks2 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 removable media containing CD filesystems (iso9660 and udf) through
UDisks2 and D-Bus. This includes CD-ROMS, DVD-ROMS, and USB flash drives.
=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
Return the list of C<udisks_path> of the removable media (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 if !($drive && _get_property($drive, 'Drive', 'Removable'));
return unless member(_get_property($device, 'Block', 'IdType'), 'iso9660', 'udf');
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($udisks_path)
Mount the C<udisks_path> through UDisks2.
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 $device = _get_device($o, $udisks_path, "$dn.Filesystem");
my $mountpoint;
_try($o, sub { $mountpoint = $device->Mount(undef) }) or return;
$mountpoint;
}
=head2 $cdroms->unmount($udisks_path)
Unmount the C<udisks_path> through UDisks2.
Return true on success (see $cdroms->{error} on failure)
=cut
sub unmount {
my ($o, $udisks_path) = @_;
my $device = _get_device($o, $udisks_path, "$dn.Filesystem");
_try($o, sub { $device->Unmount(undef) });
}
=head2 $cdroms->eject($udisks_path)
Eject 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])
Wait until media containing a CD filesystem is inserted.
Return the inserted C<udisks_path> on success. Otherwise return 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])
Wait until media containing a CD filesystem is inserted and mounted by a volume manager (eg: gnome-volume-manager).
Return the mounted C<udisks_path> on success. Otherwise return 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;
}
1;
});
if ($timeout) {
$reactor->add_timeout($timeout, Net::DBus::Callback->new(method => sub {
$reactor->shutdown;
}));
}
$reactor->run;
$found_val;
}
=head2 member(SCALAR, LIST)
is the value in the list?
=cut
# From MDK::Common::DataStructure :
sub member { my $e = shift; foreach (@_) { $e eq $_ and return 1 } 0 }
=head1 AUTHOR
Pascal Rigaux <pixel@mandriva.com>
Martin Whitaker <martinw@mageia.org>
=cut
|