MountPoints probe for fstab and mtab files
Robert 'phaylon' Sedlacek [Thu, 3 May 2012 16:43:06 +0000 (16:43 +0000)]
lib/System/Introspector/MountPoints.pm [new file with mode: 0644]

diff --git a/lib/System/Introspector/MountPoints.pm b/lib/System/Introspector/MountPoints.pm
new file mode 100644 (file)
index 0000000..761cabe
--- /dev/null
@@ -0,0 +1,47 @@
+package System::Introspector::MountPoints;
+use Moo;
+
+sub gather {
+    my ($self) = @_;
+    return {
+        mtab  => $self->_parse_tab_fh($self->_open_fh('/etc/mtab')),
+        fstab => $self->_parse_tab_fh($self->_open_fh('/etc/fstab')),
+    };
+}
+
+sub _open_fh {
+    my ($self, $file) = @_;
+    open my $fh, '<', $file
+        or die "Unable to open $file: $!\n";
+    return $fh;
+}
+
+sub _parse_tab_fh {
+    my ($self, $fh) = @_;
+    my @mounts;
+    while (defined( my $line = <$fh> )) {
+        chomp $line;
+        next if $line =~ m{^\s*$}
+             or $line =~ m{^\s*#};
+        my ($device, $point, $type, $opt, $dump, $pass)
+            = split m{\s+}, $line;
+        push @mounts, {
+            device_name => $device,
+            moint_point => $point,
+            fs_type     => $type,
+            dump_freq   => $dump,
+            pass_num    => $pass,
+            options     => {
+                map {
+                    my ($name, $value) = split m{=}, $_, 2;
+                    $value = 1
+                        unless defined $value;
+                    ($name => $value);
+                } split m{,}, $opt,
+            },
+        };
+    }
+    return \@mounts;
+}
+
+1;