blib/lib/Hal/Cdroms.pm

filter: Statementstmt 1.9% 2 / 104 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 25 Podpod 100% 10 / 10 totaltotal 6.2% 12 / 191 SCAR 64.4
CC
25
Cov
1%
__ANON__
42.0
_is_cdrom
20.0
__ANON__
20.0
CRAP 625.7
↑ Summary
1 package Hal::Cdroms;
2  
3 2 our $VERSION = 0.05;
4  
5 # Copyright (C) 2008 Mandriva
6 # Copyright (C) 2020 Mageia
7 #
8 # This program is free software; You can redistribute it and/or modify
9 # it under the same terms as Perl itself. Either:
10 #
11 # a) the GNU General Public License as published by the Free
12 # Software Foundation; either version 2, or (at your option) any
13 # later version,
14 #
15 # or
16 #
17 # b) the "Artistic License"
18 #
19 # The file "COPYING" distributed along with this file provides full
20 # details of the terms and conditions of the two licenses.
21  
22 =head1 NAME
23  
24 Hal::Cdroms - access removable media containing CD filesystems through UDisks2 and D-Bus
25  
26 =head1 SYNOPSIS
27  
28 use Hal::Cdroms;
29  
30 my $cdroms = Hal::Cdroms->new;
31  
32 foreach my $udisks_path ($cdroms->list) {
33 my $m = $cdroms->get_mount_point($udisks_path);
34 print "$udisks_path ", $m ? "is mounted in $m" : "is not mounted", "\n";
35 }
36  
37 my $udisks_path = $cdroms->wait_for_insert;
38 my $m = $cdroms->mount($udisks_path);
39 print "$udisks_path is now mounted in $m\n";
40  
41 =head1 DESCRIPTION
42  
43 Access removable media containing CD filesystems (iso9660 and udf) through
44 UDisks2 and D-Bus. This includes CD-ROMS, DVD-ROMS, and USB flash drives.
45  
46 =cut
47  
48 # internal constant
49 2 my $dn = 'org.freedesktop.UDisks2';
50  
51  
52 =head2 Hal::Cdroms->new
53  
54 Creates the object
55  
56 =cut
57  
58 sub new {
59 0 ▶ my ($class) = @_;
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
60  
61 0 require Net::DBus;
62 0 require Net::DBus::Reactor; # must be done before line below:
63 0 my $dbus = Net::DBus->system;
64 0 my $service = $dbus->get_service($dn);
65  
66 0 bless { dbus => $dbus, service => $service }, $class;
67 }
68  
69 =head2 $cdroms->list
70  
71 Return the list of C<udisks_path> of the removable media (mounted or not).
72  
73 =cut
74  
75 sub list {
76 0 ▶ my ($o) = @_;
Subroutine list: not called CC=1 SCAR=6.9
Pod documented
77  
78 0 my $manager = $o->{service}->get_object('/org/freedesktop/UDisks2/Manager');
79  
80 0 grep { _is_cdrom($o, $_); } @{$manager->GetBlockDevices(undef)};
81 }
82  
83 =head2 $cdroms->get_mount_point($udisks_path)
84  
85 Return the mount point associated to the C<udisks_path>, or undef it is not mounted.
86  
87 =cut
88  
89 sub _is_cdrom {
90 0 ▶ my ($o, $udisks_path) = @_;
Subroutine _is_cdrom: not called CC=4 SCAR=30.0
91 0 my $device = _get_device($o, $udisks_path);
92 0 my $drive = _get_drive($o, $device);
93 0 ▶ return unless $drive && _get_property($drive, 'Drive', 'Removable');
Branch: 1 branch
BranchTrueFalseTotal
unless $drive and _get_property($drive, "Drive", "Removable") 0 0 0
Condition0%0/3
$drive and _get_property($drive, "Drive", "Removable")
!ll&&!rl&&r
000
MC/DC0%0/2
$drive and _get_property($drive, "Drive", "Removable")
$drive _get_property($drive, "Drive", "Removable")
Truth table0%0/3
A and B
A= $driveB= _get_property($drive, "Drive", "Removable")
ABresult
0X0
100
111
94 0 ▶ return unless member(_get_property($device, 'Block', 'IdType'), 'iso9660', 'udf');
Branch: 1 branch
BranchTrueFalseTotal
unless member(_get_property($device, "Block", "IdType"), "iso9660", "udf") 0 0 0
95 0 eval { _get_property($device, 'Filesystem', 'MountPoints') };
96 }
97  
98 sub _get_device {
99 0 ▶ my ($o, $udisks_path, $o_interface_name) = @_;
Subroutine _get_device: not called CC=1 SCAR=6.9
100 0 $o->{service}->get_object($udisks_path, $o_interface_name);
101 }
102  
103 sub _get_drive {
104 0 ▶ my ($o, $device) = @_;
Subroutine _get_drive: not called CC=2 SCAR=17.9
105 0 my $drive_path = _get_property($device, 'Block', 'Drive');
106 0 ▶ return if $drive_path eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if $drive_path eq "/" 0 0 0
107 0 $o->{service}->get_object($drive_path);
108 }
109  
110 sub _get_property {
111 0 ▶ my ($device, $interface_name, $property_name) = @_;
Subroutine _get_property: not called CC=1 SCAR=6.9
112 0 $device->Get("$dn.$interface_name", $property_name);
113 }
114  
115 sub get_mount_point {
116 0 ▶ my ($o, $udisks_path) = @_;
Subroutine get_mount_point: not called CC=2 SCAR=17.9
Pod documented
117 0 my $mounts = _get_mount_points($o, $udisks_path);
118 0 ▶ _int_array_to_string($$mounts[0]) if @{$mounts};
Branch: 1 branch
BranchTrueFalseTotal
if @{$mounts;} 0 0 0
119 }
120  
121 sub _get_mount_points {
122 0 ▶ my ($o, $udisks_path) = @_;
Subroutine _get_mount_points: not called CC=2 SCAR=17.9
123 0 my $device = _get_device($o, $udisks_path);
124 0 ▶ eval { _get_property($device, 'Filesystem', 'MountPoints') } || [];
Branch: 1 branch
BranchTrueFalseTotal
eval { do { _get_property($device, "Filesystem", "MountPoints") } } or [] 0 0 0
125 }
126  
127 sub _int_array_to_string {
128 0 ▶ my ($array) = @_;
Subroutine _int_array_to_string: not called CC=2 SCAR=17.9
129 0 ▶ join('', map { $_ ? chr($_) : '' } @{$array});
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 0 0 0
130 }
131  
132 sub _try {
133 0 ▶ my ($o, $f) = @_;
Subroutine _try: not called CC=2 SCAR=17.9
134  
135 0 ▶ if (eval { $f->(); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { &$f(); 1 } }) { } 0 0 0
136 0 1;
137 } else {
138 0 $o->{error} = $@;
139 0 undef;
140 }
141 }
142  
143 =head2 $cdroms->ensure_mounted($udisks_path)
144  
145 Mount the C<udisks_path> if not already mounted.
146 Return the mount point associated to the C<udisks_path>, or undef it cannot be mounted successfully (see $cdroms->{error}).
147  
148 =cut
149  
150 sub ensure_mounted {
151 0 ▶ my ($o, $udisks_path) = @_;
Subroutine ensure_mounted: not called CC=3 SCAR=24.8
Pod documented
152
153 0 ▶ $o->get_mount_point($udisks_path) # check if it is already mounted
Branch: 1 branch
BranchTrueFalseTotal
$o->get_mount_point($udisks_path) or $o->mount($udisks_path) or $o->get_mount_point($udisks_path) 0 0 0
Condition0%0/3
$o->get_mount_point($udisks_path) or $o->mount($udisks_path)
l!l&&r!l&&!r
000
MC/DC0%0/2
$o->get_mount_point($udisks_path) or $o->mount($udisks_path)
$o->get_mount_point($udisks_path) $o->mount($udisks_path)
Truth table0%0/3
A or B
A= $o->get_mount_point($udisks_path)B= $o->mount($udisks_path)
ABresult
1X1
011
000
154 || $o->mount($udisks_path) # otherwise try to mount
155 || $o->get_mount_point($udisks_path); # checking wether a volume manager did it for us
156 }
157  
158  
159 =head2 $cdroms->mount($udisks_path)
160  
161 Mount the C<udisks_path> through UDisks2.
162 Return the mount point associated to the C<udisks_path>, or undef it cannot be mounted successfully (see $cdroms->{error}).
163  
164 =cut
165  
166 sub mount {
167 0 ▶ my ($o, $udisks_path) = @_;
Subroutine mount: not called CC=2 SCAR=17.9
Pod documented
168  
169 0 my $device = _get_device($o, $udisks_path, "$dn.Filesystem");
170  
171 0 my $mountpoint;
172 0 ▶ _try($o, sub { $mountpoint = $device->Mount(undef) }) or return;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
_try($o, sub { $mountpoint = $device->Mount(undef); } ) or return 0 0 0
173 0 $mountpoint;
174 }
175  
176 =head2 $cdroms->unmount($udisks_path)
177  
178 Unmount the C<udisks_path> through UDisks2.
179 Return true on success (see $cdroms->{error} on failure)
180  
181 =cut
182  
183 sub unmount {
184 0 ▶ my ($o, $udisks_path) = @_;
Subroutine unmount: not called CC=1 SCAR=6.9
Pod documented
185  
186 0 my $device = _get_device($o, $udisks_path, "$dn.Filesystem");
187 0 ▶ _try($o, sub { $device->Unmount(undef) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
188 }
189  
190 =head2 $cdroms->eject($udisks_path)
191  
192 Eject the C<udisks_path>. Return true on success (see $cdroms->{error} on failure).
193  
194 =cut
195  
196 sub eject {
197 0 ▶ my ($o, $udisks_path) = @_;
Subroutine eject: not called CC=1 SCAR=6.9
Pod documented
198  
199 0 my $device = _get_device($o, $udisks_path);
200 0 my $drive = _get_drive($o, $device);
201 0 ▶ _try($o, sub { $device->as_interface("$dn.Filesystem")->Unmount(undef); $drive->Eject(undef) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
202 }
203  
204 =head2 $cdroms->wait_for_insert([$timeout])
205  
206 Wait until media containing a CD filesystem is inserted.
207 Return the inserted C<udisks_path> on success. Otherwise return undef.
208  
209 You can give an optional timeout in milliseconds.
210  
211 =cut
212  
213 sub wait_for_insert {
214 0 ▶ my ($o, $o_timeout) = @_;
Subroutine wait_for_insert: not called CC=2 SCAR=17.9
Pod documented
215  
216 0 ▶ return if $o->list;
Branch: 1 branch
BranchTrueFalseTotal
if $o->list 0 0 0
217  
218 _reactor_wait($o->{dbus}, $o_timeout, sub {
219 0 ▶ my ($msg) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
220 0 ▶ return unless $msg->get_member eq 'InterfacesAdded';
Branch: 1 branch
BranchTrueFalseTotal
unless $msg->get_member eq "InterfacesAdded" 0 0 0
221 0 my $udisks_path = ($msg->get_args_list)[0];
222 0 ▶ return unless $udisks_path =~ /block_devices/;
Branch: 1 branch
BranchTrueFalseTotal
unless $udisks_path =~ /block_devices/ 0 0 0
223 0 ▶ return unless _is_cdrom($o, $udisks_path);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_cdrom($o, $udisks_path) 0 0 0
224 0 $udisks_path;
225 0 });
226 }
227  
228 =head2 $cdroms->wait_for_mounted([$timeout])
229  
230 Wait until media containing a CD filesystem is inserted and mounted by a volume manager (eg: gnome-volume-manager).
231 Return the mounted C<udisks_path> on success. Otherwise return undef.
232  
233 You can give an optional timeout in milliseconds.
234  
235 =cut
236  
237 sub wait_for_mounted {
238 0 ▶ my ($o, $o_timeout) = @_;
Subroutine wait_for_mounted: not called CC=1 SCAR=6.9
Pod documented
239  
240 _reactor_wait($o->{dbus}, $o_timeout, sub {
241 0 ▶ my ($msg) = @_;
Subroutine __ANON__: not called CC=6 SCAR=37.4
242 0 ▶ return unless member($msg->get_member, 'InterfacesAdded', 'PropertiesChanged');
Branch: 1 branch
BranchTrueFalseTotal
unless member($msg->get_member, "InterfacesAdded", "PropertiesChanged") 0 0 0
243 0 ▶ my $udisks_path = $msg->get_member eq 'InterfacesAdded' ? ($msg->get_args_list)[0] : $msg->get_path;
Branch: 1 branch
BranchTrueFalseTotal
$msg->get_member eq 'InterfacesAdded' ? : 0 0 0
244 0 ▶ return unless $udisks_path =~ /block_devices/;
Branch: 1 branch
BranchTrueFalseTotal
unless $udisks_path =~ /block_devices/ 0 0 0
245 0 ▶ return unless _is_cdrom($o, $udisks_path);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_cdrom($o, $udisks_path) 0 0 0
246 0 ▶ return unless @{_get_mount_points($o, $udisks_path)} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless @{_get_mount_points($o, $udisks_path);} > 0 0 0 0
247 0 $udisks_path;
248 0 });
249 }
250  
251 sub _reactor_wait {
252 0 ▶ my ($dbus, $timeout, $check_found) = @_;
Subroutine _reactor_wait: not called CC=2 SCAR=17.9
253  
254 0 my $found_val;
255 0 my $reactor = Net::DBus::Reactor->main;
256  
257 0 my $con = $dbus->get_connection;
258 0 $con->add_match("type='signal',sender='$dn'");
259 $con->add_filter(sub {
260 0 ▶ my ($_con, $msg) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
261  
262 0 ▶ if (my $val = $check_found->($msg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $val = &$check_found($msg)) 0 0 0
263 0 $found_val = $val;
264 0 $reactor->shutdown;
265 }
266 0 1;
267 0 });
268 0 ▶ if ($timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout) 0 0 0
269 $reactor->add_timeout($timeout, Net::DBus::Callback->new(method => sub {
270 0 ▶ $reactor->shutdown;
Subroutine __ANON__: not called CC=1 SCAR=6.9
271 0 }));
272 }
273 0 $reactor->run;
274  
275 0 $found_val;
276 }
277  
278 =head2 member(SCALAR, LIST)
279  
280 is the value in the list?
281  
282 =cut
283  
284 # From MDK::Common::DataStructure :
285 0 ▶sub member { my $e = shift; foreach (@_) { $e eq $_ and return 1 } 0 }
Subroutine member: not called CC=3 SCAR=24.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$e eq $_ and return 1 0 0 0
286  
287 =head1 AUTHOR
288  
289 Pascal Rigaux <pixel@mandriva.com>
290 Martin Whitaker <martinw@mageia.org>
291  
292 =cut
↑ Summary