Hal-Cdroms

 view release on metacpan or  search on metacpan

lib/Hal/Cdroms.pm  view on Meta::CPAN

package Hal::Cdroms;

use strict;

our $VERSION = 0.06;

# 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} = $@;



( run in 3.899 seconds using v1.01-cache-2.11-cpan-df04353d9ac )