/[soft]/perl-Hal-Cdroms/trunk/lib/Hal/Cdroms.pm
ViewVC logotype

Diff of /perl-Hal-Cdroms/trunk/lib/Hal/Cdroms.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 6134 by tv, Mon Oct 1 16:17:05 2012 UTC revision 6135 by tv, Mon Oct 1 16:17:07 2012 UTC
# Line 28  Hal::Cdroms - access cdroms through HAL Line 28  Hal::Cdroms - access cdroms through HAL
28    
29    my $cdroms = Hal::Cdroms->new;    my $cdroms = Hal::Cdroms->new;
30    
31    foreach my $hal_path ($cdroms->list) {    foreach my $udisks_path ($cdroms->list) {
32       my $m = $cdroms->get_mount_point($hal_path);       my $m = $cdroms->get_mount_point($udisks_path);
33       print "$hal_path ", $m ? "is mounted in $m" : "is not mounted", "\n";       print "$udisks_path ", $m ? "is mounted in $m" : "is not mounted", "\n";
34    }    }
35    
36    my $hal_path = $cdroms->wait_for_insert;    my $udisks_path = $cdroms->wait_for_insert;
37    my $m = $cdroms->mount($hal_path);    my $m = $cdroms->mount($udisks_path);
38    print "$hal_path is now mounted in $m\n";    print "$udisks_path is now mounted in $m\n";
39    
40  =head1 DESCRIPTION  =head1 DESCRIPTION
41    
# Line 66  sub new { Line 66  sub new {
66    
67  =head2 $cdroms->list  =head2 $cdroms->list
68    
69  Returns the list of C<hal_path> of the cdroms (mounted or not).  Returns the list of C<udisks_path> of the cdroms (mounted or not).
70    
71  =cut  =cut
72    
# Line 80  sub list { Line 80  sub list {
80      grep { _GetProperty(_get_device($o, $_), 'DeviceIsOpticalDisc') } @{$manager->EnumerateDevices};      grep { _GetProperty(_get_device($o, $_), 'DeviceIsOpticalDisc') } @{$manager->EnumerateDevices};
81  }  }
82    
83  =head2 $cdroms->get_mount_point($hal_path)  =head2 $cdroms->get_mount_point($udisks_path)
84    
85  Return the mount point associated to the C<hal_path>, or undef it is not mounted.  Return the mount point associated to the C<udisks_path>, or undef it is not mounted.
86    
87  =cut  =cut
88    
89  sub _get_udisks_device {  sub _get_udisks_device {
90      my ($o, $hal_path) = @_;      my ($o, $udisks_path) = @_;
91      $o->{service}->get_object($hal_path, "$dn.Device");      $o->{service}->get_object($udisks_path, "$dn.Device");
92  }  }
93    
94  sub _get_device {  sub _get_device {
95      my ($o, $hal_path) = @_;      my ($o, $udisks_path) = @_;
96      $o->{service}->get_object($hal_path, 'org.freedesktop.DBus.Properties');      $o->{service}->get_object($udisks_path, 'org.freedesktop.DBus.Properties');
97  }  }
98    
99  sub _get_volume {  sub _get_volume {
100      my ($o, $hal_path) = @_;      my ($o, $udisks_path) = @_;
101      $o->{service}->get_object($hal_path, "$dn.Device.Volume");      $o->{service}->get_object($udisks_path, "$dn.Device.Volume");
102  }  }
103    
104  sub _GetProperty {  sub _GetProperty {
# Line 107  sub _GetProperty { Line 107  sub _GetProperty {
107  }  }
108    
109  sub get_mount_point {  sub get_mount_point {
110      my ($o, $hal_path) = @_;      my ($o, $udisks_path) = @_;
111    
112      my $device = _get_device($o, $hal_path);      my $device = _get_device($o, $udisks_path);
113      eval { _GetProperty($device, 'DeviceIsMounted')      eval { _GetProperty($device, 'DeviceIsMounted')
114             && @{_GetProperty($device, 'DeviceMountPaths')}[0] };             && @{_GetProperty($device, 'DeviceMountPaths')}[0] };
115  }  }
# Line 125  sub _try { Line 125  sub _try {
125      }      }
126  }  }
127    
128  =head2 $cdroms->ensure_mounted($hal_path)  =head2 $cdroms->ensure_mounted($udisks_path)
129    
130  Mount the C<hal_path> if not already mounted.  Mount the C<udisks_path> if not already mounted.
131  Return the mount point associated to the C<hal_path>, or undef it cannot be mounted successfully (see $cdroms->{error}).  Return the mount point associated to the C<udisks_path>, or undef it cannot be mounted successfully (see $cdroms->{error}).
132    
133  =cut  =cut
134    
135  sub ensure_mounted {  sub ensure_mounted {
136      my ($o, $hal_path) = @_;      my ($o, $udisks_path) = @_;
137            
138      $o->get_mount_point($hal_path) # check if it is already mounted      $o->get_mount_point($udisks_path) # check if it is already mounted
139        || $o->mount($hal_path) # otherwise try to mount        || $o->mount($udisks_path) # otherwise try to mount
140        || $o->get_mount_point($hal_path); # checking wether a volume manager did it for us        || $o->get_mount_point($udisks_path); # checking wether a volume manager did it for us
141  }  }
142    
143    
144  =head2 $cdroms->mount_through_hal($hal_path)  =head2 $cdroms->mount_through_hal($udisks_path)
145    
146  Mount the C<hal_path> through HAL  Mount the C<udisks_path> through HAL
147  Return the mount point associated to the C<hal_path>, or undef it cannot be mounted successfully (see $cdroms->{error}).  Return the mount point associated to the C<udisks_path>, or undef it cannot be mounted successfully (see $cdroms->{error}).
148  If the cdrom is listed in fstab, HAL will refuse to mount it.  If the cdrom is listed in fstab, HAL will refuse to mount it.
149    
150  =cut  =cut
151    
152  sub mount_hal {  sub mount_hal {
153      my ($o, $hal_path) = @_;      my ($o, $udisks_path) = @_;
154    
155      my $device = _get_udisks_device($o, $hal_path);      my $device = _get_udisks_device($o, $udisks_path);
156    
157      my $mountpoint;      my $mountpoint;
158      _try($o, sub { $mountpoint = $device->FilesystemMount(undef, []) }) or return;      _try($o, sub { $mountpoint = $device->FilesystemMount(undef, []) }) or return;
159      $mountpoint;      $mountpoint;
160  }  }
161    
162  =head2 $cdroms->mount($hal_path)  =head2 $cdroms->mount($udisks_path)
163    
164  Mount the C<hal_path> through HAL or fallback to plain mount(8).  Mount the C<udisks_path> through HAL or fallback to plain mount(8).
165  Return the mount point associated to the C<hal_path>, or undef it cannot be mounted successfully (see $cdroms->{error})  Return the mount point associated to the C<udisks_path>, or undef it cannot be mounted successfully (see $cdroms->{error})
166    
167  =cut  =cut
168    
169  sub mount {  sub mount {
170      my ($o, $hal_path) = @_;      my ($o, $udisks_path) = @_;
171    
172      my $mntpoint = mount_hal($o, $hal_path);      my $mntpoint = mount_hal($o, $udisks_path);
173      if (!$mntpoint) {      if (!$mntpoint) {
174          # this usually means HAL refused to mount a cdrom listed in fstab          # this usually means HAL refused to mount a cdrom listed in fstab
175          my $dev = _GetProperty(_get_device($o, $hal_path), 'NativePath');          my $dev = _GetProperty(_get_device($o, $udisks_path), 'NativePath');
176          # try to get real path:          # try to get real path:
177          $dev =~ s!.*/!/dev/!;          $dev =~ s!.*/!/dev/!;
178          if (my $wanted = $dev && _rdev($dev)) {          if (my $wanted = $dev && _rdev($dev)) {
179              my ($fstab_dev) = grep { $wanted == _rdev($_) } _fstab_devices();              my ($fstab_dev) = grep { $wanted == _rdev($_) } _fstab_devices();
180              system("mount", $fstab_dev) == 0              system("mount", $fstab_dev) == 0
181                and $mntpoint = get_mount_point($o, $hal_path);                and $mntpoint = get_mount_point($o, $udisks_path);
182          }          }
183      }      }
184      $mntpoint;      $mntpoint;
# Line 193  sub _fstab_devices() { Line 193  sub _fstab_devices() {
193      map { /(\S+)/ } <$F>;      map { /(\S+)/ } <$F>;
194  }  }
195    
196  =head2 $cdroms->unmount($hal_path)  =head2 $cdroms->unmount($udisks_path)
197    
198  Unmount the C<hal_path>. Return true on success (see $cdroms->{error} on failure)  Unmount the C<udisks_path>. Return true on success (see $cdroms->{error} on failure)
199  If the cdrom is listed in not mounted by HAL, HAL will refuse to unmount it.  If the cdrom is listed in not mounted by HAL, HAL will refuse to unmount it.
200    
201  =cut  =cut
202    
203  sub unmount_hal {  sub unmount_hal {
204      my ($o, $hal_path) = @_;      my ($o, $udisks_path) = @_;
205    
206      my $volume = _get_udisks_device($o, $hal_path);      my $volume = _get_udisks_device($o, $udisks_path);
207      _try($o, sub { $volume->FilesystemUnmount([]) });      _try($o, sub { $volume->FilesystemUnmount([]) });
208  }  }
209    
210  =head2 $cdroms->unmount($hal_path)  =head2 $cdroms->unmount($udisks_path)
211    
212  Unmount the C<hal_path> through HAL or fallback on umount(8).  Unmount the C<udisks_path> through HAL or fallback on umount(8).
213  Return true on success (see $cdroms->{error} on failure)  Return true on success (see $cdroms->{error} on failure)
214    
215  =cut  =cut
216    
217  sub unmount {  sub unmount {
218      my ($o, $hal_path) = @_;      my ($o, $udisks_path) = @_;
219    
220      unmount_hal($o, $hal_path) and return 1;      unmount_hal($o, $udisks_path) and return 1;
221    
222      system('umount', get_mount_point($o, $hal_path)) == 0;      system('umount', get_mount_point($o, $udisks_path)) == 0;
223  }  }
224    
225  =head2 $cdroms->eject($hal_path)  =head2 $cdroms->eject($udisks_path)
226    
227  Ejects the C<hal_path>. Return true on success (see $cdroms->{error} on failure)  Ejects the C<udisks_path>. Return true on success (see $cdroms->{error} on failure)
228    
229  =cut  =cut
230    
231  sub eject {  sub eject {
232      my ($o, $hal_path) = @_;      my ($o, $udisks_path) = @_;
233    
234      my $volume = _get_udisks_device($o, $hal_path);      my $volume = _get_udisks_device($o, $udisks_path);
235      _try($o, sub { $volume->FilesystemUnmount([]); $volume->DriveEject([]) });      _try($o, sub { $volume->FilesystemUnmount([]); $volume->DriveEject([]) });
236  }  }
237    
238  =head2 $cdroms->wait_for_insert([$timeout])  =head2 $cdroms->wait_for_insert([$timeout])
239    
240  Waits until a cdrom is inserted.  Waits until a cdrom is inserted.
241  Returns the inserted C<hal_path> on success. Otherwise returns undef.  Returns the inserted C<udisks_path> on success. Otherwise returns undef.
242    
243  You can give an optional timeout in milliseconds.  You can give an optional timeout in milliseconds.
244    
# Line 260  sub wait_for_insert { Line 260  sub wait_for_insert {
260  =head2 $cdroms->wait_for_mounted([$timeout])  =head2 $cdroms->wait_for_mounted([$timeout])
261    
262  Waits until a cdrom is inserted and mounted by a volume manager (eg: gnome-volume-manager).  Waits until a cdrom is inserted and mounted by a volume manager (eg: gnome-volume-manager).
263  Returns the mounted C<hal_path> on success. Otherwise returns undef.  Returns the mounted C<udisks_path> on success. Otherwise returns undef.
264    
265  You can give an optional timeout in milliseconds.  You can give an optional timeout in milliseconds.
266    
# Line 276  sub wait_for_mounted { Line 276  sub wait_for_mounted {
276          my (undef, $modified_properties) = $msg->get_args_list;          my (undef, $modified_properties) = $msg->get_args_list;
277          grep { $_->[0] eq 'volume.is_mounted' } @$modified_properties or return;          grep { $_->[0] eq 'volume.is_mounted' } @$modified_properties or return;
278    
279          my $hal_path = $msg->get_path;          my $udisks_path = $msg->get_path;
280          my $device = _get_device($o, $hal_path);          my $device = _get_device($o, $udisks_path);
281    
282          eval { _GetProperty($device, 'DeviceIsMounted') } && $hal_path;          eval { _GetProperty($device, 'DeviceIsMounted') } && $udisks_path;
283      });      });
284  }  }
285    

Legend:
Removed from v.6134  
changed lines
  Added in v.6135

  ViewVC Help
Powered by ViewVC 1.1.30