lib/Chj/IO/Tempdir.pm

filter: Statementstmt 28.4% 27 / 95 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 38.8% 7 / 18 Podpod 0.0% 0 / 9 totaltotal 19.5% 34 / 174 CC 22 SCAR 37.1
CC
22
Cov
19%
xtmpdir
110.0
DESTROY
56.0
_Addslash
12.0
CRAP 40.8
« lib/Chj/Package/OfPath.pm ↑ Summary lib/Chj/ruse.pm »
1 #
2 # Copyright (c) 2003-2021 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::Tempdir
13  
14 =head1 SYNOPSIS
15  
16 =head1 DESCRIPTION
17  
18  
19 =head1 NOTE
20  
21 This is alpha software! Read the status section in the package README
22 or on the L<website|http://functional-perl.org/>.
23  
24 =cut
25  
26 package Chj::IO::Tempdir;
27 1 @ISA = "Chj::IO::Dir";
28 1 require Chj::IO::Dir;
29  
30 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use Errno qw(EEXIST EINTR);
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 use overload
37 1 ▶ '""' => \&stringify,
Subroutine BEGIN: called CC=1 SCAR=0.0
38 '0+' => \&numify,
39 1 ;
40  
41 1 our $MAXTRIES = 10;
42 1 our $DEFAULT_AUTOCLEAN = 1;
43  
44 # 0=no, 1=yes and warn if not present in DESTROY, 2=yes but don't warn.
45 # XX or just boolean?
46  
47 1 my %meta;
48  
49 0 ▶sub numify {
Subroutine numify: not called CC=1 SCAR=6.9
Pod undocumented
50 0 my $self = shift;
51 0 my $class = ref $self;
52 0 bless $self,
53 "Chj::IO::Tempdir::FOOOOOOO"; # this is even how overload::StrVal works.
54 0 my $num = $self +0;
55 0 bless $self, $class;
56 0 $num
57 }
58  
59 0 ▶sub stringify {
Subroutine stringify: not called CC=1 SCAR=6.9
Pod undocumented
60 0 my $self = shift;
61 0 $self->path
62 }
63  
64 0 ▶sub _Addslash {
Subroutine _Addslash: not called CC=3 SCAR=24.8
65 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
66 0 my ($str) = @_;
67 0 ▶ $str =~ m|/$|s ? $str : $str . "/"
Branch: 1 branch
BranchTrueFalseTotal
$str =~ m[/$]s ? : 0 0 0
68 }
69  
70 0 ▶sub xtmpdir {
Subroutine xtmpdir: not called CC=10 SCAR=47.0
Pod undocumented
71 0 my $class = shift;
72 0 ▶ @_ <= 2 or fp_croak_arity "<= 2";
Branch: 1 branch
BranchTrueFalseTotal
@_ <= 2 or fp_croak_arity("<= 2") 0 0 0
73 0 my ($opt_basepath, $opt_mask) = @_;
74 my $basepath = (
75 0 ▶ $opt_basepath // do {
Condition0%0/3
$opt_basepath // do { $ENV{"CHJ_TEMPDIR_BASEPATH"} or $ENV{"CHJ_TEMPDIR"} ? _Addslash($ENV{"CHJ_TEMPDIR"}) : "/tmp/" }
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt_basepath // do { $ENV{"CHJ_TEMPDIR_BASEPATH"} or $ENV{"CHJ_TEMPDIR"} ? _Addslash($ENV{"CHJ_TEMPDIR"}) : "/tmp/" }
$opt_basepath do { $ENV{"CHJ_TEMPDIR_BASEPATH"} or $ENV{"CHJ_TEMPDIR"} ? _Addslash($ENV{"CHJ_TEMPDIR"}) : "/tmp/" }
Truth table0%0/3
A // B
A= $opt_basepathB= do { $ENV{"CHJ_TEMPDIR_BASEPATH"} or $ENV{"CHJ_TEMPDIR"} ? _Addslash($ENV{"CHJ_TEMPDIR"}) : "/tmp/" }
ABresult
1X1
011
000
76 $ENV{CHJ_TEMPDIR_BASEPATH}
77 or $ENV{CHJ_TEMPDIR}
78 ? _Addslash($ENV{CHJ_TEMPDIR})
79 0 ▶ : "/tmp/"
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'CHJ_TEMPDIR'} ? : 0 0 0
Condition0%0/3
$ENV{'CHJ_TEMPDIR_BASEPATH'} || ($ENV{'CHJ_TEMPDIR'} ? _Addslash($ENV{'CHJ_TEMPDIR'}) : '/tmp/')
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'CHJ_TEMPDIR_BASEPATH'} || ($ENV{'CHJ_TEMPDIR'} ? _Addslash($ENV{'CHJ_TEMPDIR'}) : '/tmp/')
$ENV{'CHJ_TEMPDIR_BASEPATH'} ($ENV{'CHJ_TEMPDIR'} ? _Addslash($ENV{'CHJ_TEMPDIR'}) : '/tmp/')
Truth table0%0/3
A || B
A= $ENV{'CHJ_TEMPDIR_BASEPATH'}B= ($ENV{'CHJ_TEMPDIR'} ? _Addslash($ENV{'CHJ_TEMPDIR'}) : '/tmp/')
ABresult
1X1
011
000
80 }
81 );
82 0 ▶ my $mask
Branch: 1 branch
BranchTrueFalseTotal
defined $opt_mask ? : 0 0 0
83 = defined($opt_mask)
84 ? $opt_mask
85 : 0700; # 0777 would be the perl default
86 0 my $item;
87 0 my $n = $MAXTRIES;
88 TRY: {
89 0 $item = int(rand(999) * 1000 + rand(999));
90 0 my $path = "$basepath$item";
91 0 ▶ if (mkdir $path, $mask) {
Branch: 2 branches
BranchTrueFalseTotal
if (mkdir $path, $mask) { } 0 0 0
elsif ($! == 17 or $! == 4) { } 0 0 0
92 0 my $self = $class->SUPER::new;
93 0 $self->set(path => $path, autoclean => $DEFAULT_AUTOCLEAN);
94 0 return $self;
95 ▶ } elsif ($! == EEXIST or $! == EINTR) {
Condition0%0/3
$! == 17 or $! == 4
l!l&&r!l&&!r
000
MC/DC0%0/2
$! == 17 or $! == 4
$! == 17 $! == 4
Truth table0%0/3
A or B
A= $! == 17B= $! == 4
ABresult
1X1
011
000
96 0 ▶ if (--$n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$n > 0) { } 0 0 0
97 0 redo TRY;
98 } else {
99 0 croak "xtmpdir: too many attempts to create a "
100 . "tempfile starting with path '$basepath'";
101 }
102 } else {
103 0 croak "xtmpdir: could not create dir '$path': $!";
104 }
105 }
106 }
107  
108 0 ▶sub set {
Subroutine set: not called CC=1 SCAR=6.9
Pod undocumented
109 0 my $self = shift;
110 0 %{ $meta{ pack "I", $self } } = @_ # XX does this delete old keys?
111 }
112  
113 0 ▶sub path {
Subroutine path: not called CC=2 SCAR=17.9
Pod undocumented
114 0 my $self = shift;
115 0 my $key = pack "I", $self;
116 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
117 0 ($meta{$key}{path}) = @_
118 } else {
119 0 $meta{$key}{path};
120 }
121 }
122  
123 0 ▶sub autoclean {
Subroutine autoclean: not called CC=2 SCAR=17.9
Pod undocumented
124 0 my $self = shift;
125 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
126 0 ($meta{ pack "I", $self }{autoclean}) = @_;
127 } else {
128 $meta{ pack "I", $self }{autoclean}
129 0 }
130 }
131  
132 0 ▶sub xtmpfile {
Subroutine xtmpfile: not called CC=2 SCAR=17.9
Pod undocumented
133 0 my $self = shift;
134 0 my ($mode, $autoclean) = @_;
135 0 ▶ defined(my $path = $self->path)
Branch: 1 branch
BranchTrueFalseTotal
defined(my $path = $self->path) or die "xtmpfile: can't create tmpfile inside undefined dir" 0 0 0
136 or die "xtmpfile: can't create tmpfile inside undefined dir";
137 0 require Chj::IO::Tempfile;
138 0 my $ret = Chj::IO::Tempfile->xtmpfile($path . "/", $mode, $autoclean);
139 0 $ret->attribute("parent_dir_obj", $self);
140 0 $ret
141 }
142  
143 # useful for when other code creates files in it (not tmpfiles): "rm -rf"
144  
145 0 ▶sub rmrf {
Subroutine rmrf: not called CC=1 SCAR=6.9
Pod undocumented
146 0 my $s = shift;
147 0 require Chj::Shelllike::Rmrf;
148 0 Chj::Shelllike::Rmrf::Rmrf($s->path);
149  
150 # to avoid warning, and since recreation later on should be
151 # understood as independent process anyway, ok?:
152 0 $s->autoclean(0)
153 }
154  
155 0 ▶sub push_on_destruction {
Subroutine push_on_destruction: not called CC=2 SCAR=17.9
Pod undocumented
156 0 my $self = shift;
157 0 ▶ @_ == 1 or die;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or die 0 0 0
158 0 my $key = pack "I", $self;
159 0 my ($handler) = @_;
160 0 push @{ $meta{$key}{on_destruction} }, $handler
161 }
162  
163 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=7 SCAR=40.3
164 0 my $self = shift;
165  
166 #warn "DESTROY $self";
167 0 local ($@, $!, $?, $_);
168 0 my $str = pack "I", $self;
169 0 ▶ if (my $arr = $meta{$str}{on_destruction}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $arr = $meta{$str}{"on_destruction"}) 0 0 0
170 0 &$_($self) for @$arr
171 }
172 0 ▶ if (my $autoclean = $meta{$str}{autoclean}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $autoclean = $meta{$str}{"autoclean"}) 0 0 0
173 0 ▶ rmdir $meta{$str}{path} or do {
Branch: 1 branch
BranchTrueFalseTotal
rmdir $meta{$str}{"path"} or do { warn qq[autoclean: could not remove dir '$meta{$str}{"path"}': $!] unless $autoclean and $autoclean == 2 } 0 0 0
174 0 ▶ warn "autoclean: could not remove dir '$meta{$str}{path}': $!"
Branch: 1 branch
BranchTrueFalseTotal
unless $autoclean and $autoclean == 2 0 0 0
Condition0%0/3
$autoclean and $autoclean == 2
!ll&&!rl&&r
000
MC/DC0%0/2
$autoclean and $autoclean == 2
$autoclean $autoclean == 2
Truth table0%0/3
A and B
A= $autocleanB= $autoclean == 2
ABresult
0X0
100
111
175 unless $autoclean
176 and $autoclean == 2
177  
178 # XX how to do right order with cleaning contained tmpfiles?
179 };
180 }
181 0 delete $meta{$str};
182  
183 #warn "/DESTROY $self";
184 }
185  
186 1 1;
« lib/Chj/Package/OfPath.pm ↑ Summary lib/Chj/ruse.pm »