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
|
package Hal::Cdroms;
our $VERSION = 0.01;
# 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 $hal_cdroms = Hal::Cdroms->new;
foreach my $hal_path ($hal_cdroms->list) {
my $m = $hal_cdroms->get_mount_point($hal_path);
print "$hal_path ", $m ? "is mounted in $m" : "is not mounted", "\n";
}
my $hal_path = $hal_cdroms->wait_for_insert;
my $m = $hal_cdroms->mount($hal_path);
print "$hal_path is now mounted in $m\n";
=head1 DESCRIPTION
Access cdroms through HAL and D-Bus.
=cut
# internal constant
my $hal_dn = 'org.freedesktop.Hal';
=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 $hal = $dbus->get_service($hal_dn);
bless { dbus => $dbus, hal => $hal }, $class;
}
=head2 $hal_cdroms->list
Returns the list of C<hal_path> of the cdroms (mounted or not).
=cut
sub list {
my ($o) = @_;
my $manager = $o->{hal}->get_object("/org/freedesktop/Hal/Manager",
"$hal_dn.Manager");
@{$manager->FindDeviceByCapability('volume.disc')};
}
=head2 $hal_cdroms->get_mount_point($hal_path)
Return the mount point associated to the C<hal_path>, or undef it is not mounted.
=cut
sub _get_device {
my ($o, $hal_path) = @_;
$o->{hal}->get_object($hal_path, "$hal_dn.Device");
}
sub _get_volume {
my ($o, $hal_path) = @_;
$o->{hal}->get_object($hal_path, "$hal_dn.Device.Volume");
}
sub get_mount_point {
my ($o, $hal_path) = @_;
my $device = _get_device($o, $hal_path);
eval { $device->GetProperty('volume.is_mounted')
&& $device->GetProperty('volume.mount_point') };
}
sub _try {
my ($o, $f) = @_;
if (eval { $f->(); 1 }) {
1;
} else {
$o->{error} = $@;
undef;
}
}
=head2 $hal_cdroms->ensure_mounted($hal_path)
Mount the C<hal_path> if not already mounted.
Return the mount point associated to the C<hal_path>, or undef it cannot be mounted successfully (see $hal_cdroms->{error}).
=cut
sub ensure_mounted {
my ($o, $hal_path) = @_;
$o->get_mount_point($hal_path) # check if it is already mounted
|| $o->mount($hal_path) # otherwise try to mount
|| $o->get_mount_point($hal_path); # checking wether a volume manager did it for us
}
=head2 $hal_cdroms->mount($hal_path)
Mount the C<hal_path>.
Return the mount point associated to the C<hal_path>, or undef it cannot be mounted successfully (see $hal_cdroms->{error})
=cut
sub mount {
my ($o, $hal_path) = @_;
my $device = _get_device($o, $hal_path);
my $volume = _get_volume($o, $hal_path);
my $fstype = $device->GetProperty('volume.fstype');
_try($o, sub { $volume->Mount("", $fstype, []) }) or return;
eval { $device->GetProperty('volume.mount_point') };
}
=head2 $hal_cdroms->unmount($hal_path)
Unmount the C<hal_path>. Return true on success (see $hal_cdroms->{error} on failure)
=cut
sub unmount {
my ($o, $hal_path) = @_;
my $volume = _get_volume($o, $hal_path);
_try($o, sub { $volume->Unmount([]) });
}
=head2 $hal_cdroms->eject($hal_path)
Ejects the C<hal_path>. Return true on success (see $hal_cdroms->{error} on failure)
=cut
sub eject {
my ($o, $hal_path) = @_;
my $volume = _get_volume($o, $hal_path);
_try($o, sub { $volume->Eject([]) });
}
=head2 $hal_cdroms->wait_for_insert([$timeout])
Waits until a cdrom is inserted.
Returns the inserted C<hal_path> on success. Otherwise returns undef.
You can give an optional timeout in milliseconds.
=cut
sub wait_for_insert {
my ($o, $o_timeout) = @_;
_reactor_wait($o->{dbus}, "$hal_dn.Manager", $o_timeout, sub {
my ($msg) = @_;
$msg->get_member eq 'DeviceAdded' && ($msg->get_args_list)[0];
});
}
=head2 $hal_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<hal_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}, "$hal_dn.Device", $o_timeout, sub {
my ($msg) = @_;
$msg->get_member eq 'PropertyModified' or return;
my (undef, $modified_properties) = $msg->get_args_list;
grep { $_->[0] eq 'volume.is_mounted' } @$modified_properties or return;
my $hal_path = $msg->get_path;
my $device = _get_device($o, $hal_path);
eval { $device->QueryCapability('volume.disc') &&
$device->GetProperty('volume.is_mounted') } && $hal_path;
});
}
sub _reactor_wait {
my ($dbus, $interface, $timeout, $check_found) = @_;
my $val;
my $reactor = Net::DBus::Reactor->main;
my $con = $dbus->get_connection;
$con->add_match("type='signal',interface='$interface'");
$con->add_filter(sub {
my ($_con, $msg) = @_;
if ($val = $check_found->($msg)) {
_reactor_shutdown($reactor);
}
});
if ($timeout) {
$reactor->add_timeout($timeout, Net::DBus::Callback->new(method => sub {
_reactor_shutdown($reactor);
}));
}
$reactor->run;
$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 {}));
}
=head1 AUTHOR
Pascal Rigaux <pixel@mandriva.com>
=cut
|