File Coverage

blib/lib/Sys/Filesystem/Unix.pm
Criterion Covered Total %
statement 44 104 42.3
branch 8 54 14.8
condition 4 27 14.8
subroutine 9 14 64.2
pod 5 6 83.3
total 70 205 34.1


line stmt bran cond sub pod time code
1             ############################################################
2             #
3             # $Id$
4             # Sys::Filesystem - Retrieve list of filesystems and their properties
5             #
6             # Copyright 2004,2005,2006 Nicola Worthington
7             # Copyright 2009 Jens Rehsack
8             #
9             # Licensed under the Apache License, Version 2.0 (the "License");
10             # you may not use this file except in compliance with the License.
11             # You may obtain a copy of the License at
12             #
13             # http://www.apache.org/licenses/LICENSE-2.0
14             #
15             # Unless required by applicable law or agreed to in writing, software
16             # distributed under the License is distributed on an "AS IS" BASIS,
17             # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18             # See the License for the specific language governing permissions and
19             # limitations under the License.
20             #
21             ############################################################
22              
23             package Sys::Filesystem::Unix;
24              
25             # vim:ts=4:sw=4:tw=78
26              
27 6     6   13571 use 5.008001;
  6         21  
  6         293  
28              
29 6     6   34 use strict;
  6         11  
  6         205  
30 6     6   33 use warnings;
  6         11  
  6         215  
31 6     6   29 use vars qw($VERSION);
  6         13  
  6         288  
32              
33 6     6   34 use Carp qw(croak);
  6         10  
  6         330  
34 6     6   42 use Cwd 'abs_path';
  6         11  
  6         306  
35 6     6   33 use Fcntl qw(:flock);
  6         11  
  6         1168  
36 6     6   6082 use IO::File;
  6         75366  
  6         11127  
