File Coverage

blib/lib/Linux/Proc/Mounts.pm
Criterion Covered Total %
statement 48 69 69.5
branch 9 18 50.0
condition 3 6 50.0
subroutine 7 18 38.8
pod 4 4 100.0
total 71 115 61.7


line stmt bran cond sub pod time code
1             package Linux::Proc::Mounts;
2              
3             our $VERSION = '0.02';
4              
5 1     1   24322 use strict;
  1         3  
  1         38  
6 1     1   6 use warnings;
  1         2  
  1         27  
7 1     1   7 use Carp;
  1         5  
  1         1173  
8              
9             sub read {
10 1     1 1 16 my ($class, %opts) = @_;
11 1         3 my $mnt = delete $opts{mnt};
12 1         3 my $pid = delete $opts{pid};
13 1         3 my $file = delete $opts{file};
14 1 50       4 %opts and croak "Unknown option(s) ". join(", ", sort keys %opts);
15              
16 1 50       5 unless (defined $file) {
17 1 50       4 $mnt = "/proc" unless defined $mnt;
18 1 50 33     94 croak "$mnt is not a proc filesystem" unless -d $mnt and (stat _)[12] == 0;
19 1 50       5 $mnt .= "/$pid" if defined $pid;
20 1         4 $file = "$mnt/mounts";
21             }
22 1 50       111 open my $fh, '<', $file
23             or croak "Unable to open '$file': $!";
24              
25 1         2 my @entries;
26 1         87 while (<$fh>) {
27 18         23 chomp;
28 18         88 my @entry = split;
29 18 50       43 if (@entry != 6) {
30 0         0 warn "invalid number of entries in $file line $.";
31 0         0 next;
32             }
33 18         55 $#entry = 3; # ignore the two dummy values at the end
34 18         75 s/\\([0-7]{1,3})/chr oct $1/g for @entry;
35 18         41 push @entry, _key($entry[1]);
36 18         36 push @entry, $.;
37 18         26 my $entry = \@entry;
38 18         40 bless $entry, 'Linux::Proc::Mounts::Entry';
39 18         74 push @entries, $entry;
40             }
41              
42 1         5 for my $i (1..$#entries) {
43 17         25 my $shadow = $entries[$i];
44             # my $re = quotemeta $shadow->[4];
45             # $re = qr/^$re/;
46             # for my $e (@entries[0..$i-1]) {
47             # $e->[6] = $shadow if $e->[4] =~ $re and not $e->[6];
48             # }
49 17         45 for my $e (@entries[0..$i-1]) {
50 153 100 66     395 $e->[6] = $shadow if $e->[4] eq $shadow->[4] and not $e->[6];
51             }
52             }
53 1         19 bless \@entries, $class;
54             }
55              
56             sub _key {
57 19     19   28 my $mnt = shift;
58 19         72 $mnt =~ s|/+|/|g;
59 19         62 $mnt =~ s|/?$|/|;
60 19         128 $mnt =~ s|([\0/])|\0$1|g;
61 19         53 $mnt;
62             }
63              
64             sub at {
65 1     1 1 7 my ($self, $at) = @_;
66 1         5 my $key = _key $at;
67 1         6 bless [grep { $_->[4] eq $key } @$self], ref $self;
  18         36  
68             }
69              
70             sub under {
71 0     0 1 0 my ($self, $under) = @_;
72 0         0 my $key = quotemeta _key $under;
73 0         0 my $re = qr/^$key/;
74 0         0 bless [grep $_->[4] =~ $re, @$self], ref $self;
75             }
76              
77             sub visible {
78 1     1 1 3 my $self = shift;
79 1         9 bless [grep !$_->[6], @$self], ref $self;
80             }
81              
82             package Linux::Proc::Mounts::Entry;
83              
84 0     0     sub _key { shift->[4] }
85 0     0     sub spec { shift->[0] }
86 0     0     sub file { shift->[1] }
87 0     0     sub fstype { shift->[2] }
88 0     0     sub opts { shift->[3] }
89 0     0     sub ix { shift->[5] }
90 0     0     sub shadower { shift->[6] }
91              
92             sub opts_hash {
93 0     0     my %h;
94 0           for (split /,/, shift->[3]) {
95 0 0         if (/(.*)=(.*)/) {
96 0           $h{$1} = $2;
97             }
98             else {
99 0           $h{$_} = 1;
100             }
101             }
102 0           \%h;
103             }
104              
105 0     0     sub is_ro { shift->[3] =~ /(?:^|,)ro(?:,|$)/ }
106              
107 0     0     sub is_rw { shift->[3] =~ /(?:^|,)rw(?:,|$)/ }
108              
109              
110             __END__