| 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
| |||||||||||||||||||||||||||||
| 66 | 0 | my ($str) = @_; | |||||||||||||||||||||||||||
| 67 | 0 | ▶ | $str =~ m|/$|s ? $str : $str . "/" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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/"
}
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/"
}
| |||||||||||||||||||||||||||||
| 76 | $ENV{CHJ_TEMPDIR_BASEPATH} | ||||||||||||||||||||||||||||
| 77 | or $ENV{CHJ_TEMPDIR} | ||||||||||||||||||||||||||||
| 78 | ? _Addslash($ENV{CHJ_TEMPDIR}) | ||||||||||||||||||||||||||||
| 79 | 0 | ▶ | : "/tmp/" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$ENV{'CHJ_TEMPDIR_BASEPATH'} || ($ENV{'CHJ_TEMPDIR'} ? _Addslash($ENV{'CHJ_TEMPDIR'}) : '/tmp/')
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/')
| |||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | ); | ||||||||||||||||||||||||||||
| 82 | 0 | ▶ | my $mask | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/2
$! == 17 or $! == 4
$! == 17
$! == 4
Truth table0%0/3
A or B
A= $! == 17B= $! == 4
| |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | if (--$n > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 170 | 0 | &$_($self) for @$arr | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | 0 | ▶ | if (my $autoclean = $meta{$str}{autoclean}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 0 | ▶ | rmdir $meta{$str}{path} or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 0 | ▶ | warn "autoclean: could not remove dir '$meta{$str}{path}': $!" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$autoclean and $autoclean == 2
MC/DC0%0/2
$autoclean and $autoclean == 2
$autoclean
$autoclean == 2
Truth table0%0/3
A and B
A= $autocleanB= $autoclean == 2
| |||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||