blib/lib/IO/File/AtomicChange.pm

filter: Statementstmt 21.2% 20 / 94 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 30.0% 6 / 20 Podpod 25.0% 2 / 8 totaltotal 16.1% 28 / 173 CC 25 SCAR 33.9
CC
25
Cov
14%
close
90.0
open
42.0
backup
30.0
CRAP 29.8
↑ Summary Build »
1 package IO::File::AtomicChange;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.07_01';
7  
8 1 ▶use base qw(IO::File);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Temp qw(:mktemp);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub new {
14 0 ▶ my $class = shift;
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
15 0 my $self = $class->SUPER::new();
16 0 $self->_temp_file("");
17 0 $self->_target_file("");
18 0 $self->_backup_dir("");
19 0 ▶ $self->open(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
20 0 $self;
21 }
22  
23 sub _accessor {
24 0 ▶ my($self, $tag, $val) = @_;
Subroutine _accessor: not called CC=2 SCAR=17.9
25 0 ▶ ${*$self}{$tag} = $val if $val;
Branch: 1 branch
BranchTrueFalseTotal
if $val 0 0 0
26 0 return ${*$self}{$tag};
27 }
28 0 ▶sub _temp_file { return shift->_accessor("io_file_atomicchange_temp", @_) }
Subroutine _temp_file: not called CC=1 SCAR=6.9
29 0 ▶sub _target_file { return shift->_accessor("io_file_atomicchange_path", @_) }
Subroutine _target_file: not called CC=1 SCAR=6.9
30 0 ▶sub _backup_dir { return shift->_accessor("io_file_atomicchange_back", @_) }
Subroutine _backup_dir: not called CC=1 SCAR=6.9
31  
32 sub DESTROY {
33 0 ▶ carp "[CAUTION] disposed object without closing file handle." unless $_[0]->_closed;
Subroutine DESTROY: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]->_closed 0 0 0
34 }
35  
36 sub open {
37 0 ▶ my ($self, $path, $mode, $opt) = @_;
Subroutine open: not called CC=6 SCAR=37.4
Pod documented
38 0 ▶ ref($self) or $self = $self->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $self or $self = $self->new 0 0 0
39  
40 # Because we do rename(2) atomically, temporary file must be in same
41 # partion with target file.
42 0 my $temp = mktemp("${path}.XXXXXX");
43 0 $self->_temp_file($temp);
44 0 $self->_target_file($path);
45  
46 0 ▶ copy_preserving_attr($path, $temp) if -f $path;
Branch: 1 branch
BranchTrueFalseTotal
if -f $path 0 0 0
47 0 ▶ if (exists $opt->{backup_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt->{"backup_dir"}) 0 0 0
48 0 ▶ unless (-d $opt->{backup_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $opt->{"backup_dir"}) 0 0 0
49 0 croak "no such directory: $opt->{backup_dir}";
50 }
51 0 $self->_backup_dir($opt->{backup_dir});
52 }
53  
54 0 ▶ $self->SUPER::open($temp, $mode) ? $self : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::open($temp, $mode) ? : 0 0 0
55 }
56  
57 sub _closed {
58 0 ▶ my $self = shift;
Subroutine _closed: not called CC=2 SCAR=17.9
59 0 my $tag = "io_file_atomicchange_closed";
60  
61 0 my $oldval = ${*$self}{$tag};
62 0 ▶ ${*$self}{$tag} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
63 0 return $oldval;
64 }
65  
66 sub close {
67 0 ▶ my ($self, $die) = @_;
Subroutine close: not called CC=9 SCAR=45.0
Pod undocumented
68 0 ▶ $self->sync() or croak "sync: $!";
Branch: 1 branch
BranchTrueFalseTotal
$self->sync or croak("sync: $!") 0 0 0
69 0 ▶ unless ($self->_closed(1)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_closed(1)) 0 0 0
70 0 ▶ if ($self->SUPER::close()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->SUPER::close) { } 0 0 0
71  
72 0 ▶ $self->backup if ($self->_backup_dir && -f $self->_target_file);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_backup_dir and -f $self->_target_file 0 0 0
Condition0%0/3
$self->_backup_dir and -f $self->_target_file
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_backup_dir and -f $self->_target_file
$self->_backup_dir -f $self->_target_file
Truth table0%0/3
A and B
A= $self->_backup_dirB= -f $self->_target_file
ABresult
0X0
100
111
73  
74 0 ▶ rename($self->_temp_file, $self->_target_file)
Branch: 2 branches
BranchTrueFalseTotal
rename $self->_temp_file, $self->_target_file or $die ? croak("close (rename) atomic file: $!\n") : (return) 0 0 0
$die ? : 0 0 0
75 or ($die ? croak "close (rename) atomic file: $!\n" : return);
76 } else {
77 0 ▶ $die ? croak "close atomic file: $!\n" : return;
Branch: 1 branch
BranchTrueFalseTotal
$die ? : 0 0 0
78 }
79 }
80 0 1;
81 }
82  
83 sub copy_modown_to_temp {
84 0 ▶ my($self) = @_;
Subroutine copy_modown_to_temp: not called CC=1 SCAR=6.9
Pod undocumented
85  
86 0 my($mode, $uid, $gid) = (stat($self->_target_file))[2,4,5];
87 0 chown $uid, $gid, $self->_temp_file;
88 0 chmod $mode, $self->_temp_file;
89 }
90  
91 sub backup {
92 0 ▶ my($self) = @_;
Subroutine backup: not called CC=5 SCAR=34.0
Pod undocumented
93  
94 0 require Path::Class;
95 0 require POSIX;
96 0 require Time::HiRes;
97  
98 0 my $basename = Path::Class::file($self->_target_file)->basename;
99  
100 0 my $backup_file;
101 0 my $n = 0;
102 0 ▶ while ($n < 7) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 7) 0 0 0
103 0 ▶ $backup_file = sprintf("%s/%s_%s.%d_%d%s",
Branch: 1 branch
BranchTrueFalseTotal
$n == 0 ? : 0 0 0
104 $self->_backup_dir,
105 $basename,
106 POSIX::strftime("%Y-%m-%d_%H%M%S",localtime()),
107 (Time::HiRes::gettimeofday())[1],
108 $$,
109 ($n == 0 ? "" : ".$n"),
110 );
111 0 ▶ last unless -f $backup_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $backup_file 0 0 0
112 0 $n++;
113 }
114 0 ▶ croak "already exists backup file: $backup_file" if -f $backup_file;
Branch: 1 branch
BranchTrueFalseTotal
if -f $backup_file 0 0 0
115  
116 0 copy_preserving_attr($self->_target_file, $backup_file);
117 }
118  
119  
120 sub delete {
121 0 ▶ my $self = shift;
Subroutine delete: not called CC=2 SCAR=17.9
Pod undocumented
122 0 ▶ unless ($self->_closed(1)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_closed(1)) 0 0 0
123 0 $self->SUPER::close();
124 0 return unlink($self->_temp_file);
125 }
126 0 1;
127 }
128  
129 sub detach {
130 0 ▶ my $self = shift;
Subroutine detach: not called CC=2 SCAR=17.9
Pod undocumented
131 0 ▶ $self->SUPER::close() unless ($self->_closed(1));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_closed(1) 0 0 0
132 0 1;
133 }
134  
135 sub copy_preserving_attr {
136 0 ▶ my($from, $to) = @_;
Subroutine copy_preserving_attr: not called CC=2 SCAR=17.9
Pod undocumented
137  
138 0 ▶ File::Copy::copy($from, $to) or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
File::Copy::copy($from, $to) or croak($!) 0 0 0
139  
140 0 my($mode, $uid, $gid, $atime, $mtime) = (stat($from))[2,4,5,8,9];
141 0 chown $uid, $gid, $to;
142 0 chmod $mode, $to;
143 0 utime $atime, $mtime, $to;
144 0 1;
145 }
146  
147  
148 1 1;
149 __END__
↑ Summary Build »