File Coverage

lib/Sys/Filesystem/MountPoint.pm
Criterion Covered Total %
statement 37 40 92.5
branch 16 24 66.6
condition 5 11 45.4
subroutine 11 11 100.0
pod 4 5 80.0
total 73 91 80.2


line stmt bran cond sub pod time code
1             package Sys::Filesystem::MountPoint;
2 1     1   19999 use strict;
  1         4  
  1         46  
3 1     1   15 use vars qw($VERSION @EXPORT_OK %EXPORT_TAGS @ISA $DEBUG $errstr $_f);
  1         1  
  1         118  
4 1     1   7 use Exporter;
  1         6  
  1         49  
5 1     1   5 use Carp;
  1         2  
  1         89  
6 1     1   1069 use Sys::Filesystem;
  1         64452  
  1         719  
7             $VERSION = sprintf "%d.%02d", q$Revision: 1.2 $ =~ /(\d+)/g;
8             @ISA = qw/Exporter/;
9             @EXPORT_OK = qw(path_to_mount_point is_mount_point dev_to_mount_point to_mount_point);
10             %EXPORT_TAGS = ( all => \@EXPORT_OK );
11              
12 314 50   314 0 671 sub debug { $DEBUG or return 1; print STDERR "@_" }
  0         0  
13              
14             # Sys::Filesystem object
15 925   33 925   4713 sub _f { $_f ||= (Sys::Filesystem->new or confess("Can't init Sys::Filesystem???")) }
      66        
16              
17              
18             sub is_mount_point {
19 295 50   295 1 5353 $_[0] or confess('missing arg');
20 295 100       623 ( grep { $_[0] eq $_ } _f->filesystems ) ? $_[0] : undef;
  5015         19666  
21             }
22              
23              
24              
25             sub path_to_mount_point {
26 42 50   42 1 18226 my $arg = $_[0] or confess("missing arg");
27              
28 42         259 require Cwd; # deffinitely resolve symlinks!!!
29 42 100 50     2969 my $abs = Cwd::abs_path($arg)
30             or $errstr = "Cant resolve '$arg' with Cwd::abs_path()"
31             and return;
32              
33 40         133 debug("resolved to '$abs'");
34              
35            
36 40         55 my $subpath = $abs;
37 40         99 while($subpath){
38 274         2664 debug("testing subpath : $subpath");
39            
40 274 100       430 is_mount_point($subpath) and return $subpath;
41            
42 234 50       1006 last if $subpath eq '/'; # we hit root but not FS mnt (just in case).
43              
44 234 100       1714 $subpath=~s/^\/[^\/]+$/\// # change /this to /
45             or $subpath=~s/\/[^\/]+$// ;# change /this/that to /this
46             }
47              
48 0         0 $errstr="Can't get mount point for $abs";
49 0         0 return;
50             }
51              
52              
53             sub dev_to_mount_point {
54 35 50   35 1 2184 my $arg = $_[0] or confess("missing arg");
55              
56 35         52 for my $mnt ( _f->filesystems ){
57 595 50       7842 (_f->device($mnt) eq $arg) and return $mnt;
58             }
59 35         519 $errstr="Can't find mount point for $arg";
60 35         139 return;
61             }
62              
63              
64              
65             sub to_mount_point {
66 20 50   20 1 10515 my $arg = $_[0] or confess("missing arg");
67 20 50 33     41 is_mount_point($arg) || dev_to_mount_point($arg) || path_to_mount_point($arg);
68             }
69              
70              
71              
72              
73              
74             1;
75              
76             __END__