lib/Chj/IO/Tempfile.pm

filter: Statementstmt 53.4% 85 / 159 Branchbran 17.9% 14 / 78 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 68.1% 15 / 22 Podpod 0.0% 0 / 8 totaltotal 41.1% 114 / 277 CC 43 SCAR 41.1
CC
43
Cov
41%
xreplace_or_withmode
156.7
_xlinkrename
56.0
xtmpfile
44.6
CRAP 61.0
« .htmlgen/FunctionalPerl/Htmlgen/Linking.pm ↑ Summary lib/Chj/IO/File.pm »
1 #
2 # Copyright (c) 2003-2014 Christian Jaeger, copying@christianjaeger.ch
3 #
4 # This is free software, offered under either the same terms as perl 5
5 # or the terms of the Artistic License version 2 or the terms of the
6 # MIT License (Expat version). See the file COPYING.md that came
7 # bundled with this file.
8 #
9  
10 =head1 NAME
11  
12 Chj::IO::Tempfile
13  
14 =head1 SYNOPSIS
15  
16 =head1 DESCRIPTION
17  
18 =head1 NOTES
19  
20 If you kill the program i.e. using Ctl-C, it will not clean up
21 its temp file.
22  
23 =head1 NOTE
24  
25 This is alpha software! Read the status section in the package README
26 or on the L<website|http://functional-perl.org/>.
27  
28 =cut
29  
30 package Chj::IO::Tempfile;
31 31 ▶use base "Chj::IO::File";
Subroutine BEGIN: called CC=1 SCAR=0.0
32 31 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 31 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 31 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 31 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 31 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 31 ▶use POSIX qw(EEXIST EINTR ENOENT);
Subroutine BEGIN: called CC=1 SCAR=0.0
38 31 ▶use Chj::singlequote ();
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 31 our $MAXTRIES = 10;
41 31 our $DEFAULT_AUTOCLEAN = 1; # 0=never unlink automatically, 1= unlink on
42 # destruction, 2= unlink immediately.
43  
44 31 my %metadata;
45  
46 # numified => [ basepath,
47 # autoclean,
48 # [{hashwithattributes}],
49 # ]
50  
51 7 ▶sub xtmpfile {
Subroutine xtmpfile: called CC=14 SCAR=38.0
Pod undocumented
52 7 my $proto = shift;
53 7 my ($maybe_basepath, $mode, $autoclean, $mkdircb) = @_;
54  
55 # basepath: /dir/startofname (or: /dir/, but not /dir ),
56 # or a function which receives an autogenerated basepath
57 # and a random number and should return the path to use
58 7 my $genpath = do {
59 0 ▶ my $gen_base = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
60 0 my ($n) = $0 =~ m{(.*[^.]{2,}.*)}; # at least 2 non-"." characters
61 0 $n =~ tr/\//-/;
62 0 "/tmp/$n"
63 7 };
64 7 ▶ my $mk_basepath_n = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
65 7 my ($basepath) = @_;
66 14 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
67 14 my ($n) = @_;
68 14 "$basepath$n"
69 }
70 7 };
71 7 ▶ if ($maybe_basepath) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maybe_basepath) { } 7 0 7
72 7 ▶ if (ref($maybe_basepath) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $maybe_basepath eq 'CODE') { } 0 7 7
73 0 my $basepath = &$gen_base;
74 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
75 0 my ($n) = @_;
76 0 &$maybe_basepath($basepath, $n)
77 }
78 0 } else {
79 7 &$mk_basepath_n($maybe_basepath)
80 }
81 } else {
82 0 &$mk_basepath_n(&$gen_base)
83 }
84 };
85 7 ▶ defined $mode or $mode = 0600;
Branch: 1 branch
BranchTrueFalseTotal
defined $mode or $mode = 384 0 7 7
86 7 ▶ defined $autoclean or $autoclean = $DEFAULT_AUTOCLEAN;
Branch: 1 branch
BranchTrueFalseTotal
defined $autoclean or $autoclean = $Chj::IO::Tempfile::DEFAULT_AUTOCLEAN 0 7 7
87 7 my $self;
88 7 my $tries = 0;
89 7 my $called_mkdircb;
90 TRY: {
91 7 my $last_path;
92 7 eval {
93 7 $! = 0;
94  
95 #better today:
96 7 $Chj::IO::ERRNO = 0;
97 7 my $rand = int(rand(99999) * 100000 + rand(99999));
98  
99 # ^ XX probably not good enough against DoS or fork
100 7 my $path = &$genpath($rand);
101 7 $last_path = $path;
102  
103 #$DB::single=1;
104 7 $self = $proto->xsysopen($path, O_EXCL | O_CREAT | O_RDWR, $mode);
105 7 ▶ if ($autoclean == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($autoclean == 2) 0 7 7
106 0 ▶ unlink $path
Branch: 1 branch
BranchTrueFalseTotal
unlink $path or croak('xtmpfile: could not unlink ' . Chj::singlequote($path) . " that we created moments ago ???: $!") 0 0 0
107 or croak "xtmpfile: could not unlink "
108 . Chj::singlequote($path)
109 . " that we created moments ago ???: $!";
110 0 undef $path;
111 }
112 7 $metadata{ pack "I", $self } = [&$genpath(""), $autoclean];
113 };
114 7 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 7 7
115 0 ▶ if ($Chj::IO::ERRNO == EEXIST or $Chj::IO::ERRNO == EINTR) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Chj::IO::ERRNO == 17 or $Chj::IO::ERRNO == 4) { } 0 0 0
elsif ($Chj::IO::ERRNO == 2 and $mkdircb) { } 0 0 0
Condition0%0/3
$Chj::IO::ERRNO == 17 or $Chj::IO::ERRNO == 4
l!l&&r!l&&!r
000
MC/DC0%0/2
$Chj::IO::ERRNO == 17 or $Chj::IO::ERRNO == 4
$Chj::IO::ERRNO == 17 $Chj::IO::ERRNO == 4
Truth table0%0/3
A or B
A= $Chj::IO::ERRNO == 17B= $Chj::IO::ERRNO == 4
ABresult
1X1
011
000
116  
117 # ^ not sure whether the latter test is needed
118 0 ▶ if (++$tries < $MAXTRIES) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$tries < $Chj::IO::Tempfile::MAXTRIES) { } 0 0 0
119 0 redo TRY;
120 } else {
121 0 croak "xtmpfile: too many attempts to create a "
122 . "tempfile, last attempt was "
123 . Chj::singlequote($last_path);
124 }
125 ▶ } elsif ($Chj::IO::ERRNO == ENOENT and $mkdircb) {
Condition0%0/3
$Chj::IO::ERRNO == 2 and $mkdircb
!ll&&!rl&&r
000
MC/DC0%0/2
$Chj::IO::ERRNO == 2 and $mkdircb
$Chj::IO::ERRNO == 2 $mkdircb
Truth table0%0/3
A and B
A= $Chj::IO::ERRNO == 2B= $mkdircb
ABresult
0X0
100
111
126 0 ▶ if ($called_mkdircb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($called_mkdircb) { } 0 0 0
127 0 croak "xtmpfile: got ENOENT but mkdir-callback has "
128 . "already been called, for attempt "
129 . Chj::singlequote($last_path);
130 } else {
131  
132 #&$mkdircb;
133 0 $mkdircb->(&$genpath(""));
134 0 $called_mkdircb = 1;
135 0 redo TRY;
136 }
137 } else {
138 0 croak "xtmpfile: could not create tempfile at "
139 . Chj::singlequote($last_path) . ": $@";
140 }
141 }
142 }
143 $self
144 7 }
145  
146 0 ▶sub autoclean {
Subroutine autoclean: not called CC=2 SCAR=17.9
Pod undocumented
147 0 my $self = shift;
148 0 ▶ if (@_) { # set
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
149 0 my ($v) = @_;
150 0 $metadata{ pack "I", $self }[1] = $v;
151  
152 # should we return former setting? does that really make sense?
153 } else {
154 0 $metadata{ pack "I", $self }[1]
155 }
156 }
157  
158 # No need to override the xunlink method.
159  
160 7 ▶sub xrename {
Subroutine xrename: called CC=1 SCAR=0.0
Pod undocumented
161 7 my $self = shift;
162 7 $self->SUPER::xrename(@_);
163 7 $metadata{ pack "I", $self }[1] = 0;
164 }
165  
166 # This one is a bit non-sensible, since xlink is enough, unlinkig
167 # source anyway.
168 0 ▶sub xlinkunlink {
Subroutine xlinkunlink: not called CC=1 SCAR=6.9
Pod undocumented
169 0 my $self = shift;
170 0 $self->SUPER::xlinkunlink(@_);
171 0 $metadata{ pack "I", $self }[1] = 0;
172 }
173  
174 7 ▶sub DESTROY {
Subroutine DESTROY: called CC=4 SCAR=15.9
175 7 my $self = shift;
176 7 local ($@, $!, $?, $_);
177 7 ▶ if (defined(my $path = $self->path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $path = $self->path)) 7 0 7
178 7 ▶ if ($metadata{ pack "I", $self }[1] == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($metadata{pack "I", $self}[1] == 1) 0 7 7
179 0 ▶ unlink $path
Branch: 1 branch
BranchTrueFalseTotal
unlink $path or warn 'DESTROY: unlink ' . $self->quotedname . ": $!" 0 0 0
180 or warn "DESTROY: unlink " . $self->quotedname . ": $!";
181 }
182 }
183 7 delete $metadata{ pack "I", $self };
184 7 $self->SUPER::DESTROY;
185 }
186  
187 # (where am I using this?)
188 0 ▶sub attribute { # :lvalue does not work because of perl bugs. :-(
Subroutine attribute: not called CC=2 SCAR=17.9
Pod undocumented
189 0 my $self = shift;
190 0 my $key = shift;
191 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
192 0 ($metadata{ pack "I", $self }[2]{$key}) = @_
193 } else {
194 0 $metadata{ pack "I", $self }[2]{$key}
195 }
196 }
197  
198 0 ▶sub _xlinkrename {
Subroutine _xlinkrename: not called CC=7 SCAR=40.3
199 0 my ($from, $to) = @_; # to must be file path, not dir.
200 0 my $tobase = $to;
201 0 ▶ $tobase =~ s{/?([^/]+)\z}{} or croak "_xlinkrename: missing to parameter";
Branch: 1 branch
BranchTrueFalseTotal
$tobase =~ s[/?([^/]+)\z][] or croak("_xlinkrename: missing to parameter") 0 0 0
202 0 my $toname = $1;
203 0 ▶ $tobase .= "/" if length $tobase;
Branch: 1 branch
BranchTrueFalseTotal
if length $tobase 0 0 0
204 0 for (1 .. 10) {
205 0 my $tmppath = "$tobase.$toname." . rand(10000);
206 0 ▶ if (link $from, $tmppath) {
Branch: 1 branch
BranchTrueFalseTotal
if (link $from, $tmppath) { } 0 0 0
207 0 ▶ if (rename $tmppath, $to) {
Branch: 1 branch
BranchTrueFalseTotal
if (rename $tmppath, $to) { } 0 0 0
208 0 return;
209 } else {
210 0 croak "_xlinkrename: rename "
211 . Chj::singlequote($tmppath) . ", "
212 . Chj::singlequote($to) . ": $!";
213 }
214 } else {
215 0 ▶ if ($! == EEXIST) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! == 17) { } 0 0 0
216 0 next;
217 } else {
218 0 croak "_xlinkrename: link "
219 . Chj::singlequote($from) . ", "
220 . Chj::singlequote($tmppath) . ": $!";
221 }
222 }
223 }
224 0 croak "_xlinkrename: too many attempts to make a link from "
225 . Chj::singlequote($from)
226 . " to a random name around "
227 . Chj::singlequote($to) . ": $!";
228 }
229  
230 31 our $warn_all_failures = 1;
231  
232 7 ▶sub xreplace_or_withmode {
Subroutine xreplace_or_withmode: called CC=18 SCAR=50.5
Pod undocumented
233 7 my $self = shift;
234 7 my ($targetpath, $orwithmode) = @_;
235  
236 # $orwithmode can be an integer, an octal string, or a stat
237 # object; in case of a stat object and if running as root, it also
238 # keeps uid/gid.
239 7 my $path = $self->xpath;
240 7 my ($uid, $gid, $mode);
241 7 ▶ if (($uid, $gid, $mode) = (stat $targetpath)[4, 5, 2]) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = (stat $targetpath)[4, 5, 2]) { } 0 7 7
242 0 my $euid = (stat $path)[4]; # better than $> because of peculiarities
243 0 ▶ defined $euid
Branch: 1 branch
BranchTrueFalseTotal
defined $euid or croak(q[xreplace_or_withmode: ?? can't stat own file ] . Chj::singlequote($path) . ": $!") 0 0 0
244 or croak "xreplace_or_withmode: ?? can't stat own file "
245 . Chj::singlequote($path) . ": $!";
246 0 ▶ if ($euid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($euid == 0) { } 0 0 0
247 0 $! = undef;
248 0 ▶ chown $uid, $gid, $path
Branch: 1 branch
BranchTrueFalseTotal
chown $uid, $gid, $path or croak('xreplace_or_withmode: chown ' . Chj::singlequote($path) . ": $!") 0 0 0
249 or croak "xreplace_or_withmode: chown "
250 . Chj::singlequote($path) . ": $!";
251 } else {
252 0 ▶ if ($uid != $euid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uid != $euid) 0 0 0
253 0 ▶ carp "xreplace_or_withmode: warning: cannot set owner of "
Branch: 1 branch
BranchTrueFalseTotal
if $Chj::IO::Tempfile::warn_all_failures 0 0 0
254 . Chj::singlequote($path)
255 . " to $uid since we are not root"
256 if $warn_all_failures;
257 0 $mode &= 0777; # see below
258 }
259 0 $! = undef;
260 0 ▶ chown $euid, $gid, $path or do {
Branch: 1 branch
BranchTrueFalseTotal
chown $euid, $gid, $path or do { carp('xreplace_or_withmode: warning: could not set group of ' . Chj::singlequote($path) . " to ${gid}: $!") if $Chj::IO::Tempfile::warn_all_failures; $mode &= 511 } 0 0 0
261  
262 # only a warning, ok?
263 0 ▶ carp "xreplace_or_withmode: warning: could not set group of "
Branch: 1 branch
BranchTrueFalseTotal
if $Chj::IO::Tempfile::warn_all_failures 0 0 0
264 . Chj::singlequote($path)
265 . " to $gid: $!"
266 if $warn_all_failures;
267 0 $mode &= 0777; # mask off setuid and such stuff. correct?
268 };
269 }
270  
271 # keep backup:
272 # we need it atomic, thus link. but a 'replacing link'.
273 eval {
274 0 _xlinkrename $targetpath, "$targetpath~"; # make configurable?
275 0 1
276 0 ▶ } || do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { _xlinkrename($targetpath, "$targetpath~"); 1 } } or do { warn "xreplace_or_withmode: warning: could not make backup file: $@" if $Chj::IO::Tempfile::warn_all_failures } 0 0 0
277 0 ▶ warn "xreplace_or_withmode: warning: could not make backup file: $@"
Branch: 1 branch
BranchTrueFalseTotal
if $Chj::IO::Tempfile::warn_all_failures 0 0 0
278 if $warn_all_failures;
279 }
280 } else {
281 7 ▶ if (defined $orwithmode) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $orwithmode) { } 7 0 7
282 7 ▶ if (ref $orwithmode) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $orwithmode) { } 0 7 7
283  
284 # assuming stat object
285 0 $mode = $orwithmode->permissions;
286 0 ▶ if ($> == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($> == 0) 0 0 0
287 0 $! = undef;
288 0 ▶ chown $orwithmode->uid, $orwithmode->gid, $path
Branch: 1 branch
BranchTrueFalseTotal
chown $orwithmode->uid, $orwithmode->gid, $path or croak('xreplace_or_withmode: chown ' . Chj::singlequote($path) . ": $!") 0 0 0
289 or croak "xreplace_or_withmode: chown "
290 . Chj::singlequote($path) . ": $!";
291 }
292 } else {
293 7 ▶ if ($orwithmode =~ /^0/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($orwithmode =~ /^0/) 0 7 7
294 0 $orwithmode = oct $orwithmode;
295 0 ▶ defined($orwithmode)
Branch: 1 branch
BranchTrueFalseTotal
defined $orwithmode or croak("xreplace_or_withmode: illegal octal withmode value given") 0 0 0
296 or croak "xreplace_or_withmode: illegal octal "
297 . "withmode value given";
298  
299 # ^ well, never happens when givint numbers not strings
300 }
301 7 $mode = $orwithmode
302 ; # & 0777; # mask off dito, since we do not know which uid/gid the programmer meant. which is a bug in itself. wellll , programmer should know what he's doing then, right?
303 }
304 } else {
305 0 croak "xreplace_or_withmode: error getting target permissions"
306 . " and no default mode given, stat "
307 . Chj::singlequote($targetpath) . ": $!";
308 }
309 }
310 7 $! = undef;
311 7 ▶ chmod $mode, $path
Branch: 1 branch
BranchTrueFalseTotal
chmod $mode, $path or croak('xreplace_or_withmode: chmod ' . Chj::singlequote($path) . ": $!") 7 0 7
312 or croak "xreplace_or_withmode: chmod "
313 . Chj::singlequote($path) . ": $!";
314 7 $self->xrename($targetpath);
315 }
316  
317 7 ▶sub xputback { # better name?
Subroutine xputback: called CC=2 SCAR=7.0
Pod undocumented
318 7 my $self = shift;
319 7 my ($maybe_orwithmode) = @_;
320 7 ▶ croak "xputback: file " . $self->quotedname . " is still open"
Branch: 1 branch
BranchTrueFalseTotal
if $self->opened 0 7 7
321 if $self->opened;
322 7 my $basepath = $metadata{ pack "I", $self }[0];
323 7 $self->xreplace_or_withmode($basepath, $maybe_orwithmode);
324 }
325  
326 0 ▶sub basepath {
Subroutine basepath: not called CC=1 SCAR=6.9
Pod undocumented
327 0 my $self = shift;
328 0 $metadata{ pack "I", $self }[0]
329 }
330  
331 31 1
« .htmlgen/FunctionalPerl/Htmlgen/Linking.pm ↑ Summary lib/Chj/IO/File.pm »