blib/lib/Sysadm/Install.pm

filter: Statementstmt 62.3% 316 / 507 Branchbran 39.7% 112 / 282 Conditioncond 30.1% 16 / 53 MC/DCmcdc 8.8% 3 / 34 Subroutinesub 70.3% 45 / 64 Podpod 80.8% 38 / 47 totaltotal 53.7% 530 / 987 CC 166 SCAR 29.9
CC
166
Cov
53%
pick
110.0
user_prompt
72.0
untar_in
42.0
CRAP 20.0
↑ Summary
1 ###############################################
2 package Sysadm::Install;
3 ###############################################
4  
5 18 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 18 our $VERSION = '0.48';
10  
11 18 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 18 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 18 ▶use File::Which;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 18 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 18 ▶use Log::Log4perl::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 18 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 18 ▶use File::Spec::Functions qw(rel2abs abs2rel);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 18 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 18 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 18 our $DRY_RUN;
22 18 our $CONFIRM;
23 18 our $DRY_RUN_MSG;
24 18 our $DATA_SNIPPED_LEN = 60;
25  
26 18 dry_run(0);
27 18 confirm(0);
28  
29 ###############################################
30 20 ▶sub dry_run {
Subroutine dry_run: called CC=2 SCAR=6.9
Pod undocumented
31 ###############################################
32 20 my($on) = @_;
33  
34 20 ▶ if($on) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on) { } 1 19 20
35 1 $DRY_RUN = 1;
36 1 $DRY_RUN_MSG = "(skipped - dry run)";
37 } else {
38 19 $DRY_RUN = 0;
39 19 $DRY_RUN_MSG = "";
40 }
41 }
42  
43 ###############################################
44 18 ▶sub confirm {
Subroutine confirm: called CC=1 SCAR=0.0
Pod undocumented
45 ###############################################
46 18 my($on) = @_;
47  
48 18 $CONFIRM = $on;
49 }
50  
51 ###########################################
52 30 ▶sub _confirm {
Subroutine _confirm: called CC=5 SCAR=20.9
53 ###########################################
54 30 my($msg) = @_;
55  
56 30 ▶ if($DRY_RUN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Sysadm::Install::DRY_RUN) 1 29 30
57 1 INFO "$msg $DRY_RUN_MSG";
58 1 ▶ return 0 if $DRY_RUN;
Branch: 1 branch
BranchTrueFalseTotal
if $Sysadm::Install::DRY_RUN 1 0 1
59 }
60  
61 29 ▶ if($CONFIRM) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Sysadm::Install::CONFIRM) 0 29 29
62 0 my $answer = ask("$msg ([y]/n)", "y");
63 0 ▶ if($answer =~ /^\s*y\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($answer =~ /^\s*y\s*$/) 0 0 0
64 0 INFO $msg;
65 0 return 1;
66 }
67  
68 0 INFO "$msg (*CANCELLED* as requested)";
69 0 return 0;
70 }
71  
72 29 return 1;
73 }
74  
75 18 our @EXPORTABLE = qw(
76 cp rmf mkd cd make
77 cdback download untar
78 pie slurp blurt mv tap
79 plough qquote quote perm_cp owner_cp
80 perm_get perm_set
81 sysrun untar_in pick ask
82 hammer say
83 sudo_me bin_find
84 fs_read_open fs_write_open pipe_copy
85 snip password_read nice_time
86 def_or blurt_atomic
87 is_utf8_data utf8_available
88 printable home_dir
89 );
90  
91 18 our %EXPORTABLE = map { $_ => 1 } @EXPORTABLE;
92  
93 18 our @DIR_STACK;
94  
95 ##################################################
96 18 ▶sub import {
Subroutine import: called CC=4 SCAR=13.9
97 ##################################################
98 18 my($class) = shift;
99  
100 18 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
101  
102 18 my $caller_pkg = caller();
103  
104 18 my(%tags) = map { $_ => 1 } @_;
105  
106 # Export all
107 18 ▶ if(exists $tags{':all'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $tags{":all"}) 16 2 18
108 16 %tags = map { $_ => 1 } @EXPORTABLE;
109 }
110  
111 18 for my $func (keys %tags) {
112 LOGDIE __PACKAGE__ .
113 641 ▶ "doesn't export \"$func\"" unless exists $EXPORTABLE{$func};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Sysadm::Install::EXPORTABLE{$func} 0 641 641
114 641 *{"$caller_pkg\::$func"} = *{$func};
115 }
116 }
117  
118 =pod
119  
120 =head1 NAME
121  
122 Sysadm::Install - Typical installation tasks for system administrators
123  
124 =head1 SYNOPSIS
125  
126 use Sysadm::Install qw(:all);
127  
128 my $INST_DIR = '/home/me/install/';
129  
130 cd($INST_DIR);
131 cp("/deliver/someproj.tgz", ".");
132 untar("someproj.tgz");
133 cd("someproj");
134  
135 # Write out ...
136 blurt("Builder: Mike\nDate: Today\n", "build.dat");
137  
138 # Slurp back in ...
139 my $data = slurp("build.dat");
140  
141 # or edit in place ...
142 pie(sub { s/Today/scalar localtime()/ge; $_; }, "build.dat");
143  
144 make("test install");
145  
146 # run a cmd and tap into stdout and stderr
147 my($stdout, $stderr, $exit_code) = tap("ls", "-R");
148  
149 =head1 DESCRIPTION
150  
151 Have you ever wished for your installation shell scripts to run
152 reproducibly, without much programming fuzz, and even with optional
153 logging enabled? Then give up shell programming, use Perl.
154  
155 C<Sysadm::Install> executes shell-like commands performing typical
156 installation tasks: Copying files, extracting tarballs, calling C<make>.
157 It has a C<fail once and die> policy, meticulously checking the result
158 of every operation and calling C<die()> immediately if anything fails.
159  
160 C<Sysadm::Install> also supports a I<dry_run> mode, in which it
161 logs everything, but suppresses any write actions. Dry run mode
162 is enabled by calling C<Sysadm::Install::dry_run(1)>. To switch
163 back to normal, call C<Sysadm::Install::dry_run(0)>.
164  
165 As of version 0.17, C<Sysadm::Install> supports a I<confirm> mode,
166 in which it interactively asks the user before running any of its
167 functions (just like C<rm -i>). I<confirm> mode is enabled by calling
168 C<Sysadm::Install::confirm(1)>. To switch
169 back to normal, call C<Sysadm::Install::confirm(0)>.
170  
171 C<Sysadm::Install> is fully Log4perl-enabled. To start logging, just
172 initialize C<Log::Log4perl>. C<Sysadm::Install> acts as a wrapper class,
173 meaning that file names and line numbers are reported from the calling
174 program's point of view.
175  
176 =head2 FUNCTIONS
177  
178 =over 4
179  
180 =item C<cp($source, $target)>
181  
182 Copy a file from C<$source> to C<$target>. C<target> can be a directory.
183 Note that C<cp> doesn't copy file permissions. If you want the target
184 file to reflect the source file's user rights, use C<perm_cp()>
185 shown below.
186  
187 =cut
188  
189 ###############################################
190 2 ▶sub cp {
Subroutine cp: called CC=3 SCAR=11.0
Pod documented
191 ###############################################
192  
193 2 local $Log::Log4perl::caller_depth =
194 $Log::Log4perl::caller_depth + 1;
195  
196 2 ▶ _confirm("cp $_[0] $_[1]") or return 1;
Branch: 1 branch
BranchTrueFalseTotal
_confirm("cp $_[0] $_[1]") or return 1 2 0 2
197  
198 2 INFO "cp $_[0] $_[1]";
199  
200 2 ▶ File::Copy::copy @_ or
Branch: 1 branch
BranchTrueFalseTotal
File::Copy::copy(@_) or LOGCROAK("Cannot copy $_[0] to $_[1] ($!)") 1 1 2
201 LOGCROAK("Cannot copy $_[0] to $_[1] ($!)");
202 }
203  
204 =pod
205  
206 =item C<mv($source, $target)>
207  
208 Move a file from C<$source> to C<$target>. C<target> can be a directory.
209  
210 =cut
211  
212 ###############################################
213 0 ▶sub mv {
Subroutine mv: not called CC=3 SCAR=24.8
Pod documented
214 ###############################################
215  
216 0 local $Log::Log4perl::caller_depth =
217 $Log::Log4perl::caller_depth + 1;
218  
219 0 ▶ _confirm("mv $_[0] $_[1]") or return 1;
Branch: 1 branch
BranchTrueFalseTotal
_confirm("mv $_[0] $_[1]") or return 1 0 0 0
220  
221 0 INFO "mv $_[0] $_[1]";
222  
223 0 ▶ File::Copy::move @_ or
Branch: 1 branch
BranchTrueFalseTotal
File::Copy::move(@_) or LOGCROAK("Cannot move $_[0] to $_[1] ($!)") 0 0 0
224 LOGCROAK("Cannot move $_[0] to $_[1] ($!)");
225 }
226  
227 =pod
228  
229 =item C<download($url)>
230  
231 Download a file specified by C<$url> and store it under the
232 name returned by C<basename($url)>.
233  
234 =cut
235  
236 ###############################################
237 1 ▶sub download {
Subroutine download: called CC=3 SCAR=11.1
Pod documented
238 ###############################################
239 1 my($url) = @_;
240  
241 1 local $Log::Log4perl::caller_depth =
242 $Log::Log4perl::caller_depth + 1;
243  
244 1 INFO "download $url";
245  
246 1 ▶ _confirm("Downloading $url => ", basename($url)) or return 1;
Branch: 1 branch
BranchTrueFalseTotal
_confirm("Downloading $url => ", basename($url)) or return 1 1 0 1
247  
248 1 require LWP::UserAgent;
249 1 require HTTP::Request;
250 1 require HTTP::Status;
251  
252 1 my $ua = LWP::UserAgent->new();
253 1 my $request = HTTP::Request->new(GET => $url);
254 1 my $response = $ua->request($request, basename($_[0]));
255 1 my $rc = $response->code();
256
257 1 ▶ if($rc != HTTP::Status::RC_OK()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc != HTTP::Status::RC_OK()) 1 0 1
258 1 LOGCROAK("Cannot download $_[0] (",
259 $response->message(),
260 ")");
261 }
262  
263 0 return 1;
264 }
265  
266 =pod
267  
268 =item C<untar($tarball)>
269  
270 Untar the tarball in C<$tarball>, which typically adheres to the
271 C<someproject-X.XX.tgz> convention.
272 But regardless of whether the
273 archive actually contains a top directory C<someproject-X.XX>,
274 this function will behave if it had one. If it doesn't have one,
275 a new directory is created before the unpacking takes place. Unpacks
276 the tarball into the current directory, no matter where the tarfile
277 is located.
278 Please note that if you're
279 using a compressed tarball (.tar.gz or .tgz), you'll need
280 IO::Zlib installed.
281  
282 =cut
283  
284 ###############################################
285 3 ▶sub untar {
Subroutine untar: called CC=10 SCAR=25.8
Pod documented
286 ###############################################
287  
288 3 local $Log::Log4perl::caller_depth =
289 $Log::Log4perl::caller_depth + 1;
290  
291 3 ▶ LOGCROAK("untar called without defined tarfile") unless
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 and defined $_[0] 0 3 3
Condition33%1/3
@_ == 1 and defined $_[0]
!ll&&!rl&&r
003
MC/DC0%0/2
@_ == 1 and defined $_[0]
@_ == 1 defined $_[0]
Truth table33%1/3
A and B
A= @_ == 1B= defined $_[0]
ABresult
0X0
100
111
292 @_ == 1 and defined $_[0];
293  
294 3 ▶ _confirm "untar $_[0]" or return 1;
Branch: 1 branch
BranchTrueFalseTotal
_confirm("untar $_[0]") or return 1 3 0 3
295  
296 3 my($nice, $topdir, $namedir) = archive_sniff($_[0]);
297  
298 3 check_zlib($_[0]);
299 3 require Archive::Tar;
300 3 my $arch = Archive::Tar->new($_[0]);
301  
302 3 my @extracted = ();
303  
304 3 ▶ if($nice and $topdir eq $namedir) {
Branch: 2 branches
BranchTrueFalseTotal
if ($nice and $topdir eq $namedir) { } 1 2 3
elsif ($nice) { } 0 2 2
Condition66%2/3
$nice and $topdir eq $namedir
!ll&&!rl&&r
201
MC/DC50%1/2
$nice and $topdir eq $namedir
$nice $topdir eq $namedir
Truth table66%2/3
A and B
A= $niceB= $topdir eq $namedir
ABresult
0X0
100
111
305 1 DEBUG "Nice archive, extracting to subdir $topdir";
306 1 @extracted = $arch->extract();
307 } elsif($nice) {
308 0 DEBUG "Not-so-nice archive topdir=$topdir namedir=$namedir";
309 # extract as topdir
310 0 @extracted = $arch->extract();
311 0 ▶ rename $topdir, $namedir or
Branch: 1 branch
BranchTrueFalseTotal
rename $topdir, $namedir or LOGCROAK("Can't rename $topdir, $namedir") 0 0 0
312 LOGCROAK("Can't rename $topdir, $namedir");
313 } else {
314 2 ▶ LOGCROAK("no topdir") unless defined $topdir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $topdir 0 2 2
315 2 DEBUG "Not-so-nice archive (no topdir), extracting to subdir $topdir";
316 2 $topdir = basename $topdir;
317 2 mkd($topdir);
318 2 cd($topdir);
319 2 @extracted = $arch->extract();
320 2 cdback();
321 }
322  
323 3 ▶ if( !@extracted ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@extracted) 0 3 3
324 0 LOGCROAK "Archive $_[0] was empty.";
325 }
326  
327 3 return $topdir;
328 }
329  
330 =pod
331  
332 =item C<untar_in($tar_file, $dir)>
333  
334 Untar the tarball in C<$tgz_file> in directory C<$dir>. Create
335 C<$dir> if it doesn't exist yet.
336  
337 =cut
338  
339 ###############################################
340 0 ▶sub untar_in {
Subroutine untar_in: not called CC=6 SCAR=37.4
Pod documented
341 ###############################################
342 0 my($tar_file, $dir) = @_;
343  
344 0 local $Log::Log4perl::caller_depth =
345 $Log::Log4perl::caller_depth + 1;
346  
347 0 ▶ LOGCROAK("not enough arguments") if
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tar_file and defined $dir 0 0 0
Condition0%0/3
defined $tar_file and defined $dir
!ll&&!rl&&r
000
MC/DC0%0/2
defined $tar_file and defined $dir
defined $tar_file defined $dir
Truth table0%0/3
A and B
A= defined $tar_fileB= defined $dir
ABresult
0X0
100
111
348 ! defined $tar_file or ! defined $dir;
349  
350 0 ▶ _confirm "Untarring $tar_file in $dir" or return 1;
Branch: 1 branch
BranchTrueFalseTotal
_confirm("Untarring $tar_file in $dir") or return 1 0 0 0
351  
352 0 ▶ mkd($dir) unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
353  
354 0 my $tar_file_abs = rel2abs($tar_file);
355  
356 0 cd($dir);
357  
358 0 check_zlib($tar_file_abs);
359 0 require Archive::Tar;
360 0 my $arch = Archive::Tar->new("$tar_file_abs");
361 0 ▶ $arch->extract() or
Branch: 1 branch
BranchTrueFalseTotal
$arch->extract or LOGCROAK("Extract failed: ($!)") 0 0 0
362 LOGCROAK("Extract failed: ($!)");
363 0 cdback();
364 }
365  
366 =pod
367  
368 =item C<pick($prompt, $options, $default, $opts)>
369  
370 Ask the user to pick an item from a displayed list. C<$prompt>
371 is the text displayed, C<$options> is a referenc to an array of
372 choices, and C<$default> is the number (starting from 1, not 0)
373 of the default item. For example,
374  
375 pick("Pick a fruit", ["apple", "pear", "pineapple"], 3);
376  
377 will display the following:
378  
379 [1] apple
380 [2] pear
381 [3] pineapple
382 Pick a fruit [3]>
383  
384 If the user just hits I<Enter>, "pineapple" (the default value) will
385 be returned. Note that 3 marks the 3rd element of the list, and is
386 I<not> an index value into the array.
387  
388 If the user enters C<1>, C<2> or C<3>, the corresponding text string
389 (C<"apple">, C<"pear">, C<"pineapple"> will be returned by
390 C<pick()>.
391  
392 If the optional C<$opts> hash has C<{ tty =E<gt> 1 }> set, then
393 the user response will be expected from the console, not STDIN.
394  
395 =cut
396  
397 ##################################################
398 0 ▶sub pick {
Subroutine pick: not called CC=10 SCAR=47.0
Pod documented
399 ##################################################
400 0 my ($prompt, $options, $default, $opts) = @_;
401  
402 0 local $Log::Log4perl::caller_depth =
403 $Log::Log4perl::caller_depth + 1;
404  
405 0 my $default_int;
406 my %files;
407  
408 0 ▶ if(@_ < 3 or ref($options) ne "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 3 or ref $options ne "ARRAY") 0 0 0
Condition0%0/3
@_ < 3 or ref $options ne "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ < 3 or ref $options ne "ARRAY"
@_ < 3 ref $options ne "ARRAY"
Truth table0%0/3
A or B
A= @_ < 3B= ref $options ne "ARRAY"
ABresult
1X1
011
000
409 0 LOGCROAK("pick called with wrong #/type of args");
410 }
411
412 {
413 0 my $count = 0;
414  
415 0 my $user_prompt = "";
416  
417 0 foreach (@$options) {
418 0 $user_prompt .= "[" . ++$count . "] $_\n";
419 0 ▶ $default_int = $count if $count eq $default;
Branch: 1 branch
BranchTrueFalseTotal
if $count eq $default 0 0 0
420 0 $files{$count} = $_;
421 }
422
423 0 $user_prompt .= "$prompt [$default_int]> ";
424 0 my $input = user_input($user_prompt, $opts);
425  
426 0 ▶ $input = $default_int if !defined $input or !length($input);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input and length $input 0 0 0
Condition0%0/3
defined $input and length $input
!ll&&!rl&&r
000
MC/DC0%0/2
defined $input and length $input
defined $input length $input
Truth table0%0/3
A and B
A= defined $inputB= length $input
ABresult
0X0
100
111
427  
428 0 ▶ redo if $input !~ /^\d+$/ or
Branch: 1 branch
BranchTrueFalseTotal
if not $input =~ /^\d+$/ or $input == 0 or $input > scalar @$options 0 0 0
Condition0%0/6
not $input =~ /^\d+$/ or $input == 0 or $input > scalar @$options
l!l&&r!l&&!r
000
not $input =~ /^\d+$/ or $input == 0
l!l&&r!l&&!r
000
MC/DC0%0/3
not $input =~ /^\d+$/ or $input == 0 or $input > scalar @$options
not $input =~ /^\d+$/ $input == 0 $input > scalar @$options
Truth table0%0/4
not(A) or B or C
A= not $input =~ /^\d+$/B= $input == 0C= $input > scalar @$options
ABCresult
1XX1
01X1
0011
0000
429 $input == 0 or
430 $input > scalar @$options;
431 0 return "$files{$input}";
432 }
433 }
434  
435 =pod
436  
437 =item C<ask($prompt, $default, $opts)>
438  
439 Ask the user to either hit I<Enter> and select the displayed default
440 or to type in another string.
441  
442 If the optional C<$opts> hash has C<{ tty =E<gt> 1 }> set, then
443 the user response will be expected from the console, not STDIN.
444  
445 =cut
446  
447 ##################################################
448 0 ▶sub ask {
Subroutine ask: not called CC=4 SCAR=30.0
Pod documented
449 ##################################################
450 0 my ($prompt, $default, $opts) = @_;
451  
452 0 ▶ $opts = {} if !defined $opts;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts 0 0 0
453  
454 0 local $Log::Log4perl::caller_depth =
455 $Log::Log4perl::caller_depth + 1;
456  
457 0 ▶ if(@_ < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 2) 0 0 0
458 0 LOGCROAK("ask() called with wrong # of args");
459 }
460  
461 0 my $value = user_input("$prompt [$default]> ", $opts);
462 0 ▶ $value = $default if $value eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "" 0 0 0
463  
464 0 return $value;
465 }
466  
467 ##################################################
468 0 ▶sub user_prompt {
Subroutine user_prompt: not called CC=8 SCAR=42.8
Pod undocumented
469 ##################################################
470 0 my ($prompt, $opts) = @_;
471  
472 0 ▶ $opts = {} if !defined $opts;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts 0 0 0
473  
474 0 my $fh = *STDERR;
475 0 my $old_stderr;
476 0 ▶ if( $opts->{ tty } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"tty"}) 0 0 0
477 0 ▶ open $old_stderr, ">&", \*STDERR or
Branch: 1 branch
BranchTrueFalseTotal
open $old_stderr, ">&", \*STDERR or die "Can't dup STDERR: $!" 0 0 0
478 die "Can't dup STDERR: $!";
479 0 ▶ open $fh, ">>", '/dev/tty' or
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">>", "/dev/tty" or die "Cannot open /dev/tty ($!)" 0 0 0
480 die "Cannot open /dev/tty ($!)";
481 }
482  
483 0 ▶ print $fh $prompt
Branch: 1 branch
BranchTrueFalseTotal
print $fh $prompt or die "Couldn't write to ${fh}: ($!)" 0 0 0
484 or die "Couldn't write to $fh: ($!)";
485  
486 0 ▶ if( $opts->{ tty } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"tty"}) 0 0 0
487 0 close $fh;
488 0 ▶ open STDERR, ">&", $old_stderr or
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", $old_stderr or die "Can't reset STDERR" 0 0 0
489 die "Can't reset STDERR";
490 }
491  
492 0 return 1;
493 }
494  
495 ##################################################
496 0 ▶sub user_input {
Subroutine user_input: not called CC=5 SCAR=34.0
Pod undocumented
497 ##################################################
498 0 my ($prompt, $opts) = @_;
499  
500 0 ▶ $opts = {} if !defined $opts;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts 0 0 0
501  
502 0 user_prompt( $prompt );
503  
504 0 my $fh = *STDIN;
505  
506 0 ▶ if( $opts->{ tty } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"tty"}) 0 0 0
507 0 ▶ open $fh, "<", '/dev/tty' or
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", "/dev/tty" or die "Cannot open /dev/tty ($!)" 0 0 0
508 die "Cannot open /dev/tty ($!)";
509 }
510  
511 0 my $input = <$fh>;
512 0 ▶ chomp $input if defined $input;
Branch: 1 branch
BranchTrueFalseTotal
if defined $input 0 0 0
513  
514 0 return $input;
515 }
516  
517 =pod
518  
519 =item C<mkd($dir)>
520  
521 Create a directory of arbitrary depth, just like C<File::Path::mkpath>.
522  
523 =cut
524  
525 ###############################################
526 4 ▶sub mkd {
Subroutine mkd: called CC=3 SCAR=11.0
Pod documented
527 ###############################################
528  
529 4 local $Log::Log4perl::caller_depth =
530 $Log::Log4perl::caller_depth + 1;
531  
532 4 ▶ _confirm "mkd @_" or return 1;
Branch: 1 branch
BranchTrueFalseTotal
_confirm("mkd @_") or return 1 4 0 4
533  
534 4 INFO "mkpath @_";
535  
536 4 ▶ mkpath @_ or
Branch: 1 branch
BranchTrueFalseTotal
mkpath(@_) or LOGCROAK("Cannot mkdir @_ ($!)") 3 1 4
537 LOGCROAK("Cannot mkdir @_ ($!)");
538 }
539  
540 =pod
541  
542 =item C<rmf($dir)>
543  
544 Delete a directory and all of its descendents, just like C<rm -rf>
545 in the shell.
546  
547 =cut
548  
549 ###############################################
550 3 ▶sub rmf {
Subroutine rmf: called CC=4 SCAR=14.0
Pod documented
551 ###############################################
552  
553 3 local $Log::Log4perl::caller_depth =
554 $Log::Log4perl::caller_depth + 1;
555  
556 3 ▶ _confirm "rmf $_[0]" or return 1;
Branch: 1 branch
BranchTrueFalseTotal
_confirm("rmf $_[0]") or return 1 3 0 3
557  
558 3 ▶ if(!-e $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $_[0]) 2 1 3
559 2 DEBUG "$_[0] doesn't exist - ignored";
560 2 return;
561 }
562  
563 1 INFO "rmtree @_";
564  
565 1 ▶ rmtree $_[0] or
Branch: 1 branch
BranchTrueFalseTotal
rmtree($_[0]) or LOGCROAK("Cannot rmtree $_[0] ($!)") 1 0 1
566 LOGCROAK("Cannot rmtree $_[0] ($!)");
567 }
568  
569 =pod
570  
571 =item C<cd($dir)>
572  
573 chdir to the given directory. If you don't want to have cd() modify
574 the internal directory stack (used for subsequent cdback() calls),
575 set the stack_update parameter to a false value:
576  
577 cd($dir, {stack_update => 0});
578  
579 =cut
580  
581 ###############################################
582 15 ▶sub cd {
Subroutine cd: called CC=5 SCAR=16.2
Pod documented
583 ###############################################
584  
585 15 local $Log::Log4perl::caller_depth =
586 $Log::Log4perl::caller_depth + 1;
587  
588 15 INFO "cd $_[0]";
589  
590 15 my $opts = { stack_update => 1 };
591 15 ▶ $opts = $_[1] if ref $_[1] eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[1] eq "HASH" 5 10 15
592  
593 15 ▶ if ($opts->{stack_update}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"stack_update"}) 10 5 15
594 10 my $cwd = getcwd();
595 10 ▶ if(! defined $cwd) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cwd) 0 10 10
596 0 LOGCROAK("Cannot getcwd ($!)"); ;
597 }
598 10 push @DIR_STACK, $cwd;
599 }
600  
601 15 ▶ chdir($_[0]) or
Branch: 1 branch
BranchTrueFalseTotal
chdir $_[0] or LOGCROAK("Cannot cd $_[0] ($!)") 14 1 15
602 LOGCROAK("Cannot cd $_[0] ($!)");
603 }
604  
605 =pod
606  
607 =item C<cdback()>
608  
609 chdir back to the last directory before a previous C<cd>. If the
610 option C<reset> is set, it goes all the way back to the beginning of the
611 directory stack, i.e. no matter how many cd() calls were made in between,
612 it'll go back to the original directory:
613  
614 # go all the way back
615 cdback( { reset => 1 } );
616  
617 =cut
618  
619 ###############################################
620 5 ▶sub cdback {
Subroutine cdback: called CC=5 SCAR=16.2
Pod documented
621 ###############################################
622 5 my( $opts ) = @_;
623  
624 5 ▶ $opts = {} if !defined $opts;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts 4 1 5
625  
626 5 local $Log::Log4perl::caller_depth =
627 $Log::Log4perl::caller_depth + 1;
628  
629 5 ▶ LOGCROAK("cd stack empty") unless @DIR_STACK;
Branch: 1 branch
BranchTrueFalseTotal
unless @Sysadm::Install::DIR_STACK 0 5 5
630  
631 5 ▶ if( $opts->{ reset } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"reset"}) 1 4 5
632 1 @DIR_STACK = ( $DIR_STACK[0] );
633 }
634  
635 5 my $old_dir = pop @DIR_STACK;
636  
637 5 ▶ LOGCROAK("Directory stack empty")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $old_dir 0 5 5
638 if ! defined $old_dir;
639  
640 5 INFO "cdback to $old_dir";
641 5 cd($old_dir, {stack_update => 0});
642 }
643  
644 =pod
645  
646 =item C<make()>
647  
648 Call C<make> in the shell.
649  
650 =cut
651  
652 ###############################################
653 0 ▶sub make {
Subroutine make: not called CC=3 SCAR=24.8
Pod documented
654 ###############################################
655  
656 0 local $Log::Log4perl::caller_depth =
657 $Log::Log4perl::caller_depth + 1;
658  
659 0 ▶ _confirm "make @_" or return 1;
Branch: 1 branch
BranchTrueFalseTotal
_confirm("make @_") or return 1 0 0 0
660  
661 0 INFO "make @_";
662  
663 0 ▶ system("make @_") and
Branch: 1 branch
BranchTrueFalseTotal
system "make @_" and LOGCROAK("Cannot make @_ ($!)") 0 0 0
664 LOGCROAK("Cannot make @_ ($!)");
665 }
666  
667 =pod
668  
669 =cut
670  
671 ###############################################
672 6 ▶sub check_zlib {
Subroutine check_zlib: called CC=3 SCAR=14.2
Pod undocumented
673 ###############################################
674 6 my($tar_file) = @_;
675  
676 6 ▶ if($tar_file =~ /\.tar\.gz\b|\.tgz\b/ and
Branch: 1 branch
BranchTrueFalseTotal
if ($tar_file =~ /\.tar\.gz\b|\.tgz\b/ and not Log::Log4perl::Util::module_available("IO::Zlib")) 0 6 6
Condition33%1/3
$tar_file =~ /\.tar\.gz\b|\.tgz\b/ and not Log::Log4perl::Util::module_available("IO::Zlib")
!ll&&!rl&&r
600
MC/DC0%0/2
$tar_file =~ /\.tar\.gz\b|\.tgz\b/ and not Log::Log4perl::Util::module_available("IO::Zlib")
$tar_file =~ /\.tar\.gz\b|\.tgz\b/ not Log::Log4perl::Util::module_available("IO::Zlib")
Truth table33%1/3
A and not(B)
A= $tar_file =~ /\.tar\.gz\b|\.tgz\b/B= not Log::Log4perl::Util::module_available("IO::Zlib")
ABresult
0X0
100
111
677 !Log::Log4perl::Util::module_available("IO::Zlib")) {
678  
679 0 LOGCROAK("$tar_file: Compressed tarballs can ",
680 "only be processed with IO::Zlib installed.");
681 }
682 }
683
684 #######################################
685 3 ▶sub archive_sniff {
Subroutine archive_sniff: called CC=6 SCAR=18.0
Pod undocumented
686 #######################################
687 3 my($name) = @_;
688  
689 3 local $Log::Log4perl::caller_depth =
690 $Log::Log4perl::caller_depth + 1;
691  
692 3 DEBUG "Sniffing archive '$name'";
693  
694 3 my ($dir) = ($name =~ /(.*?)\.(tar\.gz|tgz|tar)$/);
695
696 3 ▶ return 0 unless defined $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dir 0 3 3
697  
698 3 $dir = basename($dir);
699 3 DEBUG "dir=$dir";
700  
701 3 my $topdir;
702  
703 3 check_zlib($name);
704  
705 3 require Archive::Tar;
706 3 my $tar = Archive::Tar->new($name);
707  
708 3 my @names = $tar->list_files(["name"]);
709
710 3 ▶ LOGCROAK("Archive $name is empty") unless @names;
Branch: 1 branch
BranchTrueFalseTotal
unless @names 0 3 3
711  
712 3 (my $archdir = $names[0]) =~ s#/.*##;
713  
714 3 DEBUG "archdir=$archdir";
715  
716 3 for my $name (@names) {
717 7 ▶ next if $name eq "./";
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "./" 0 7 7
718 7 $name =~ s#^\./##;
719 7 ($topdir = $name) =~ s#/.*##;
720 7 ▶ if($topdir ne $archdir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($topdir ne $archdir) 2 5 7
721 2 return (0, $dir, $dir);
722 }
723 }
724  
725 1 DEBUG "Return $topdir $dir";
726  
727 1 return (1, $topdir, $dir);
728 }
729  
730 =pod
731  
732 =item C<pie($coderef, $filename, ...)>
733  
734 Simulate "perl -pie 'do something' file". Edits files in-place. Expects
735 a reference to a subroutine as its first argument. It will read out the
736 file C<$filename> line by line and calls the subroutine setting
737 a localized C<$_> to the current line. The return value of the subroutine
738 will replace the previous value of the line.
739  
740 Example:
741  
742 # Replace all 'foo's by 'bar' in test.dat
743 pie(sub { s/foo/bar/g; $_; }, "test.dat");
744  
745 Works with one or more file names.
746  
747 If the files are known to contain UTF-8 encoded data, and you want it
748 to be read/written as a Unicode strings, use the C<utf8> option:
749  
750 pie(sub { s/foo/bar/g; $_; }, "test.dat", { utf8 => 1 });
751  
752 =cut
753  
754 ###############################################
755 1 ▶sub pie {
Subroutine pie: called CC=8 SCAR=22.5
Pod documented
756 ###############################################
757 1 my($coderef, @files) = @_;
758  
759 1 my $options = {};
760  
761 1 ▶ if(defined $files[-1] and
Branch: 1 branch
BranchTrueFalseTotal
if (defined $files[-1] and ref $files[-1] eq "HASH") 0 1 1
Condition33%1/3
defined $files[-1] and ref $files[-1] eq "HASH"
!ll&&!rl&&r
010
MC/DC0%0/2
defined $files[-1] and ref $files[-1] eq "HASH"
defined $files[-1] ref $files[-1] eq "HASH"
Truth table33%1/3
A and B
A= defined $files[-1]B= ref $files[-1] eq "HASH"
ABresult
0X0
100
111
762 ref $files[-1] eq "HASH") {
763 0 $options = pop @files;
764 }
765  
766 1 local $Log::Log4perl::caller_depth =
767 $Log::Log4perl::caller_depth + 1;
768  
769 1 for my $file (@files) {
770  
771 1 ▶ _confirm "editing $file in-place" or next;
Branch: 1 branch
BranchTrueFalseTotal
_confirm("editing $file in-place") or next 1 0 1
772  
773 1 my $out = "";
774  
775 1 ▶ open FILE, "<$file" or
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "<$file" or LOGCROAK("Cannot open $file ($!)") 1 0 1
776 LOGCROAK("Cannot open $file ($!)");
777  
778 1 ▶ if( $options->{utf8} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"utf8"}) 0 1 1
779 0 binmode FILE, ":utf8";
780 }
781  
782 1 ▶ while(<FILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FILE)) 3 1 4
783 3 $out .= $coderef->($_);
784 }
785 1 close FILE;
786  
787 1 blurt($out, $file, $options);
788 }
789 }
790  
791 =pod
792  
793 =item C<plough($coderef, $filename, ...)>
794  
795 Simulate "perl -ne 'do something' file". Iterates over all lines
796 of all input files and calls the subroutine provided as the first argument.
797  
798 Example:
799  
800 # Print all lines containing 'foobar'
801 plough(sub { print if /foobar/ }, "test.dat");
802  
803 Works with one or more file names.
804  
805 If the files are known to contain UTF-8 encoded data, and you want it
806 to be read into Unicode strings, use the C<utf8> option:
807  
808 plough(sub { print if /foobar/ }, "test.dat", { utf8 => 1 });
809  
810 =cut
811  
812 ###############################################
813 1 ▶sub plough {
Subroutine plough: called CC=8 SCAR=22.7
Pod documented
814 ###############################################
815 1 my($coderef, @files) = @_;
816  
817 1 my $options = {};
818  
819 1 ▶ if(defined $files[-1] and
Branch: 1 branch
BranchTrueFalseTotal
if (defined $files[-1] and ref $files[-1] eq "HASH") 0 1 1
Condition33%1/3
defined $files[-1] and ref $files[-1] eq "HASH"
!ll&&!rl&&r
010
MC/DC0%0/2
defined $files[-1] and ref $files[-1] eq "HASH"
defined $files[-1] ref $files[-1] eq "HASH"
Truth table33%1/3
A and B
A= defined $files[-1]B= ref $files[-1] eq "HASH"
ABresult
0X0
100
111
820 ref $files[-1] eq "HASH") {
821 0 $options = pop @files;
822 }
823  
824 1 local $Log::Log4perl::caller_depth =
825 $Log::Log4perl::caller_depth + 1;
826  
827 1 for my $file (@files) {
828  
829 1 ▶ _confirm "Ploughing through $file" or next;
Branch: 1 branch
BranchTrueFalseTotal
_confirm("Ploughing through $file") or next 1 0 1
830  
831 1 my $out = "";
832  
833 1 ▶ open FILE, "<$file" or
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "<$file" or LOGCROAK("Cannot open $file ($!)") 1 0 1
834 LOGCROAK("Cannot open $file ($!)");
835  
836 1 ▶ if( $options->{utf8} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"utf8"}) 0 1 1
837 0 binmode FILE, ":utf8";
838 }
839  
840 1 ▶ while(<FILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FILE)) 3 1 4
841 3 $coderef->($_);
842 }
843 1 close FILE;
844 }
845 }
846  
847 =pod
848  
849 =item C<my $data = slurp($file, $options)>
850  
851 Slurps in the file and returns a scalar with the file's content. If
852 called without argument, data is slurped from STDIN or from any files
853 provided on the command line (like E<lt>E<gt> operates).
854  
855 If the file is known to contain UTF-8 encoded data and you want to
856 read it in as a Unicode string, use the C<utf8> option:
857  
858 my $unicode_string = slurp( $file, {utf8 => 1} );
859  
860 =cut
861  
862 ###############################################
863 9 ▶sub slurp {
Subroutine slurp: called CC=5 SCAR=16.1
Pod documented
864 ###############################################
865 9 my($file, $options) = @_;
866  
867 9 ▶ $options = {} unless defined $options;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options 3 6 9
868  
869 9 local $Log::Log4perl::caller_depth =
870 $Log::Log4perl::caller_depth + 1;
871  
872 9 my $from_file = defined($file);
873  
874 9 local $/ = undef;
875  
876 9 my $data;
877  
878 9 ▶ if($from_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from_file) { } 8 1 9
879 8 INFO "Slurping data from $file";
880 8 ▶ open FILE, "<$file" or
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "<$file" or LOGCROAK("Cannot open $file ($!)") 8 0 8
881 LOGCROAK("Cannot open $file ($!)");
882 8 binmode FILE; # Win32 wants that
883 8 ▶ if( exists $options->{utf8} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"utf8"}) 1 7 8
884 1 binmode FILE, ":utf8";
885 }
886 8 $data = <FILE>;
887 8 close FILE;
888 8 DEBUG "Read ", snip($data, $DATA_SNIPPED_LEN), " from $file";
889 } else {
890 1 INFO "Slurping data from <>";
891 1 $data = <>;
892 1 DEBUG "Read ", snip($data, $DATA_SNIPPED_LEN), " from <>";
893 }
894  
895 9 return $data;
896 }
897  
898 =pod
899  
900 =item C<blurt($data, $file, $options)>
901  
902 Opens a new file, prints the data in C<$data> to it and closes the file. If
903 C<$options-E<gt>{append}> is set to a true value, data will be appended to the
904 file. Default is false, existing files will be overwritten.
905  
906 If the string is a Unicode string, use the C<utf8> option:
907  
908 blurt( $unicode_string, $file, {utf8 => 1} );
909  
910 =cut
911  
912 ###############################################
913 7 ▶sub blurt {
Subroutine blurt: called CC=12 SCAR=27.6
Pod documented
914 ###############################################
915 7 my($data, $file, $options) = @_;
916  
917 # legacy signature
918 7 ▶ if(defined $options and ref $options eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options and ref $options eq "") 0 7 7
Condition66%2/3
defined $options and ref $options eq ""
!ll&&!rl&&r
520
MC/DC0%0/2
defined $options and ref $options eq ""
defined $options ref $options eq ""
Truth table66%2/3
A and B
A= defined $optionsB= ref $options eq ""
ABresult
0X0
100
111
919 0 $options = { append => 1 };
920 }
921  
922 7 ▶ $options = {} unless defined $options;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options 5 2 7
923  
924 7 local $Log::Log4perl::caller_depth =
925 $Log::Log4perl::caller_depth + 1;
926  
927 7 ▶ $options->{append} = 0 unless defined $options->{append};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"append"} 7 0 7
928  
929 7 ▶ _confirm(($options->{append} ? "Appending" : "Writing") . " " .
Branch: 2 branches
BranchTrueFalseTotal
_confirm(($options->{"append"} ? "Appending" : "Writing") . ' ' . length($data) . " bytes to $file") or return 1 6 1 7
$options->{'append'} ? : 0 7 7
930 length($data) . " bytes to $file") or return 1;
931  
932 6 ▶ open FILE, ">" . ($options->{append} ? ">" : "") . $file
Branch: 2 branches
BranchTrueFalseTotal
open FILE, '>' . ($options->{"append"} ? ">" : "") . $file or LOGCROAK("Cannot open $file for writing ($!)") 6 0 6
$options->{'append'} ? : 0 6 6
933 or
934 LOGCROAK("Cannot open $file for writing ($!)");
935  
936 6 binmode FILE; # Win32 wants that
937  
938 6 ▶ if( $options->{utf8} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"utf8"}) 0 6 6
939 0 binmode FILE, ":utf8";
940 }
941  
942 6 ▶ print FILE $data
Branch: 1 branch
BranchTrueFalseTotal
print FILE $data or LOGCROAK("Cannot write to $file ($!)") 6 0 6
943 or
944 LOGCROAK("Cannot write to $file ($!)");
945 6 ▶ close FILE
Branch: 1 branch
BranchTrueFalseTotal
close FILE or LOGCROAK("Cannot close $file ($!)") 6 0 6
946 or
947 LOGCROAK("Cannot close $file ($!)");
948  
949 6 DEBUG "Wrote ", snip($data, $DATA_SNIPPED_LEN), " to $file";
950 }
951  
952 =pod
953  
954 =item C<blurt_atomic($data, $file, $options)>
955  
956 Write the data in $data to a file $file, guaranteeing that the operation
957 will either complete fully or not at all. This is accomplished by first
958 writing to a temporary file which is then rename()ed to the target file.
959  
960 Unlike in C<blurt>, there is no C<$append> mode in C<blurt_atomic>.
961  
962 If the string is a Unicode string, use the C<utf8> option:
963  
964 blurt_atomic( $unicode_string, $file, {utf8 => 1} );
965  
966 =cut
967  
968 ###############################################
969 1 ▶sub blurt_atomic {
Subroutine blurt_atomic: called CC=4 SCAR=14.2
Pod documented
970 ###############################################
971 1 my($data, $file, $options) = @_;
972  
973 1 ▶ _confirm("Writing atomically " .
Branch: 1 branch
BranchTrueFalseTotal
_confirm('Writing atomically ' . length($data) . " bytes to $file") or return 1 1 0 1
974 length($data) . " bytes to $file") or return 1;
975  
976 1 ▶ $options = {} unless defined $options;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options 1 0 1
977  
978 1 my($fh, $tmpname) = tempfile(DIR => dirname($file));
979  
980 1 blurt($data, $tmpname, $options);
981  
982 1 close $fh;
983  
984 1 ▶ rename $tmpname, $file or
Branch: 1 branch
BranchTrueFalseTotal
rename $tmpname, $file or LOGDIE("Can't rename $tmpname to $file") 1 0 1
985 LOGDIE "Can't rename $tmpname to $file";
986  
987 1 DEBUG "Wrote ", snip($data, $DATA_SNIPPED_LEN), " atomically to $file";
988 }
989  
990 =pod
991  
992 =item C<($stdout, $stderr, $exit_code) = tap($cmd, @args)>
993  
994 Run a command $cmd in the shell, and pass it @args as args.
995 Capture STDOUT and STDERR, and return them as strings. If
996 C<$exit_code> is 0, the command succeeded. If it is different,
997 the command failed and $exit_code holds its exit code.
998  
999 Please note that C<tap()> is limited to single shell
1000 commands, it won't work with output redirectors (C<ls E<gt>/tmp/foo>
1001 2E<gt>&1).
1002  
1003 In default mode, C<tap()> will concatenate the command and args
1004 given and create a shell command line by redirecting STDERR to a temporary
1005 file. C<tap("ls", "/tmp")>, for example, will result in
1006  
1007 'ls' '/tmp' 2>/tmp/sometempfile |
1008  
1009 Note that all commands are protected by single quotes to make sure
1010 arguments containing spaces are processed as singles, and no globbing
1011 happens on wildcards. Arguments containing single quotes or backslashes
1012 are escaped properly.
1013  
1014 If quoting is undesirable, C<tap()> accepts an option hash as
1015 its first parameter,
1016  
1017 tap({no_quotes => 1}, "ls", "/tmp/*");
1018  
1019 which will suppress any quoting:
1020  
1021 ls /tmp/* 2>/tmp/sometempfile |
1022  
1023 Or, if you prefer double quotes, use
1024  
1025 tap({double_quotes => 1}, "ls", "/tmp/$VAR");
1026  
1027 wrapping all args so that shell variables are interpolated properly:
1028  
1029 "ls" "/tmp/$VAR" 2>/tmp/sometempfile |
1030  
1031 Another option is "utf8" which runs the command in a terminal set to
1032 UTF8.
1033  
1034 Error handling: By default, tap() won't raise an error if the command's
1035 return code is nonzero, indicating an error reported by the shell. If
1036 bailing out on errors is requested to avoid return code checking by
1037 the script, use the raise_error option:
1038  
1039 tap({raise_error => 1}, "ls", "doesn't exist");
1040  
1041 In DEBUG mode, C<tap> logs the entire stdout/stderr output, which
1042 can get too verbose at times. To limit the number of bytes logged, use
1043 the C<stdout_limit> and C<stderr_limit> options
1044  
1045 tap({stdout_limit => 10}, "echo", "123456789101112");
1046  
1047 =cut
1048  
1049 ###############################################
1050 5 ▶sub tap {
Subroutine tap: called CC=13 SCAR=27.8
Pod documented
1051 ###############################################
1052 5 my(@args) = @_;
1053  
1054 5 my $options = {};
1055  
1056 5 ▶ if(defined $args[-1] and
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args[-1] and ref $args[-1] eq "HASH") 0 5 5
Condition33%1/3
defined $args[-1] and ref $args[-1] eq "HASH"
!ll&&!rl&&r
050
MC/DC0%0/2
defined $args[-1] and ref $args[-1] eq "HASH"
defined $args[-1] ref $args[-1] eq "HASH"
Truth table33%1/3
A and B
A= defined $args[-1]B= ref $args[-1] eq "HASH"
ABresult
0X0
100
111
1057 ref $args[-1] eq "HASH") {
1058 0 $options = pop @args;
1059 }
1060  
1061 5 local $Log::Log4perl::caller_depth =
1062 $Log::Log4perl::caller_depth + 1;
1063  
1064 5 ▶ _confirm "tapping @args" or return 1;
Branch: 1 branch
BranchTrueFalseTotal
_confirm("tapping @args") or return 1 5 0 5
1065  
1066 5 my $opts = {};
1067  
1068 5 ▶ $opts = shift @args if ref $args[0] eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if ref $args[0] eq "HASH" 3 2 5
1069  
1070 5 my $tmpfh = File::Temp->new(UNLINK => 1, SUFFIX => '.dat');
1071 5 my $tmpfile = $tmpfh->filename();
1072  
1073 5 DEBUG "tempfile $tmpfile created";
1074  
1075 5 my $cmd;
1076  
1077 5 ▶ if($opts->{no_quotes}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'no_quotes'}) { } 0 5 5
elsif ($opts->{'double_quotes'}) { } 0 5 5
1078 0 $cmd = join ' ', @args;
1079 } elsif($opts->{double_quotes}) {
1080 0 $cmd = join ' ', map { qquote($_, ":shell") } @args;
1081 } else {
1082 # Default mode: Single quotes
1083 5 $cmd = join ' ', map { quote($_, ":shell") } @args;
1084 }
1085
1086 5 $cmd = "$cmd 2>$tmpfile |";
1087 5 INFO "tapping $cmd";
1088  
1089 5 ▶ open PIPE, $cmd or
Branch: 1 branch
BranchTrueFalseTotal
open PIPE, $cmd or LOGCROAK("open $cmd | failed ($!)") 5 0 5
1090 LOGCROAK("open $cmd | failed ($!)");
1091
1092 5 ▶ if( $options->{utf8} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"utf8"}) 0 5 5
1093 0 binmode PIPE, ":utf8";
1094 }
1095  
1096 5 my $stdout = join '', <PIPE>;
1097 5 close PIPE;
1098  
1099 5 my $exit_code = $?;
1100  
1101 5 my $stderr = slurp($tmpfile, $options);
1102  
1103 5 ▶ if( $opts->{ stderr_limit } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"stderr_limit"}) 0 5 5
1104 0 $stderr = snip( $stderr, $opts->{ stderr_limit } );
1105 }
1106  
1107 5 ▶ if($exit_code != 0 and $opts->{raise_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit_code != 0 and $opts->{"raise_error"}) 1 4 5
Condition66%2/3
$exit_code != 0 and $opts->{"raise_error"}
!ll&&!rl&&r
401
MC/DC50%1/2
$exit_code != 0 and $opts->{"raise_error"}
$exit_code != 0 $opts->{"raise_error"}
Truth table66%2/3
A and B
A= $exit_code != 0B= $opts->{"raise_error"}
ABresult
0X0
100
111
1108 1 LOGCROAK("tap $cmd | failed ($stderr)");
1109 }
1110  
1111 4 ▶ if( $opts->{ stdout_limit } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"stdout_limit"}) 1 3 4
1112 1 $stdout = snip( $stdout, $opts->{ stdout_limit } );
1113 }
1114  
1115 4 DEBUG "tap $cmd results: rc=$exit_code stderr=[$stderr] stdout=[$stdout]";
1116  
1117 4 return ($stdout, $stderr, $exit_code);
1118 }
1119  
1120 =pod
1121  
1122 =item C<$quoted_string = qquote($string, [$metachars])>
1123  
1124 Put a string in double quotes and escape all sensitive characters so
1125 there's no unwanted interpolation.
1126 E.g., if you have something like
1127  
1128 print "foo!\n";
1129  
1130 and want to put it into a double-quoted string, it will look like
1131  
1132 "print \"foo!\\n\""
1133  
1134 Sometimes, not only backslashes and double quotes need to be escaped,
1135 but also the target environment's meta chars. A string containing
1136  
1137 print "$<\n";
1138  
1139 needs to have the '$' escaped like
1140  
1141 "print \"\$<\\n\";"
1142  
1143 if you want to reuse it later in a shell context:
1144  
1145 $ perl -le "print \"\$<\\n\";"
1146 1212
1147  
1148 C<qquote()> supports escaping these extra characters with its second,
1149 optional argument, consisting of a string listing all escapable characters:
1150  
1151 my $script = 'print "$< rocks!\\n";';
1152 my $escaped = qquote($script, '!$'); # Escape for shell use
1153 system("perl -e $escaped");
1154  
1155 => 1212 rocks!
1156  
1157 And there's a shortcut for shells: By specifying ':shell' as the
1158 metacharacters string, qquote() will actually use '!$`'.
1159  
1160 For example, if you wanted to run the perl code
1161  
1162 print "foobar\n";
1163  
1164 via
1165  
1166 perl -e ...
1167  
1168 on a box via ssh, you would use
1169  
1170 use Sysadm::Install qw(qquote);
1171  
1172 my $cmd = 'print "foobar!\n"';
1173 $cmd = "perl -e " . qquote($cmd, ':shell');
1174 $cmd = "ssh somehost " . qquote($cmd, ':shell');
1175  
1176 print "$cmd\n";
1177 system($cmd);
1178  
1179 and get
1180  
1181 ssh somehost "perl -e \"print \\\"foobar\\\!\\\\n\\\"\""
1182  
1183 which runs on C<somehost> without hickup and prints C<foobar!>.
1184  
1185 Sysadm::Install comes with a script C<one-liner> (installed in bin),
1186 which takes arbitrary perl code on STDIN and transforms it into
1187 a one-liner:
1188  
1189 $ one-liner
1190 Type perl code, terminate by CTRL-D
1191 print "hello\n";
1192 print "world\n";
1193 ^D
1194 perl -e "print \"hello\\n\"; print \"world\\n\"; "
1195  
1196 =cut
1197  
1198 ###############################################
1199 3 ▶sub qquote {
Subroutine qquote: called CC=3 SCAR=11.0
Pod documented
1200 ###############################################
1201 3 my($str, $metas) = @_;
1202  
1203 3 $str =~ s/([\\"])/\\$1/g;
1204  
1205 3 ▶ if(defined $metas) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $metas) 3 0 3
1206 3 ▶ $metas = '!$`' if $metas eq ":shell";
Branch: 1 branch
BranchTrueFalseTotal
if $metas eq ":shell" 1 2 3
1207 3 $metas =~ s/\]/\\]/g;
1208 3 $str =~ s/([$metas])/\\$1/g;
1209 }
1210  
1211 3 return "\"$str\"";
1212 }
1213  
1214 =pod
1215  
1216 =item C<$quoted_string = quote($string, [$metachars])>
1217  
1218 Similar to C<qquote()>, just puts a string in single quotes and
1219 escapes what needs to be escaped.
1220  
1221 Note that shells typically don't support escaped single quotes within
1222 single quotes, which means that
1223  
1224 $ echo 'foo\'bar'
1225 >
1226  
1227 is invalid and the shell waits until it finds a closing quote.
1228 Instead, there is an evil trick which gives the desired result:
1229  
1230 $ echo 'foo'\''bar' # foo, single quote, \, 2 x single quote, bar
1231 foo'bar
1232  
1233 It uses the fact that shells interpret back-to-back strings as one.
1234 The construct above consists of three back-to-back strings:
1235  
1236 (1) 'foo'
1237 (2) '
1238 (3) 'bar'
1239  
1240 which all get concatenated to a single
1241  
1242 foo'bar
1243  
1244 If you call C<quote()> with C<$metachars> set to ":shell", it will
1245 perform that magic behind the scenes:
1246  
1247 print quote("foo'bar");
1248 # prints: 'foo'\''bar'
1249  
1250 =cut
1251  
1252 ###############################################
1253 11 ▶sub quote {
Subroutine quote: called CC=5 SCAR=17.0
Pod documented
1254 ###############################################
1255 11 my($str, $metas) = @_;
1256  
1257 11 ▶ if(defined $metas and $metas eq ":shell") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $metas and $metas eq ':shell') { } 10 1 11
Condition66%2/3
defined $metas and $metas eq ":shell"
!ll&&!rl&&r
1010
MC/DC50%1/2
defined $metas and $metas eq ":shell"
defined $metas $metas eq ":shell"
Truth table66%2/3
A and B
A= defined $metasB= $metas eq ":shell"
ABresult
0X0
100
111
1258 10 $str =~ s/([\\])/\\$1/g;
1259 10 $str =~ s/(['])/'\\''/g;
1260 } else {
1261 1 $str =~ s/([\\'])/\\$1/g;
1262 }
1263  
1264 11 ▶ if(defined $metas and $metas ne ":shell") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $metas and $metas ne ":shell") 0 11 11
Condition66%2/3
defined $metas and $metas ne ":shell"
!ll&&!rl&&r
1100
MC/DC0%0/2
defined $metas and $metas ne ":shell"
defined $metas $metas ne ":shell"
Truth table66%2/3
A and B
A= defined $metasB= $metas ne ":shell"
ABresult
0X0
100
111
1265 0 $metas =~ s/\]/\\]/g;
1266 0 $str =~ s/([$metas])/\\$1/g;
1267 }
1268  
1269 11 return "\'$str\'";
1270 }
1271  
1272 =pod
1273  
1274 =item C<perm_cp($src, $dst, ...)>
1275  
1276 Read the C<$src> file's user permissions and modify all
1277 C<$dst> files to reflect the same permissions.
1278  
1279 =cut
1280  
1281 ######################################
1282 1 ▶sub perm_cp {
Subroutine perm_cp: called CC=3 SCAR=11.3
Pod documented
1283 ######################################
1284 # Lifted from Ben Okopnik's
1285 # http://www.linuxgazette.com/issue87/misc/tips/cpmod.pl.txt
1286  
1287 1 local $Log::Log4perl::caller_depth =
1288 $Log::Log4perl::caller_depth + 1;
1289  
1290 1 ▶ _confirm "perm_cp @_" or return 1;
Branch: 1 branch
BranchTrueFalseTotal
_confirm("perm_cp @_") or return 1 1 0 1
1291  
1292 1 ▶ LOGCROAK("usage: perm_cp src dst ...") if @_ < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 0 1 1
1293  
1294 1 my $perms = perm_get($_[0]);
1295 1 perm_set($_[1], $perms);
1296 }
1297  
1298 =pod
1299  
1300 =item C<owner_cp($src, $dst, ...)>
1301  
1302 Read the C<$src> file/directory's owner uid and group gid and apply
1303 it to $dst.
1304  
1305 For example: copy uid/gid of the containing directory to a file
1306 therein:
1307  
1308 use File::Basename;
1309  
1310 owner_cp( dirname($file), $file );
1311  
1312 Usually requires root privileges, just like chown does.
1313  
1314 =cut
1315  
1316 ######################################
1317 0 ▶sub owner_cp {
Subroutine owner_cp: not called CC=6 SCAR=37.4
Pod documented
1318 ######################################
1319 0 my($src, @dst) = @_;
1320  
1321 0 local $Log::Log4perl::caller_depth =
1322 $Log::Log4perl::caller_depth + 1;
1323  
1324 0 ▶ _confirm "owner_cp @_" or return 1;
Branch: 1 branch
BranchTrueFalseTotal
_confirm("owner_cp @_") or return 1 0 0 0
1325  
1326 0 ▶ LOGCROAK("usage: owner_cp src dst ...") if @_ < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 0 0 0
1327  
1328 0 my($uid, $gid) = (stat($src))[4,5];
1329  
1330 0 ▶ if(!defined $uid or !defined $gid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $uid and defined $gid) 0 0 0
Condition0%0/3
defined $uid and defined $gid
!ll&&!rl&&r
000
MC/DC0%0/2
defined $uid and defined $gid
defined $uid defined $gid
Truth table0%0/3
A and B
A= defined $uidB= defined $gid
ABresult
0X0
100
111
1331 0 LOGCROAK("stat of $src failed: $!");
1332 0 return undef;
1333 }
1334  
1335 0 ▶ if(!chown $uid, $gid, @dst ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (chown $uid, $gid, @dst) 0 0 0
1336 0 LOGCROAK("chown of ", join(" ", @dst), " failed: $!");
1337 0 return undef;
1338 }
1339  
1340 0 return 1;
1341 }
1342  
1343 =pod
1344  
1345 =item C<$perms = perm_get($filename)>
1346  
1347 Read the C<$filename>'s user permissions and owner/group.
1348 Returns an array ref to be
1349 used later when calling C<perm_set($filename, $perms)>.
1350  
1351 =cut
1352  
1353 ######################################
1354 1 ▶sub perm_get {
Subroutine perm_get: called CC=2 SCAR=7.0
Pod documented
1355 ######################################
1356 1 my($filename) = @_;
1357  
1358 1 local $Log::Log4perl::caller_depth =
1359 $Log::Log4perl::caller_depth + 1;
1360  
1361 1 ▶ my @stats = (stat $filename)[2,4,5] or
Branch: 1 branch
BranchTrueFalseTotal
my(@stats) = (stat $filename)[2, 4, 5] or LOGCROAK("Cannot stat $filename ($!)") 1 0 1
1362
1363 LOGCROAK("Cannot stat $filename ($!)");
1364  
1365 1 INFO "perm_get $filename (@stats)";
1366  
1367 1 return \@stats;
1368 }
1369  
1370 =pod
1371  
1372 =item C<perm_set($filename, $perms)>
1373  
1374 Set file permissions and owner of C<$filename>
1375 according to C<$perms>, which was previously
1376 acquired by calling C<perm_get($filename)>.
1377  
1378 =cut
1379  
1380 ######################################
1381 1 ▶sub perm_set {
Subroutine perm_set: called CC=4 SCAR=14.6
Pod documented
1382 ######################################
1383 1 my($filename, $perms) = @_;
1384  
1385 1 local $Log::Log4perl::caller_depth =
1386 $Log::Log4perl::caller_depth + 1;
1387  
1388 1 ▶ _confirm "perm_set $filename (@$perms)" or return 1;
Branch: 1 branch
BranchTrueFalseTotal
_confirm("perm_set $filename (@$perms)") or return 1 1 0 1
1389  
1390 1 ▶ chown($perms->[1], $perms->[2], $filename) or
Branch: 1 branch
BranchTrueFalseTotal
chown $perms->[1], $perms->[2], $filename or LOGCROAK("Cannot chown $filename ($!)") 1 0 1
1391
1392 LOGCROAK("Cannot chown $filename ($!)");
1393 1 ▶ chmod($perms->[0] & 07777, $filename) or
Branch: 1 branch
BranchTrueFalseTotal
chmod $perms->[0] & 4095, $filename or LOGCROAK("Cannot chmod $filename ($!)") 1 0 1
1394
1395 LOGCROAK("Cannot chmod $filename ($!)");
1396 }
1397  
1398 =pod
1399  
1400 =item C<sysrun($cmd)>
1401  
1402 Run a shell command via C<system()> and die() if it fails. Also
1403 works with a list of arguments, which are then interpreted as program
1404 name plus arguments, just like C<system()> does it.
1405  
1406 =cut
1407  
1408 ######################################
1409 0 ▶sub sysrun {
Subroutine sysrun: not called CC=4 SCAR=30.0
Pod documented
1410 ######################################
1411 0 my(@cmds) = @_;
1412  
1413 0 local $Log::Log4perl::caller_depth =
1414 $Log::Log4perl::caller_depth + 1;
1415  
1416 0 ▶ _confirm "sysrun: @cmds" or return 1;
Branch: 1 branch
BranchTrueFalseTotal
_confirm("sysrun: @cmds") or return 1 0 0 0
1417  
1418 0 ▶ LOGCROAK("usage: sysrun cmd ...") if @_ < 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 1 0 0 0
1419  
1420 0 ▶ system(@cmds) and
Branch: 1 branch
BranchTrueFalseTotal
system @cmds and LOGCROAK("@cmds failed ($!)") 0 0 0
1421 LOGCROAK("@cmds failed ($!)");
1422 }
1423  
1424 =pod
1425  
1426 =item C<hammer($cmd, $arg, ...)>
1427  
1428 Run a command in the shell and simulate a user hammering the
1429 ENTER key to accept defaults on prompts.
1430  
1431 =cut
1432  
1433 ######################################
1434 0 ▶sub hammer {
Subroutine hammer: not called CC=3 SCAR=24.8
Pod documented
1435 ######################################
1436 0 my(@cmds) = @_;
1437  
1438 0 require Expect;
1439  
1440 0 local $Log::Log4perl::caller_depth =
1441 $Log::Log4perl::caller_depth + 1;
1442  
1443 0 ▶ _confirm "Hammer: @cmds" or return 1;
Branch: 1 branch
BranchTrueFalseTotal
_confirm("Hammer: @cmds") or return 1 0 0 0
1444  
1445 0 my $exp = Expect->new();
1446 0 $exp->raw_pty(0);
1447  
1448 0 INFO "spawning: @cmds";
1449 0 $exp->spawn(@cmds);
1450  
1451 0 $exp->send_slow(0.1, "\n") for 1..199;
1452 0 $exp->expect(undef);
1453 }
1454  
1455 =pod
1456  
1457 =item C<say($text, ...)>
1458  
1459 Alias for C<print ..., "\n">, just like Perl6 is going to provide it.
1460  
1461 =cut
1462  
1463 ######################################
1464 0 ▶sub say {
Subroutine say: not called CC=1 SCAR=6.9
Pod documented
1465 ######################################
1466 0 print @_, "\n";
1467 }
1468  
1469 =pod
1470  
1471 =item C<sudo_me()>
1472  
1473 Check if the current script is running as root. If yes, continue. If not,
1474 restart the current script with all command line arguments is restarted
1475 under sudo:
1476  
1477 sudo scriptname args ...
1478  
1479 Make sure to call this before any C<@ARGV>-modifying functions like
1480 C<getopts()> have kicked in.
1481  
1482 =cut
1483  
1484 ######################################
1485 0 ▶sub sudo_me {
Subroutine sudo_me: not called CC=6 SCAR=37.4
Pod documented
1486 ######################################
1487 0 my($argv) = @_;
1488  
1489 0 local $Log::Log4perl::caller_depth =
1490 $Log::Log4perl::caller_depth + 1;
1491  
1492 0 ▶ _confirm "sudo_me" or return 1;
Branch: 1 branch
BranchTrueFalseTotal
_confirm("sudo_me") or return 1 0 0 0
1493  
1494 0 ▶ $argv = \@ARGV unless $argv;
Branch: 1 branch
BranchTrueFalseTotal
unless $argv 0 0 0
1495  
1496 # If we're not running as root,
1497 # re-invoke the script via sudo
1498 0 ▶ if($> != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($> != 0) 0 0 0
1499 0 DEBUG "Not running as root, calling sudo $0 @$argv";
1500 0 my $sudo = bin_find("sudo");
1501 0 ▶ LOGCROAK("Can't find sudo in PATH") unless $sudo;
Branch: 1 branch
BranchTrueFalseTotal
unless $sudo 0 0 0
1502 0 ▶ exec($sudo, $0, @$argv) or
Branch: 1 branch
BranchTrueFalseTotal
exec $sudo, $0, @$argv or LOGCROAK("exec failed!") 0 0 0
1503 LOGCROAK("exec failed!");
1504 }
1505 }
1506  
1507 =pod
1508  
1509 =item C<bin_find($program)>
1510  
1511 Search all directories in $PATH (the ENV variable) for an executable
1512 named $program and return the full path of the first hit. Returns
1513 C<undef> if the program can't be found.
1514  
1515 =cut
1516  
1517 ######################################
1518 2 ▶sub bin_find {
Subroutine bin_find: called CC=1 SCAR=0.0
Pod documented
1519 ######################################
1520 2 my($exe) = @_;
1521  
1522 # File::Which returns a list in list context, we just want the first
1523 # match.
1524 2 return scalar File::Which::which( $exe );
1525 }
1526  
1527 =pod
1528  
1529 =item C<fs_read_open($dir)>
1530  
1531 Opens a file handle to read the output of the following process:
1532  
1533 cd $dir; find ./ -xdev -print0 | cpio -o0 |
1534  
1535 This can be used to capture a file system structure.
1536  
1537 =cut
1538  
1539 ######################################
1540 0 ▶sub fs_read_open {
Subroutine fs_read_open: not called CC=6 SCAR=37.4
Pod documented
1541 ######################################
1542 0 my($dir, $options) = @_;
1543  
1544 0 ▶ $options = {} unless defined $options;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options 0 0 0
1545  
1546 0 local $Log::Log4perl::caller_depth =
1547 $Log::Log4perl::caller_depth + 1;
1548  
1549 0 my $find = bin_find("find");
1550 0 ▶ LOGCROAK("Cannot find 'find'") unless defined $find;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $find 0 0 0
1551  
1552 0 my $cpio = bin_find("cpio");
1553 0 ▶ LOGCROAK("Cannot find 'cpio'") unless defined $cpio;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cpio 0 0 0
1554  
1555 0 cd $dir;
1556
1557 0 my $cmd = "$find . -xdev -print0 | $cpio -o0 --quiet 2>/dev/null ";
1558  
1559 0 DEBUG "Reading from $cmd";
1560 0 ▶ open my $in, "$cmd |" or
Branch: 1 branch
BranchTrueFalseTotal
open my $in, "$cmd |" or LOGCROAK("Cannot open $cmd") 0 0 0
1561 LOGCROAK("Cannot open $cmd");
1562  
1563 0 ▶ binmode $in, ":utf8" if $options->{utf8};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"utf8"} 0 0 0
1564  
1565 0 cdback;
1566  
1567 0 return $in;
1568 }
1569  
1570 =pod
1571  
1572 =item C<fs_write_open($dir)>
1573  
1574 Opens a file handle to write to a
1575  
1576 | (cd $dir; cpio -i0)
1577  
1578 process to restore a file system structure. To be used in conjunction
1579 with I<fs_read_open>.
1580  
1581 =cut
1582  
1583 ######################################
1584 0 ▶sub fs_write_open {
Subroutine fs_write_open: not called CC=6 SCAR=37.4
Pod documented
1585 ######################################
1586 0 my($dir, $options) = @_;
1587  
1588 0 ▶ $options = {} unless defined $options;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options 0 0 0
1589  
1590 0 local $Log::Log4perl::caller_depth =
1591 $Log::Log4perl::caller_depth + 1;
1592  
1593 0 my $cpio = bin_find("cpio");
1594 0 ▶ LOGCROAK("Cannot find 'cpio'") unless defined $cpio;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cpio 0 0 0
1595  
1596 0 ▶ mkd $dir unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
1597  
1598 0 cd $dir;
1599  
1600 0 my $cmd = "$cpio -i0 --quiet";
1601  
1602 0 DEBUG "Writing to $cmd in dir $dir";
1603 0 ▶ open my $out, "| $cmd" or
Branch: 1 branch
BranchTrueFalseTotal
open my $out, "| $cmd" or LOGCROAK("Cannot open $cmd") 0 0 0
1604 LOGCROAK("Cannot open $cmd");
1605  
1606 0 ▶ binmode $out, ":utf8" if $options->{utf8};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"utf8"} 0 0 0
1607  
1608 0 cdback;
1609  
1610 0 return $out;
1611 }
1612  
1613 =pod
1614  
1615 =item C<pipe_copy($in, $out, [$bufsize])>
1616  
1617 Reads from $in and writes to $out, using sysread and syswrite. The
1618 buffer size used defaults to 4096, but can be set explicitly.
1619  
1620 =cut
1621  
1622 ######################################
1623 0 ▶sub pipe_copy {
Subroutine pipe_copy: not called CC=5 SCAR=34.0
Pod documented
1624 ######################################
1625 0 my($in, $out, $bufsize) = @_;
1626  
1627 0 local $Log::Log4perl::caller_depth =
1628 $Log::Log4perl::caller_depth + 1;
1629  
1630 0 ▶ $bufsize ||= 4096;
Condition0%0/2
$bufsize ||= 4096
l!l
00
MC/DC0%0/1
$bufsize ||= 4096
$bufsize
Truth table0%0/2
A
A= $bufsize
Aresult
11
00
1631 0 my $bytes = 0;
1632  
1633 0 INFO "Opening pipe (bufsize=$bufsize)";
1634 0 my $ret;
1635 0 ▶ while($ret = sysread($in, my $buf, $bufsize)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret = sysread($in, my $buf, $bufsize)) 0 0 0
1636 0 $bytes += length $buf;
1637 0 ▶ if (!defined syswrite $out, $buf) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined syswrite($out, $buf)) 0 0 0
1638 0 LOGCROAK("Write to pipe failed: ($!)");
1639 }
1640 }
1641 0 ▶ if (!defined $ret) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ret) 0 0 0
1642 0 LOGCROAK("Read from pipe failed: ($!)");
1643 }
1644 0 INFO "Closed pipe (bufsize=$bufsize, transferred=$bytes)";
1645 }
1646  
1647 =pod
1648  
1649 =item C<snip($data, $maxlen)>
1650  
1651 Format the data string in C<$data> so that it's only (roughly) $maxlen
1652 characters long and only contains printable characters.
1653  
1654 If C<$data> is longer than C<$maxlen>, it will be
1655 formatted like
1656  
1657 (22)[abcdef[snip=11]stuvw]
1658  
1659 indicating the length of the original string, the beginning, the
1660 end, and the number of 'snipped' characters.
1661  
1662 If C<$data> is shorter than $maxlen, it will be returned unmodified
1663 (except for unprintable characters replaced, see below).
1664  
1665 If C<$data> contains unprintable character's they are replaced by
1666 "." (the dot).
1667  
1668 =cut
1669  
1670 ###########################################
1671 24 ▶sub snip {
Subroutine snip: called CC=3 SCAR=11.0
Pod documented
1672 ###########################################
1673 24 my($data, $maxlen) = @_;
1674  
1675 24 ▶ if(length $data <= $maxlen) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data <= $maxlen) 20 4 24
1676 20 return printable($data);
1677 }
1678  
1679 4 ▶ $maxlen = 12 if $maxlen < 12;
Branch: 1 branch
BranchTrueFalseTotal
if $maxlen < 12 2 2 4
1680 4 my $sniplen = int(($maxlen - 8) / 2);
1681  
1682 4 my $start = substr($data, 0, $sniplen);
1683 4 my $end = substr($data, -$sniplen);
1684 4 my $snipped = length($data) - 2*$sniplen;
1685  
1686 4 return lenformat("$start\[snip=$snipped]$end", length $data);
1687 }
1688
1689 ###########################################
1690 4 ▶sub lenformat {
Subroutine lenformat: called CC=2 SCAR=8.1
Pod undocumented
1691 ###########################################
1692 4 my($data, $orglen) = @_;
1693  
1694 4 ▶ return "(" . ($orglen || length($data)) . ")[" .
Condition33%1/3
$orglen || length $data
l!l&&r!l&&!r
400
MC/DC0%0/2
$orglen || length $data
$orglen length $data
Truth table33%1/3
A || B
A= $orglenB= length $data
ABresult
1X1
011
000
1695 printable($data) . "]";
1696 }
1697  
1698 ###########################################
1699 25 ▶sub printable {
Subroutine printable: called CC=1 SCAR=0.0
Pod undocumented
1700 ###########################################
1701 25 my($data) = @_;
1702  
1703 25 $data =~ s/[^ \w.;!?@#$%^&*()+\\|~`',><[\]{}="-]/./g;
1704 25 return $data;
1705 }
1706  
1707 =pod
1708  
1709 =item C<password_read($prompt, $opts)>
1710  
1711 Reads in a password to be typed in by the user in noecho mode.
1712 A call to password_read("password: ") results in
1713  
1714 password: ***** (stars aren't actually displayed)
1715  
1716 This function will switch the terminal back into normal mode
1717 after the user hits the 'Return' key.
1718  
1719 If the optional C<$opts> hash has C<{ tty =E<gt> 1 }> set, then
1720 the prompt will be redirected to the console instead of STDOUT.
1721  
1722 =cut
1723  
1724 ###########################################
1725 0 ▶sub password_read {
Subroutine password_read: not called CC=1 SCAR=3.5
Pod documented
1726 ###########################################
1727 0 my($prompt, $opts) = @_;
1728  
1729 18 ▶ use Term::ReadKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
1730 0 ReadMode 'noecho';
1731 0 $| = 1;
1732 0 user_prompt($prompt, $opts);
1733 0 my $pw = ReadLine 0;
1734 0 chomp $pw;
1735 0 ReadMode 'restore';
1736 0 user_prompt("\n", $opts);
1737  
1738 0 return $pw;
1739 }
1740  
1741 =pod
1742  
1743 =item C<nice_time($time)>
1744  
1745 Format the time in a human-readable way, less wasteful than the
1746 'scalar localtime' formatting.
1747  
1748 print nice_time(), "\n";
1749 # 2007/04/01 10:51:24
1750  
1751 It uses the system time by default, but it can also accept epoch seconds:
1752  
1753 print nice_time(1170000000), "\n";
1754 # 2007/01/28 08:00:00
1755  
1756 It uses localtime() under the hood, so the outcome of the above will
1757 depend on your local time zone setting.
1758  
1759 =cut
1760  
1761 ###########################################
1762 0 ▶sub nice_time {
Subroutine nice_time: not called CC=2 SCAR=17.9
Pod documented
1763 ###########################################
1764 0 my($time) = @_;
1765  
1766 0 ▶ $time = time() unless defined $time;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $time 0 0 0
1767  
1768 0 my ($sec,$min,$hour,$mday,$mon,$year,
1769 $wday,$yday,$isdst) = localtime($time);
1770  
1771 0 return sprintf("%d/%02d/%02d %02d:%02d:%02d",
1772 $year+1900, $mon+1, $mday,
1773 $hour, $min, $sec);
1774 }
1775  
1776 =item C<def_or($foo, $default)>
1777  
1778 Perl-5.9 added the //= construct, which helps assigning values to
1779 undefined variables. Instead of writing
1780  
1781 if(!defined $foo) {
1782 $foo = $default;
1783 }
1784  
1785 you can just write
1786  
1787 $foo //= $default;
1788  
1789 However, this is not available on older perl versions (although there's
1790 source filter solutions). Often, people use
1791  
1792 $foo ||= $default;
1793  
1794 instead which is wrong if $foo contains a value that evaluates as false.
1795 So Sysadm::Install, the everything-and-the-kitchen-sink under the CPAN
1796 modules, provides the function C<def_or()> which can be used like
1797  
1798 def_or($foo, $default);
1799  
1800 to accomplish the same as
1801  
1802 $foo //= $default;
1803  
1804 How does it work, how does $foo get a different value, although it's
1805 apparently passed in by value? Modifying $_[0] within the subroutine
1806 is an old Perl trick to do exactly that.
1807  
1808 =cut
1809  
1810 ###########################################
1811 3 ▶sub def_or($$) {
Subroutine def_or: called CC=2 SCAR=6.9
Pod documented
1812 ###########################################
1813 3 ▶ if(! defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $_[0]) 1 2 3
1814 1 $_[0] = $_[1];
1815 }
1816 }
1817  
1818 =item C<is_utf8_data($data)>
1819  
1820 Check if the given string has the utf8 flag turned on. Works just like
1821 Encode.pm's is_utf8() function, except that it silently returns a
1822 false if Encode isn't available, for example when an ancient perl
1823 without proper utf8 support is used.
1824  
1825 =cut
1826  
1827 ###############################################
1828 0 ▶sub is_utf8_data {
Subroutine is_utf8_data: not called CC=2 SCAR=17.9
Pod documented
1829 ###############################################
1830 0 my($data) = @_;
1831  
1832 0 ▶ if( !utf8_available() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (utf8_available()) 0 0 0
1833 0 return 0;
1834 }
1835  
1836 0 return Encode::is_utf8( $data );
1837 }
1838  
1839 =item C<utf8_check($data)>
1840  
1841 Check if we're using a perl with proper utf8 support, by verifying the
1842 Encode.pm module is available for loading.
1843  
1844 =cut
1845  
1846 ###############################################
1847 0 ▶sub utf8_available {
Subroutine utf8_available: not called CC=2 SCAR=17.9
Pod undocumented
1848 ###############################################
1849  
1850 0 eval "use Encode";
1851  
1852 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1853 0 return 0;
1854 }
1855  
1856 0 return 1;
1857 }
1858  
1859 =item C<home_dir()>
1860  
1861 Return the path to the home directory of the current user.
1862  
1863 =cut
1864  
1865 ###############################################
1866 1 ▶sub home_dir {
Subroutine home_dir: called CC=1 SCAR=0.0
Pod documented
1867 ###############################################
1868  
1869 18 ▶ my( $home ) = glob "~";
Subroutine BEGIN: called CC=1 SCAR=0.0
1870  
1871 1 return $home;
1872 }
1873  
1874 =pod
1875  
1876 =back
1877  
1878 =head1 AUTHOR
1879  
1880 Mike Schilli, E<lt>m@perlmeister.comE<gt>
1881  
1882 =head1 COPYRIGHT AND LICENSE
1883  
1884 Copyright (C) 2004-2007 by Mike Schilli
1885  
1886 This library is free software; you can redistribute it and/or modify
1887 it under the same terms as Perl itself, either Perl version 5.8.3 or,
1888 at your option, any later version of Perl 5 you may have available.
1889  
1890 =cut
1891  
1892 18 1;
↑ Summary