sort mount points by device_name/moint_point
[scpubgit/System-Introspector.git] / lib / System / Introspector / MountPoints.pm
1 package System::Introspector::MountPoints;
2 use Moo;
3
4 use System::Introspector::Util qw(
5     handle_from_file
6     transform_exceptions
7 );
8
9 sub gather {
10     my ($self) = @_;
11     return {
12         mtab => transform_exceptions {
13             return { entries
14                 => $self->_parse_tab_fh($self->_open_fh('/etc/mtab')) };
15         },
16         fstab => transform_exceptions {
17             return { entries
18                 => $self->_parse_tab_fh($self->_open_fh('/etc/fstab')) };
19         },
20     };
21 }
22
23 sub _open_fh {
24     my ($self, $file) = @_;
25     return handle_from_file $file;
26 }
27
28 sub _parse_tab_fh {
29     my ($self, $fh) = @_;
30     my @mounts;
31     while (defined( my $line = <$fh> )) {
32         chomp $line;
33         next if $line =~ m{^\s*$}
34              or $line =~ m{^\s*#};
35         my ($device, $point, $type, $opt, $dump, $pass)
36             = split m{\s+}, $line;
37         push @mounts, {
38             device_name => $device,
39             mount_point => $point,
40             fs_type     => $type,
41             dump_freq   => $dump,
42             pass_num    => $pass,
43             options     => {
44                 map {
45                     my ($name, $value) = split m{=}, $_, 2;
46                     $value = 1
47                         unless defined $value;
48                     ($name => $value);
49                 } split m{,}, $opt,
50             },
51         };
52     }
53     no warnings 'uninitialized';
54     return [ sort {
55         ($a->{device_name} cmp $b->{device_name})
56         ||
57         ($a->{mount_point} cmp $b->{mount_point})
58     } @mounts ];
59 }
60
61 1;
62
63 __END__
64
65 =head1 NAME
66
67 System::Introspector::MountPoints - Gather moint point information
68
69 =head1 DESCRIPTION
70
71 Reads C<fstab> and C<mtab> files to provide mount point information.
72
73 =head1 SEE ALSO
74
75 =over
76
77 =item L<System::Introspector>
78
79 =back
80
81 =cut
82