blib/lib/Sys/FS.pm

filter: Statementstmt 17.6% 36 / 204 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 35 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 50.0% 11 / 22 Podpod 100% 10 / 10 totaltotal 16.5% 57 / 345 CC 47 SCAR 39.1
CC
47
Cov
12%
switch
132.0
makedir
90.0
spaceleft
42.0
CRAP 49.8
↑ Summary
1 package Sys::FS;
2 {
3 2 $Sys::FS::VERSION = '0.11';
4 }
5 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 $Sys::FS::AUTHORITY = 'cpan:TEX';
7 }
8 # ABSTRACT: filesystem interaction tools
9  
10 2 ▶use 5.010_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use feature ':5.10';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # use IO::Handle;
18 # use autodie;
19 # use MooseX::Params::Validate;
20  
21 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use File::Copy qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 2 ▶use Sys::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 2 with 'Log::Tree::RequiredLogger';
28  
29 2 has 'sys' => (
30 'is' => 'ro',
31 'isa' => 'Sys::Run',
32 'lazy' => 1,
33 'builder' => '_init_sys',
34 );
35  
36 0 ▶sub _init_sys {
Subroutine _init_sys: not called CC=1 SCAR=6.9
37 0 my $self = shift;
38  
39 0 my $Sys = Sys::Run::->new( { 'logger' => $self->logger(), } );
40  
41 0 return $Sys;
42 }
43  
44 ############################################
45 # Usage : Create an absolute filename
46 0 ▶sub filename {
Subroutine filename: not called CC=2 SCAR=17.9
Pod documented
47 0 my ( $self, @dirs ) = @_;
48  
49 0 ▶ if ( !@dirs ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@dirs) 0 0 0
50 0 my $msg = 'Missing option in Sys::FS::filename. Need at least one directory. Caller: ' . ( caller(1) )[3];
51 0 $self->logger()->log( message => $msg, level => 'error', );
52 0 return;
53 }
54  
55 0 my $filename = File::Spec->catfile(@dirs);
56 0 return $filename;
57 }
58  
59 0 ▶sub switch {
Subroutine switch: not called CC=11 SCAR=48.8
Pod documented
60 0 my $self = shift;
61 0 my $sourcefile = shift;
62 0 my $destfile = shift;
63  
64 0 ▶ if($sourcefile && ref($sourcefile)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sourcefile and ref $sourcefile) 0 0 0
Condition0%0/3
$sourcefile and ref $sourcefile
!ll&&!rl&&r
000
MC/DC0%0/2
$sourcefile and ref $sourcefile
$sourcefile ref $sourcefile
Truth table0%0/3
A and B
A= $sourcefileB= ref $sourcefile
ABresult
0X0
100
111
65 0 $self->logger()->log( message => 'Refusing to switch multiple source files to one destination. Check your configuration.', level => 'error', );
66 0 return;
67 }
68  
69 0 ▶ if($destfile && ref($destfile)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($destfile and ref $destfile) 0 0 0
Condition0%0/3
$destfile and ref $destfile
!ll&&!rl&&r
000
MC/DC0%0/2
$destfile and ref $destfile
$destfile ref $destfile
Truth table0%0/3
A and B
A= $destfileB= ref $destfile
ABresult
0X0
100
111
70 0 $self->logger()->log( message => 'Refusing to switch one source file to multiple destinations. Check your configuration.', level => 'error', );
71 0 return;
72 }
73  
74 0 my @cmds = ();
75  
76 # unlink old backup, if it doesn't exist do nothing
77 0 ▶ if ( -e $destfile . '.bak' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $destfile . '.bak') 0 0 0
78 0 ▶ if ( unlink( $destfile . '.bak' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlink $destfile . '.bak') { } 0 0 0
79 0 $self->logger()->log( message => 'source: '.$sourcefile.' - dest: '.$destfile.' - Removed old backup.', level => 'debug', );
80 }
81 else {
82 0 $self->logger()->log( message => 'source: '.$sourcefile.' - dest: '.$destfile.' - Could not remove old backup: ' . $!, level => 'error', );
83 0 return;
84 }
85 }
86  
87 # prepare new config for switch
88 0 ▶ if ( File::Copy::copy( $sourcefile, $destfile . '.new' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (File::Copy::copy($sourcefile, $destfile . '.new')) { } 0 0 0
89 0 $self->logger()->log( message => "source: $sourcefile - dest: $destfile - Successfully copied to .new file.", level => 'debug', );
90 }
91 else {
92 0 $self->logger()->log( message => "source: $sourcefile - dest: $destfile - Failed to copy $sourcefile to $destfile.new", level => 'error', );
93 0 return;
94 }
95  
96 # create backup of old config
97 0 ▶ if ( -e $destfile ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $destfile) 0 0 0
98 0 ▶ if ( File::Copy::copy( $destfile, $destfile . '.bak' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (File::Copy::copy($destfile, $destfile . '.bak')) { } 0 0 0
99 0 $self->logger()->log( message => "source: $sourcefile - dest: $destfile - Successfully created backup.", level => 'debug', );
100 }
101 else {
102 0 $self->logger()->log( message => "source: $sourcefile - dest: $destfile - Failed to copy $destfile to $destfile.bak", level => 'error' );
103 0 return;
104 }
105 }
106  
107 # perform the final switch (atomic? should be ...)
108 0 ▶ if ( rename( $destfile . '.new', $destfile ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (rename $destfile . '.new', $destfile) { } 0 0 0
109 0 $self->logger()->log( message => "source: $sourcefile - dest: $destfile - Successfully switched files.", level => 'debug', );
110 0 return 1;
111 }
112 else {
113 0 $self->logger()->log( message => "source: $sourcefile - dest: $destfile - Failed to copy $destfile.new to $destfile", level => 'error', );
114 0 return;
115 }
116 }
117  
118 ############################################
119 # Usage : Create a directory stucture and return the created directory
120 0 ▶sub makedir {
Subroutine makedir: not called CC=9 SCAR=45.0
Pod documented
121 0 my $self = shift;
122 0 my $filename = shift;
123 0 ▶ my $opts = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
124  
125 0 ▶ if ( !$filename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($filename) 0 0 0
126 0 my $msg = 'No filename given in Sys::FS::makedir! Caller: ' . ( caller(1) )[3];
127 0 $self->logger()->log( message => $msg, level => 'error', );
128 0 return;
129 }
130  
131 0 ▶ my $mode = $opts->{'Mode'} || oct(777);
Condition0%0/2
$opts->{'Mode'} || 511
l!l
00
MC/DC0%0/1
$opts->{'Mode'} || 511
$opts->{'Mode'}
Truth table0%0/2
A
A= $opts->{'Mode'}
Aresult
11
00
132  
133 0 my @dirs = File::Spec::->splitdir($filename);
134 0 my $dir = q{};
135 0 my $mkdirs = 0;
136 0 foreach my $i ( 0 .. $#dirs ) {
137 0 $dir = File::Spec::->catdir( @dirs[ 0 .. $i ] );
138 0 ▶ if ( !-d $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 0 0 0
139 0 my $msg = "Filename: $filename - mkdir $dir, $mode";
140 0 ▶ if ( mkdir( $dir, $mode ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (mkdir $dir, $mode) { } 0 0 0
141 0 $self->logger()->log( message => $msg, level => 'debug', );
142 0 $mkdirs++;
143 }
144 else {
145 0 $self->logger()->log( message => $msg . ' FAILED!', level => 'debug', );
146 }
147 0 ▶ if ( $opts->{'Uid'} && $opts->{'Gid'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"Uid"} and $opts->{"Gid"}) 0 0 0
Condition0%0/3
$opts->{"Uid"} and $opts->{"Gid"}
!ll&&!rl&&r
000
MC/DC0%0/2
$opts->{"Uid"} and $opts->{"Gid"}
$opts->{"Uid"} $opts->{"Gid"}
Truth table0%0/3
A and B
A= $opts->{"Uid"}B= $opts->{"Gid"}
ABresult
0X0
100
111
148 0 chown $opts->{'Uid'}, $opts->{'Gid'} => $dir;
149 }
150 }
151 }
152 0 my $msg = "Filename: $filename - created $mkdirs dirs for $dir";
153 0 $self->logger()->log( message => $msg, level => 'debug', );
154 0 return $dir;
155 }
156  
157 0 ▶sub spaceleft {
Subroutine spaceleft: not called CC=6 SCAR=37.4
Pod documented
158 0 my $self = shift;
159 0 my $dir = shift;
160 0 ▶ my $host = shift || 'localhost';
Condition0%0/2
shift() || 'localhost'
l!l
00
MC/DC0%0/1
shift() || 'localhost'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
161 0 ▶ my $opts = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
162  
163 # check free space on destination
164 0 local $opts->{CaptureOutput} = 1;
165 0 local $opts->{Chomp} = 1;
166 0 my $cmd = 'LANG=C /bin/df -P ' . $dir . ' | /usr/bin/tail -1';
167 0 my $out = $self->sys()->run( $host, $cmd, $opts );
168  
169 0 my ( $dev, $onekblocks, $used, $avail, $pcfree, $mount_point ) = split /\s+/, $out;
170 0 ▶ $avail = 0 unless $avail =~ m/^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $avail =~ /^\d+$/ 0 0 0
171 0 ▶ $onekblocks = 0 unless $onekblocks =~ m/^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $onekblocks =~ /^\d+$/ 0 0 0
172 0 my $gbfree = int( $avail / ( 1024 * 1024 ) );
173 0 my $gbtotal = int( $onekblocks / ( 1024 * 1024 ) );
174 0 $pcfree =~ s/%$//;
175  
176 0 ▶ return wantarray ? ( $gbfree, $pcfree, $gbtotal ) : $gbfree;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
177 }
178  
179 0 ▶sub fsck {
Subroutine fsck: not called CC=4 SCAR=30.0
Pod documented
180 0 my $self = shift;
181 0 my $device = shift;
182 0 my $fs_type = shift;
183 0 ▶ my $opts = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
184 0 ▶ if ( $fs_type && -x '/sbin/fsck.'.$fs_type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fs_type and -x '/sbin/fsck.' . $fs_type) { } 0 0 0
Condition0%0/3
$fs_type and -x '/sbin/fsck.' . $fs_type
!ll&&!rl&&r
000
MC/DC0%0/2
$fs_type and -x '/sbin/fsck.' . $fs_type
$fs_type -x '/sbin/fsck.' . $fs_type
Truth table0%0/3
A and B
A= $fs_typeB= -x '/sbin/fsck.' . $fs_type
ABresult
0X0
100
111
185 0 $self->sys()->run_cmd('/sbin/fsck.'.$fs_type.' -y -p '.$device);
186 0 return 1;
187 }
188 else {
189 0 my $msg = "fsck($device,$fs_type) - dunno how to check $fs_type!";
190 0 $self->logger()->log( message => $msg, level => 'error', );
191 0 return;
192 }
193 }
194  
195 # used by get_mounted_device
196 0 ▶sub mounts {
Subroutine mounts: not called CC=5 SCAR=34.0
Pod documented
197 0 my $self = shift;
198 0 ▶ my $opts = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
199  
200 0 my $mounts_file = '/proc/mounts';
201 0 ▶ if ( open( my $FH, '<', $mounts_file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $FH, '<', $mounts_file) { } 0 0 0
202 0 my @lines = <$FH>;
203 # DGR: just reading
204 ## no critic (RequireCheckedClose)
205 0 close($FH);
206 ## use critic
207 0 my %mounts = ();
208 0 foreach my $line (@lines) {
209 0 my ( $dev, $mount_point, $fs_type, $options, $dump, $pass ) = split( /\s/, $line );
210 0 my $key;
211 0 ▶ if ( $opts->{DevAsKey} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'DevAsKey'}) { } 0 0 0
212 0 $key = $dev;
213 0 $mounts{$key}{'mount_point'} = $mount_point;
214 }
215 else { # MountPointAsKey
216 0 $key = $mount_point;
217 0 $mounts{$key}{'dev'} = $dev;
218 }
219 0 $mounts{$key}{'fs_type'} = $fs_type;
220 0 $mounts{$key}{'options'} = $options;
221 0 $mounts{$key}{'dump'} = $dump;
222 0 $mounts{$key}{'pass'} = $pass;
223 }
224 0 return \%mounts;
225 }
226 else {
227 0 my $msg = "Could not open $mounts_file: $!";
228 0 $self->logger()->log( message => $msg, level => 'error', );
229 0 return {};
230 }
231 }
232  
233 0 ▶sub is_mounted {
Subroutine is_mounted: not called CC=4 SCAR=30.0
Pod documented
234 0 my $self = shift;
235 0 my $device = shift;
236 0 ▶ my $opts = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
237  
238 # check if given device is mounted
239 0 local $opts->{DevAsKey} = 1;
240 0 my $mounts = $self->mounts($opts);
241 0 foreach my $dev ( keys %{$mounts} ) {
242 0 ▶ if ( $device =~ m/^$dev/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($device =~ /^$dev/) 0 0 0
243 0 return 1;
244 }
245 }
246 0 return;
247 }
248  
249 0 ▶sub get_mounted_device {
Subroutine get_mounted_device: not called CC=6 SCAR=37.4
Pod documented
250 0 my $self = shift;
251 0 my $path = shift;
252 0 ▶ my $opts = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
253  
254 0 $self->logger()->log( message => "$path", level => 'debug', );
255  
256 # get mounts indexed by mount point as key, so we
257 # can traverse over the mounts to find the longest matching one
258 0 my $mounts = $self->mounts( { DevAsKey => 0, } );
259 0 my $device = undef;
260 0 my $mount_point = undef;
261  
262 # sort the keys be length, the shortest (the root fs at /) first
263 # and use the longest still-matching one as the mounted device
264 # for the given path
265 0 foreach my $key ( sort { length($a) <=> length($b) } keys %{$mounts} ) {
266 0 ▶ if ( $path =~ m/^$key/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /^$key/) 0 0 0
267 0 $device = $mounts->{$key}{'dev'};
268 0 $mount_point = $key;
269 }
270 }
271  
272 0 ▶ if ( !$mount_point ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mount_point) 0 0 0
273 0 my $msg = "get_mounted_device($path) - no matching mount point found!";
274 0 $self->logger()->log( message => $msg, level => 'error', );
275 0 return;
276 }
277  
278 $self->logger()->log(
279 0 message => 'Path: '.$path.' - returning '.$device.', ' . $mounts->{$mount_point}{'fs_type'} . ', ' . $mounts->{$mount_point}{'options'} . ', '.$mount_point,
280 level => 'debug',
281 );
282  
283 0 ▶ return wantarray ? ( $device, $mounts->{$mount_point}{'fs_type'}, $mounts->{$mount_point}{'options'}, $mount_point ) : $device;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
284 }
285  
286 0 ▶sub mount {
Subroutine mount: not called CC=6 SCAR=37.4
Pod documented
287 0 my $self = shift;
288 0 my $device = shift;
289 0 my $mount_point = shift;
290 0 my $fs_type = shift;
291 0 my $fs_opts = shift;
292 0 ▶ my $opts = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
293  
294 0 ▶ if ( $fs_type eq 'xfs' && $fs_opts !~ m/nouuid/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fs_type eq "xfs" and not $fs_opts =~ /nouuid/i) 0 0 0
Condition0%0/3
$fs_type eq "xfs" and not $fs_opts =~ /nouuid/i
!ll&&!rl&&r
000
MC/DC0%0/2
$fs_type eq "xfs" and not $fs_opts =~ /nouuid/i
$fs_type eq "xfs" not $fs_opts =~ /nouuid/i
Truth table0%0/3
A and not(B)
A= $fs_type eq "xfs"B= not $fs_opts =~ /nouuid/i
ABresult
0X0
100
111
295 0 $fs_opts .= ',nouuid';
296 }
297  
298 # device must be a block device
299 0 ▶ if ( !-b $device ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-b $device) 0 0 0
300 0 my $msg = "mount($device,$mount_point,$fs_type,$fs_opts) - $device is no block-device!";
301 0 $self->logger()->log( message => $msg, level => 'error', );
302 0 return;
303 }
304  
305 # mp must be a dir
306 0 ▶ if ( !-d $mount_point ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $mount_point) 0 0 0
307 0 my $msg = "mount($device,$mount_point,$fs_type,$fs_opts) - $mount_point is no directory!";
308 0 $self->logger()->log( message => $msg, level => 'error', );
309 0 return;
310 }
311  
312 0 my $cmd = "/bin/mount -t $fs_type -o $fs_opts $device $mount_point";
313 0 local $opts->{Timeout} = 1200;
314 0 return $self->sys()->run_cmd( $cmd, $opts );
315 }
316  
317 0 ▶sub umount {
Subroutine umount: not called CC=3 SCAR=24.8
Pod documented
318 0 my $self = shift;
319 0 my $device = shift;
320 0 ▶ my $opts = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
321  
322 # device must be a block device
323 0 ▶ if ( !-b $device ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-b $device) 0 0 0
324 0 my $msg = "umount($device) - $device is no block-device!";
325 0 $self->logger()->log( message => $msg, level => 'error', );
326 0 return;
327 }
328  
329 0 my $cmd = "/bin/umount $device >/dev/null 2>&1";
330 0 return $self->sys()->run_cmd( $cmd, $opts );
331 }
332  
333 2 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
334 2 __PACKAGE__->meta->make_immutable;
335  
336 2 1;
337  
338 __END__
↑ Summary