blib/lib/Doit/File.pm

filter: Statementstmt 86.9% 93 / 107 Branchbran 72.7% 48 / 66 Conditioncond 56.2% 9 / 16 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 100% 10 / 10 Podpod 33.3% 1 / 3 totaltotal 77.7% 164 / 211 SCAR 40.5
CC
42
Cov
79%
file_atomic_write
40.4
_make_writeable
14.9
__ANON__
5.0
CRAP 57.5
« blib/lib/Doit/Gem.pm ↑ Summary blib/lib/Doit/Ini.pm »
1 # -*- perl -*-
2  
3 #
4 # Author: Slaven Rezic
5 #
6 # Copyright (C) 2017,2018,2021 Slaven Rezic. All rights reserved.
7 # This package is free software; you can redistribute it and/or
8 # modify it under the same terms as Perl itself.
9 #
10 # Mail: slaven@rezic.de
11 # WWW: http://www.rezic.de/eserte/
12 #
13  
14 package Doit::File;
15  
16 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 our $VERSION = '0.024';
19  
20 1 ▶use Doit::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Doit::Util qw(copy_stat new_scope_cleanup);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶sub new { bless {}, shift }
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
24 1 ▶sub functions { qw(file_atomic_write) }
Subroutine functions: called CC=1 SCAR=0.0
Pod undocumented
25  
26 sub file_atomic_write {
27 20 ▶ my($doit, $file, $code, %opts) = @_;
Subroutine file_atomic_write: called CC=34 SCAR=37.0
Pod documented
28  
29 20 ▶ if (!defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 1 19 20
30 1 error "File parameter is missing";
31 }
32 19 ▶ if (!defined $code) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $code) { } 1 18 19
elsif (ref $code ne 'CODE') { } 1 17 18
33 1 error "Code parameter is missing";
34 } elsif (ref $code ne 'CODE') {
35 1 error "Code parameter should be an anonymous subroutine or subroutine reference";
36 }
37  
38 17 require File::Basename;
39 17 require Cwd;
40 17 my $dest_dir = File::Basename::dirname($file);
41 17 eval { $dest_dir = Cwd::realpath($dest_dir) }; # may fail on some platforms (e.g. Windows) if $dest_dir does not exist
42  
43 17 ▶ my $tmp_suffix = delete $opts{tmpsuffix} || '.tmp';
Condition100%2/2
delete $opts{'tmpsuffix'} || '.tmp'
l!l
116
MC/DC100%1/1
delete $opts{'tmpsuffix'} || '.tmp'
delete $opts{'tmpsuffix'}
Truth table100%2/2
A
A= delete $opts{'tmpsuffix'}
Aresult
11
00
44 17 my $tmp_dir = delete $opts{tmpdir};
45 17 ▶ if (!defined $tmp_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tmp_dir) 14 3 17
46 14 ▶ if (defined $dest_dir && -d $dest_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dest_dir and -d $dest_dir) { } 13 1 14
Condition66%2/3
defined $dest_dir and -d $dest_dir
!ll&&!rl&&r
0113
MC/DC50%1/2
defined $dest_dir and -d $dest_dir
defined $dest_dir -d $dest_dir
Truth table66%2/3
A and B
A= defined $dest_dirB= -d $dest_dir
ABresult
0X0
100
111
47 13 $tmp_dir = $dest_dir;
48 } else {
49 1 ▶ if (eval { require File::Spec; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require File::Spec; 1 } }) { } 1 0 1
50 1 $tmp_dir = File::Spec->tmpdir;
51 } else {
52 0 $tmp_dir = '/tmp';
53 }
54 }
55 }
56 17 my $mode = delete $opts{mode};
57 17 my $check_change = delete $opts{check_change};
58 17 ▶ error "Unhandled options: " . join(" ", %opts) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 1 16 17
59  
60 16 my($tmp_fh,$tmp_file);
61 16 my(@cleanup_files, @cleanup_fhs);
62 my $tempfile_scope = new_scope_cleanup {
63 16 ▶ for my $cleanup_fh (@cleanup_fhs) { # required on Windows, otherwise unlink won't work
Subroutine __ANON__: called CC=5 SCAR=16.1
64 15 ▶ close $cleanup_fh if fileno($cleanup_fh);
Branch: 1 branch
BranchTrueFalseTotal
if fileno $cleanup_fh 1 14 15
65 }
66 16 for my $cleanup_file (@cleanup_files) {
67 15 ▶ unlink $cleanup_file if -e $cleanup_file;
Branch: 1 branch
BranchTrueFalseTotal
if -e $cleanup_file 5 10 15
68 }
69 16 };
70 16 ▶ if ($tmp_dir eq '/dev/full') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp_dir eq '/dev/full') { } 1 15 16
71 # This is just used for testing error on close()
72 1 $tmp_file = '/dev/full';
73 1 ▶ open $tmp_fh, '>', $tmp_file
Branch: 1 branch
BranchTrueFalseTotal
open $tmp_fh, ">", $tmp_file or &error("Can't write to ${tmp_file}: $!") 0 1 1
74 or error "Can't write to $tmp_file: $!";
75 } else {
76 15 require File::Temp;
77 15 ($tmp_fh,$tmp_file) = File::Temp::tempfile(SUFFIX => $tmp_suffix, DIR => $tmp_dir, EXLOCK => 0);
78 15 push @cleanup_files, $tmp_file;
79 15 push @cleanup_fhs, $tmp_fh;
80 15 ▶ if (defined $mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mode) { } 2 13 15
81 2 $doit->chmod({quiet => 1}, $mode, $tmp_file);
82 } else {
83 13 $doit->chmod({quiet => 1}, 0666 & ~umask, $tmp_file);
84 }
85 15 ▶ if ($tmp_dir ne $dest_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp_dir ne $dest_dir) 3 12 15
86 3 my @stat_destdir = stat $dest_dir;
87 3 ▶ if (@stat_destdir) { # may fail in dry-run mode if $dest_dir is missing
Branch: 1 branch
BranchTrueFalseTotal
if (@stat_destdir) 2 1 3
88 2 ▶ if ($^O =~ /bsd/ || $^O eq 'darwin' || ($stat_destdir[2] & 02000)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /bsd/ or $^O eq "darwin" or $stat_destdir[2] & 1024) 1 1 2
Condition50%3/6
$^O =~ /bsd/ or $^O eq "darwin" or $stat_destdir[2] & 1024
l!l&&r!l&&!r
011
$^O =~ /bsd/ or $^O eq "darwin"
l!l&&r!l&&!r
002
MC/DC33%1/3
$^O =~ /bsd/ or $^O eq "darwin" or $stat_destdir[2] & 1024
$^O =~ /bsd/ $^O eq "darwin" $stat_destdir[2] & 1024
Truth table50%2/4
A or B or C
A= $^O =~ /bsd/B= $^O eq "darwin"C= $stat_destdir[2] & 1024
ABCresult
1XX1
01X1
0011
0000
89 1 $doit->chown({quiet => 1 }, undef, $stat_destdir[5], $tmp_file);
90 }
91 }
92 }
93 }
94 16 my $same_fs = do {
95 16 my $tmp_dev = (stat($tmp_file))[0];
96 16 my $dest_dev = (stat($dest_dir))[0];
97 1 ▶ no warnings 'uninitialized'; # $dest_dev may be undefined in dry-run mode
Subroutine BEGIN: called CC=1 SCAR=0.0
98 16 $tmp_dev == $dest_dev;
99 };
100  
101 16 ▶ if ($same_fs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($same_fs) { } 14 2 16
102 14 ▶ if (-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) 8 6 14
103 8 copy_stat $file, $tmp_file, ownership => 1, mode => !defined $mode;
104 }
105 } else {
106 2 require File::Copy; # for move()
107 }
108  
109 16 eval { $code->($tmp_fh, $tmp_file) };
110 16 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 15 16
111 1 error $@;
112 }
113  
114 15 ▶ if ($] < 5.010001) { $! = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($] < "5.010001") 0 15 15
115 $tmp_fh->close
116 15 ▶ or error "Error while closing temporary file $tmp_file: $!";
Branch: 1 branch
BranchTrueFalseTotal
$tmp_fh->close or &error("Error while closing temporary file ${tmp_file}: $!") 1 14 15
117 14 ▶ if ($] < 5.010001 && $! != 0) { # at least perl 5.8.8 and 5.8.9 are buggy and do not detect errors at close time --- 5.10.1 is correct
Branch: 1 branch
BranchTrueFalseTotal
if ($] < "5.010001" and $! != 0) 0 14 14
Condition33%1/3
$] < "5.010001" and $! != 0
!ll&&!rl&&r
1400
MC/DC0%0/2
$] < "5.010001" and $! != 0
$] < "5.010001" $! != 0
Truth table33%1/3
A and B
A= $] < "5.010001"B= $! != 0
ABresult
0X0
100
111
118 0 error "Error while closing temporary file $tmp_file: $!";
119 }
120  
121 14 ▶ if ($check_change) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check_change) 3 11 14
122 3 require File::Compare;
123 3 ▶ if (File::Compare::compare($tmp_file, $file) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (File::Compare::compare($tmp_file, $file) == 0) 1 2 3
124 # unchanged
125 1 return 0;
126 }
127 }
128  
129 13 ▶ if ($same_fs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($same_fs) { } 12 1 13
130 12 _make_writeable($doit, $file, 'rename');
131 12 $doit->rename($tmp_file, $file);
132 } else {
133 1 my @dest_stat;
134 1 ▶ if (-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) 0 1 1
135 0 ▶ @dest_stat = stat($file)
Branch: 1 branch
BranchTrueFalseTotal
@dest_stat = stat $file or &warning("Cannot stat ${file}: $! (cannot preserve permissions)") 0 0 0
136 or warning "Cannot stat $file: $! (cannot preserve permissions)"; # XXX should this be an error?
137 0 _make_writeable($doit, $file, 'File::Copy::move');
138 }
139 1 $doit->move($tmp_file, $file);
140 1 ▶ if (@dest_stat) { # In dry-run mode effectively a noop
Branch: 2 branches
BranchTrueFalseTotal
if (@dest_stat) { } 0 1 1
elsif (defined $mode) { } 0 1 1
141 0 ▶ $dest_stat[2] = $mode if defined $mode;
Branch: 1 branch
BranchTrueFalseTotal
if defined $mode 0 0 0
142 0 copy_stat [@dest_stat], $file, ownership => 1, mode => 1;
143 } elsif (defined $mode) {
144 0 ▶ $dest_stat[2] = $mode if defined $mode;
Branch: 1 branch
BranchTrueFalseTotal
if defined $mode 0 0 0
145 0 copy_stat [@dest_stat], $file, mode => 1;
146 }
147 }
148  
149 13 return 1;
150 }
151  
152 sub _make_writeable {
153 12 ▶ my($doit, $file, $for) = @_;
Subroutine _make_writeable: called CC=5 SCAR=27.0
154 12 ▶ return if $for eq 'rename' && !Doit::IS_WIN; # don't need to do anything
Branch: 1 branch
BranchTrueFalseTotal
if 12 0 12
Condition50%1/2
$for eq "rename" and
!ll
012
MC/DC0%0/1
$for eq "rename" and
$for eq "rename"
Truth table50%1/2
A
A= $for eq "rename"
Aresult
00
11
155 0 my @s = stat($file);
156 0 ▶ return if !@s; # not stat-able -> file does not exist yet?
Branch: 1 branch
BranchTrueFalseTotal
unless @s 0 0 0
157 0 my $old_mode = $s[2] & 07777;
158 0 ▶ return if ($old_mode & 0200); # already writable
Branch: 1 branch
BranchTrueFalseTotal
if $old_mode & 128 0 0 0
159 0 $doit->chmod(($old_mode | 0200), $file);
160 }
161  
162 1 1;
163  
164 __END__
« blib/lib/Doit/Gem.pm ↑ Summary blib/lib/Doit/Ini.pm »