37              
38             $VERSION = '1.406';
39              
40             sub version()
41             {
42 0     0 1 0 return $VERSION;
43             }
44              
45             # Default fstab and mtab layout
46             my @keys = qw(fs_spec fs_file fs_vfstype fs_mntops fs_freq fs_passno);
47             my %special_fs = (
48             swap => 1,
49             proc => 1
50             );
51              
52             sub new
53             {
54 0 0   0 0 0 ref( my $class = shift ) && croak 'Class name required';
55 0         0 my %args = @_;
56 0         0 my $self = bless( {}, $class );
57 0 0       0 $args{canondev} and $self->{canondev} = 1;
58              
59             # Defaults
60 0   0     0 $args{fstab} ||= '/etc/fstab';
61 0   0     0 $args{mtab} ||= '/etc/mtab';
62              
63 0         0 $self->readFsTab( $args{fstab}, \@keys, [ 0, 1, 2 ], \%special_fs );
64 0         0 $self->readMntTab( $args{mtab}, \@keys, [ 0, 1, 2 ], \%special_fs );
65              
66 0         0 delete $self->{canondev};
67              
68 0         0 $self;
69             }
70              
71             sub readFsTab($\@\@\%)
72             {
73 0     0 1 0 my ( $self, $fstabPath, $fstabKeys, $pridx, $special_fs ) = @_;
74              
75             # Read the fstab
76 0         0 local $/ = "\n";
77 0 0       0 if ( my $fstab = IO::File->new( $fstabPath, 'r' ) )
78             {
79 0         0 while (<$fstab>)
80             {
81 0 0 0     0 next if ( /^\s*#/ || /^\s*$/ );
82              
83             # $_ =~ s/#.*$//;
84             # next if( /^\s*$/ );
85              
86 0         0 my @vals = split( ' ', $_ );
87 0 0 0     0 $self->{canondev} and -l $vals[ $pridx->[0] ] and $vals[ $pridx->[0] ] = abs_path( $vals[ $pridx->[0] ] );
88 0         0 $self->{ $vals[ $pridx->[1] ] }->{mount_point} = $vals[ $pridx->[1] ];
89 0         0 $self->{ $vals[ $pridx->[1] ] }->{device} = $vals[ $pridx->[0] ];
90 0 0       0 $self->{ $vals[ $pridx->[1] ] }->{unmounted} = 1
91             unless ( defined( $self->{ $vals[ $pridx->[1] ] }->{mounted} ) );
92              
93 0 0       0 if ( defined( $pridx->[2] ) )
94             {
95 0         0 my $vfs_type = $self->{ $vals[ $pridx->[1] ] }->{fs_vfstype} = $vals[ $pridx->[2] ];
96 0 0       0 $self->{ $vals[ $pridx->[1] ] }->{special} = 1
97             if ( defined( $special_fs->{$vfs_type} ) );
98             }
99             else
100             {
101 0 0       0 $self->{ $vals[ $pridx->[1] ] }->{special} = 0
102             unless ( defined( $self->{ $vals[ $pridx->[1] ] }->{special} ) );
103             }
104              
105 0         0 for ( my $i = 0; $i < @{$fstabKeys}; ++$i )
  0         0  
106             {
107 0 0       0 $self->{ $vals[ $pridx->[1] ] }->{ $fstabKeys->[$i] } =
108             defined( $vals[$i] ) ? $vals[$i] : '';
109             }
110             }
111 0         0 $fstab->close();
112 0         0 1;
113             }
114             else
115             {
116 0         0 0;
117             }
118             }
119              
120             sub readMntTab($\@\@\%)
121             {
122 9     9 1 24 my ( $self, $mnttabPath, $mnttabKeys, $pridx, $special_fs ) = @_;
123              
124             # Read the mtab
125 9         42 local $/ = "\n";
126 9         15 my $mtab;
127 9 50 33     53 if ( ( $mtab = IO::File->new( $mnttabPath, 'r' ) ) && flock( $mtab, LOCK_SH | LOCK_NB ) )
128             {
129 9         1552 while (<$mtab>)
130             {
131 162 50 33     889 next if ( /^\s*#/ || /^\s*$/ );
132              
133             # $_ =~ s/#.*$//;
134             # next if( /^\s*$/ );
135              
136 162         4618 my @vals = split( /\s+/, $_ );
137 162 50 66     879 $self->{canondev} and -l $vals[ $pridx->[0] ] and $vals[ $pridx->[0] ] = abs_path( $vals[ $pridx->[0] ] );
138 162 50       603 delete $self->{ $vals[ $pridx->[1] ] }->{unmounted}
139             if ( exists( $self->{ $vals[ $pridx->[1] ] }->{unmounted} ) );
140 162         347 $self->{ $vals[ $pridx->[1] ] }->{mounted} = 1;
141 162         430 $self->{ $vals[ $pridx->[1] ] }->{mount_point} = $vals[ $pridx->[1] ];
142 162         343 $self->{ $vals[ $pridx->[1] ] }->{device} = $vals[ $pridx->[0] ];
143              
144 162 50       292 if ( defined( $pridx->[2] ) )
145             {
146 162         601 my $vfs_type = $self->{ $vals[ $pridx->[1] ] }->{fs_vfstype} = $vals[ $pridx->[2] ];
147 162 100       821 $self->{ $vals[ $pridx->[1] ] }->{special} = 1
148             if ( defined( $special_fs->{$vfs_type} ) );
149             }
150             else
151             {
152 0 0       0 $self->{ $vals[ $pridx->[1] ] }->{special} = 0
153             unless ( defined( $self->{ $vals[ $pridx->[1] ] }->{special} ) );
154             }
155              
156 162         229 for ( my $i = 0; $i < @{$mnttabKeys}; ++$i )
  1134         3188  
157             {
158 972 50       5953 $self->{ $vals[ $pridx->[1] ] }->{ $mnttabKeys->[$i] } =
159             defined( $vals[$i] ) ? $vals[$i] : '';
160             }
161             }
162 9         92 $mtab->close();
163 9         211 1;
164             }
165             else
166             {
167 0           0;
168             }
169             }
170              
171             sub readMounts
172             {
173 0     0 1   my ( $self, $mount_rx, $pridx, $keys, $special, @lines ) = @_;
174              
175 0           foreach my $line (@lines)
176             {
177 0 0         if ( my @vals = $line =~ $mount_rx )
178             {
179 0 0 0       $self->{canondev} and -l $vals[ $pridx->[0] ] and $vals[ $pridx->[0] ] = abs_path( $vals[ $pridx->[0] ] );
180 0           $self->{ $vals[ $pridx->[1] ] }->{mount_point} = $vals[ $pridx->[1] ];
181 0           $self->{ $vals[ $pridx->[1] ] }->{device} = $vals[ $pridx->[0] ];
182 0           $self->{ $vals[ $pridx->[1] ] }->{mounted} = 1;
183 0 0         delete $self->{ $vals[ $pridx->[1] ] }->{unmounted}
184             if ( exists( $self->{ $vals[ $pridx->[1] ] }->{unmounted} ) );
185              
186 0 0         if ( defined( $pridx->[2] ) )
    0          
187             {
188 0           my $vfs_type = $self->{ $vals[ $pridx->[1] ] }->{fs_vfstype} = $vals[ $pridx->[2] ];
189 0 0         $self->{ $vals[ $pridx->[1] ] }->{special} = 1
190             if ( defined( $special->{$vfs_type} ) );
191             }
192             elsif ( !defined( $self->{ $vals[ $pridx->[1] ] }->{special} ) )
193             {
194 0           $self->{ $vals[ $pridx->[1] ] }->{special} = 0;
195             }
196              
197 0           for ( my $i = 0; $i < @{$keys}; ++$i )
  0            
198             {
199 0 0         $self->{ $vals[ $pridx->[1] ] }->{ $keys->[$i] } =
200             defined( $vals[$i] ) ? $vals[$i] : '';
201             }
202             }
203             }
204              
205 0           $self;
206             }
207              
208             sub readSwap
209             {
210 0     0 1   my ( $self, $swap_rx, @lines ) = @_;
211 0           foreach my $line (@lines)
212             {
213 0 0         if ( my ($dev) = $line =~ $swap_rx )
214             {
215 0 0 0       $self->{canondev} and -l $dev and $dev = abs_path($dev);
216 0   0       $self->{none}->{mount_point} ||= 'none';
217 0           $self->{none}->{device} = $dev;
218 0           $self->{none}->{fs_vfstype} = 'swap';
219 0           $self->{none}->{mounted} = 1;
220 0           $self->{none}->{special} = 1;
221 0           delete $self->{none}->{unmounted};
222             }
223             }
224 0           $self;
225             }
226              
227             1;
228              
229             =pod
230              
231             =head1 NAME
232              
233             Sys::Filesystem::Unix - Return generic Unix filesystem information to Sys::Filesystem
234              
235             =head1 SYNOPSIS
236              
237             See L.
238              
239             =head1 INHERITANCE
240              
241             Sys::Filesystem::Unix
242             ISA UNIVERSAL
243              
244             =head1 METHODS
245              
246             =over 4
247              
248             =item version()
249              
250             Return the version of the (sub)module.
251              
252             =item readFsTab
253              
254             This method provides the capability to parse a standard unix fstab file.
255              
256             It expects following arguments:
257              
258             =over 8
259              
260             =item fstabPath
261              
262             Full qualified path to the fstab file to read.
263              
264             =item fstabKeys
265              
266             The column names for the fstab file through an array reference.
267              
268             =item special_fs
269              
270             Hash reference containing the names of all special file systems having a true
271             value as key.
272              
273             =back
274              
275             This method return true in case the specified file could be opened for reading,
276             false otherwise.
277              
278             =item readMntTab
279              
280             This method provides the capability to read abd parse a standard unix
281             mount-tab file. The file is locked using flock after opening it.
282              
283             It expects following arguments:
284              
285             =over 8
286              
287             =item mnttabPath
288              
289             Full qualified path to the mnttab file to read.
290              
291             =item mnttabKeys
292              
293             The column names for the mnttab file through an array reference.
294              
295             =item $special_fs
296              
297             Hash reference containing the names of all special file systems having a true
298             value as key.
299              
300             =back
301              
302             This method return true in case the specified file could be opened for reading
303             and locked, false otherwise.
304              
305             =item readMounts
306              
307             This method is called to parse the information got from C system command.
308             It expects following arguments:
309              
310             =over 8
311              
312             =item mount_rx
313              
314             Regular expression to extract the information from each mount line.
315              
316             =item pridx
317              
318             Array reference containing the index for primary keys of interest in match
319             in following order: device, mount_point, type.
320              
321             =item keys
322              
323             Array reference of the columns of the match - in order of paranteses in
324             regular expression.
325              
326             =item special
327              
328             Array reference containing the names of the special file system types.
329              
330             =item lines
331              
332             Array containing the lines to parse.
333              
334             =back
335              
336             =item readSwap
337              
338             This method is called to parse the information from the swap status.
339             It expects following arguments:
340              
341             =over 8
342              
343             =item swap_rx
344              
345             Regular expression to extract the information from each swap status line.
346             This regular expression should have exact one pair of parantheses to
347             identify the swap device.
348              
349             =item lines
350              
351             Array containing the lines to parse.
352              
353             =back
354              
355             =back
356              
357             =head1 VERSION
358              
359             $Id$
360              
361             =head1 AUTHOR
362              
363             Nicola Worthington - L
364              
365             Jens Rehsack - L
366              
367             =head1 COPYRIGHT
368              
369             Copyright 2004,2005,2006 Nicola Worthington.
370             Copyright 2008-2014 Jens Rehsack.
371              
372             This software is licensed under The Apache Software License, Version 2.0.
373              
374             L
375              
376             =cut
377