| 1 | package YATT::Lite::Util::File; | ||||||||||||||||||||||||||||
| 2 | 46 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 46 | ▶ | use warnings qw(FATAL all NONFATAL misc); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 46 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 46 | ▶ | use YATT::Lite::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 46 | ▶ | use File::Basename qw(dirname); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 46 | ▶ | use File::Path qw(make_path); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 46 | ▶ | use Time::HiRes qw/usleep/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 46 | ▶ | use File::stat; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 46 | ▶ | use constant DEBUG => $ENV{DEBUG_YATT_UTIL_FILE}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # 書き換え時に mtime の厳密な前進を保証するため、旧 mtime + 1.05 秒まで | ||||||||||||||||||||||||||||
| 14 | # 実時間で眠ることがある(通常は最大 ~1 秒)。mtime 前進はこの関数自身が | ||||||||||||||||||||||||||||
| 15 | # 保証するので、utime で mtime を進める運用と併用してはならない。 | ||||||||||||||||||||||||||||
| 16 | # mtime が MAX_MTIME_WAIT 秒を超えて未来のファイル(utime 済み)には | ||||||||||||||||||||||||||||
| 17 | # 黙って眠らず croak する。 | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 46 | ▶ | use constant MAX_MTIME_WAIT => 5; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 334 | ▶ | sub mkfile_may_wait { | ||||||||||||||||||||||||||
|
Subroutine
mkfile_may_wait: called CC=11 SCAR=24.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 22 | 334 | my ($pack) = shift; | |||||||||||||||||||||||||||
| 23 | 334 | my @slept; | |||||||||||||||||||||||||||
| 24 | 334 | ▶ | while (my ($fn, $content) = splice @_, 0, 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 507 | ▶ | ($fn, my @iolayer) = ref $fn ? @$fn : ($fn); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 507 | ▶ | unless (-d (my $dir = dirname($fn))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 182 | ▶ | make_path($dir) or die "Can't mkdir $dir: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | 507 | my $old_mtime; | |||||||||||||||||||||||||||
| 30 | 507 | ▶ | if (-e $fn) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 17 | my $deadline = ($old_mtime = stat($fn)->mtime) + 1.05; | |||||||||||||||||||||||||||
| 32 | 17 | ▶ | if ((my $diff = $deadline - Time::HiRes::time) > MAX_MTIME_WAIT) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 1 | croak sprintf("mkfile_may_wait: mtime of %s is %.1f secs in the future!" | |||||||||||||||||||||||||||
| 34 | ." (do not combine mkfile_may_wait with utime-forwarded mtimes)" | ||||||||||||||||||||||||||||
| 35 | , $fn, $diff); | ||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | 16 | ▶ | if (my $slept = wait_for_time($deadline)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 10 | push @slept, $slept; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | 506 | ▶ | open my $fh, join('', '>', @iolayer), $fn or die "$fn: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 506 | print $fh $content; | |||||||||||||||||||||||||||
| 43 | 506 | close $fh; | |||||||||||||||||||||||||||
| 44 | 506 | ▶ | unless (not defined $old_mtime or $old_mtime < stat($fn)->mtime) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $old_mtime or $old_mtime < &YATT::Lite::Util::File::stat($fn)->mtime
MC/DC0%0/2
not defined $old_mtime or $old_mtime < &YATT::Lite::Util::File::stat($fn)->mtime
not defined $old_mtime
$old_mtime < &YATT::Lite::Util::File::stat($fn)->mtime
Truth table66%2/3
not(A) or B
A= not defined $old_mtimeB= $old_mtime < &YATT::Lite::Util::File::stat($fn)->mtime
| |||||||||||||||||||||||||||||
| 45 | 0 | croak "Failed to update mtime for $fn!"; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | 333 | @slept; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # 旧名 (互換のための alias)。二重代入は "used only once" warning の抑制。 | ||||||||||||||||||||||||||||
| 52 | 46 | *mkfile = *mkfile = \&mkfile_may_wait; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # This works, but not so useful. Try wait_if_near_deadline instead. | ||||||||||||||||||||||||||||
| 55 | 16 | ▶ | sub wait_for_time { | ||||||||||||||||||||||||||
|
Subroutine
wait_for_time: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 56 | 16 | my ($time) = @_; | |||||||||||||||||||||||||||
| 57 | 16 | my $now = Time::HiRes::time; | |||||||||||||||||||||||||||
| 58 | 16 | my $diff = $time - $now; | |||||||||||||||||||||||||||
| 59 | 16 | ▶ | return if $diff <= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 10 | print STDERR "# wait_for_time: $diff secs\n" | |||||||||||||||||||||||||||
| 61 | if DEBUG; | ||||||||||||||||||||||||||||
| 62 | 10 | usleep(int($diff * 1000 * 1000)); | |||||||||||||||||||||||||||
| 63 | 10 | $diff; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # sleep if ($deadline - $hires_now) < $threshold | ||||||||||||||||||||||||||||
| 67 | # Use like following: | ||||||||||||||||||||||||||||
| 68 | # | ||||||||||||||||||||||||||||
| 69 | # if (my $slept = wait_if_near_deadline(time+1, 0.1)) { | ||||||||||||||||||||||||||||
| 70 | # diag "slept: $slept"; | ||||||||||||||||||||||||||||
| 71 | # } | ||||||||||||||||||||||||||||
| 72 | # | ||||||||||||||||||||||||||||
| 73 | 2 | ▶ | sub wait_if_near_deadline { | ||||||||||||||||||||||||||
|
Subroutine
wait_if_near_deadline: called CC=4 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 74 | 2 | my ($deadline, $threshold) = @_; | |||||||||||||||||||||||||||
| 75 | 2 | ▶ | $threshold //= 0.2; | ||||||||||||||||||||||||||
|
Condition50%1/2
$threshold //= "0.2"
MC/DC0%0/1
$threshold //= "0.2"
$threshold
Truth table50%1/2
A
A= $threshold
| |||||||||||||||||||||||||||||
| 76 | 2 | my $now = Time::HiRes::time; | |||||||||||||||||||||||||||
| 77 | 2 | my $diff = $deadline - $now; | |||||||||||||||||||||||||||
| 78 | 2 | ▶ | return if $diff > $threshold; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | return if $diff <= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | usleep(int($diff * 1000 * 1000)); | |||||||||||||||||||||||||||
| 81 | 0 | $diff; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # Auto Export. | ||||||||||||||||||||||||||||
| 85 | 46 | my $symtab = YATT::Lite::Util::symtab(__PACKAGE__); | |||||||||||||||||||||||||||
| 86 | our @EXPORT_OK = grep { | ||||||||||||||||||||||||||||
| 87 | 46 | my $entry = $symtab->{$_}; | |||||||||||||||||||||||||||
| 88 | # To remove constant; | ||||||||||||||||||||||||||||
| 89 | 690 | !ref $entry && *{$entry}{CODE} | |||||||||||||||||||||||||||
| 90 | 782 | ▶ | } keys %$symtab; | ||||||||||||||||||||||||||
|
Condition100%2/2
!ref($entry) && *{$entry;}{'CODE'}
MC/DC100%1/1
!ref($entry) && *{$entry;}{'CODE'}
!ref($entry)
Truth table100%2/2
not(A)
A= !ref($entry)
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 46 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 46 | 1; | |||||||||||||||||||||||||||