Transactional.pm

filter: Statementstmt 93.7% 493 / 526 Branchbran 73.1% 180 / 246 Conditioncond 70.5% 24 / 34 MC/DCmcdc 47.6% 10 / 21 Subroutinesub 91.6% 88 / 96 Podpod 93.1% 27 / 29 totaltotal 86.3% 822 / 952 CC 147 SCAR 16.9
CC
147
Cov
86%
merge_overlay
17.3
readdir
13.7
txn_do
12.6
CRAP 5.4
« Transactional/Stream.pm ↑ Summary Transactional/TXN/Nested.pm »
1 #!/usr/bin/perl
2 # ABSTRACT: ACID transactions on a directory tree
3  
4 package Directory::Transactional;
5 8 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 $Directory::Transactional::VERSION = '0.09';
7 }
8 8 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 ▶use Time::HiRes qw(alarm);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 8 ▶use Set::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use Fcntl qw(LOCK_EX LOCK_SH LOCK_NB);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 8 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶use File::Find qw(find);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use File::Path qw(make_path remove_tree);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 8 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 8 ▶use IO::Dir;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 8 ▶use Directory::Transactional::TXN::Root;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 8 ▶use Directory::Transactional::TXN::Nested;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 #use Directory::Transactional::Stream; # we require it later, it wants real Moose
26  
27 8 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 8 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 8 has root => (
32 is => "ro",
33 required => 1,
34 );
35  
36 8 has _fatal => (
37 isa => "Bool",
38 is => "rw",
39 );
40  
41 8 has [qw(_root _work _backups _txns _locks _dirty _dirty_lock)] => (
42 isa => "Str",
43 is => "ro",
44 lazy_build => 1,
45 );
46  
47 19 ▶sub _build__root { my $self = shift; blessed($self->root) ? $self->root->stringify : $self->root }
Subroutine _build__root: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($self->root) ? : 16 3 19
48 18 ▶sub _build__work { File::Spec->catdir(shift->_root, ".txn_work_dir") } # top level for all temp files
Subroutine _build__work: called CC=1 SCAR=0.0
49 10 ▶sub _build__txns { File::Spec->catdir(shift->_work, "txns") } # one subdir per transaction, used for temporary files when transactions are active
Subroutine _build__txns: called CC=1 SCAR=0.0
50 20 ▶sub _build__backups { File::Spec->catdir(shift->_work, "backups") } # one subdir per transaction, used during commit to root
Subroutine _build__backups: called CC=1 SCAR=0.0
51 20 ▶sub _build__locks { File::Spec->catdir(shift->_work, "locks") } # shared between all workers, directory for lockfiles
Subroutine _build__locks: called CC=1 SCAR=0.0
52 20 ▶sub _build__dirty { File::Spec->catfile(shift->_work, "dirty") }
Subroutine _build__dirty: called CC=1 SCAR=0.0
53 20 ▶sub _build__dirty_lock { shift->_dirty . ".lock" }
Subroutine _build__dirty_lock: called CC=1 SCAR=0.0
54  
55 8 has nfs => (
56 isa => "Bool",
57 is => "ro",
58 default => 0,
59 );
60  
61 has global_lock => (
62 isa => "Bool",
63 is => "ro",
64 lazy => 1,
65 17 ▶ default => sub { shift->nfs },
Subroutine __ANON__: called CC=1 SCAR=0.0
66 8 );
67  
68 8 has auto_commit => (
69 isa => "Bool",
70 is => "ro",
71 default => 1,
72 );
73  
74 8 has crash_detection => (
75 isa => "Bool",
76 is => "ro",
77 default => 1,
78 );
79  
80 8 has timeout => (
81 isa => "Num",
82 is => "ro",
83 predicate => "has_timeout",
84 );
85  
86 231 ▶sub _get_lock {
Subroutine _get_lock: called CC=2 SCAR=6.9
87 231 my ( $self, @args ) = @_;
88  
89 231 ▶ return $self->nfs ? $self->_get_nfslock(@args) : $self->_get_flock(@args);
Branch: 1 branch
BranchTrueFalseTotal
$self->nfs ? : 59 172 231
90 }
91  
92 # slow, portable locking
93 # relies on atomic link()
94 # on OSX the stress test gets race conditions
95 59 ▶sub _get_nfslock {
Subroutine _get_nfslock: called CC=5 SCAR=17.4
96 59 my ( $self, $file, $mode ) = @_;
97  
98 # create the parent directory for the lock if necessary
99 # (the lock dir is cleaned on destruction)
100 59 my ( $vol, $dir ) = File::Spec->splitpath($file);
101 59 my $parent = File::Spec->catpath($vol, $dir, '');
102 59 ▶ make_path($parent) unless -d $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $parent 1 58 59
103  
104 59 require File::NFSLock;
105 59 ▶ if ( my $lock = File::NFSLock->new({
Branch: 3 branches
BranchTrueFalseTotal
if (my $lock = 'File::NFSLock'->new({'file', $file, 'lock_type', $mode, $self->has_timeout ? ('blocking_timeout', $self->timeout) : ()})) { } 59 0 59
elsif (not $mode & 4) { } 0 0 0
$self->has_timeout ? : 0 59 59
106 file => $file,
107 lock_type => $mode,
108 ( $self->has_timeout ? ( blocking_timeout => $self->timeout ) : () ),
109 }) ) {
110 59 return $lock;
111 } elsif ( not($mode & LOCK_NB) ) {
112 8 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
113 0 die $File::NFSLock::errstr;
114 }
115  
116 0 return;
117 }
118  
119 # much faster locking, doesn't work on NFS though
120 269 ▶sub _get_flock {
Subroutine _get_flock: called CC=9 SCAR=22.3
121 269 my ( $self, $file, $mode ) = @_;
122  
123 # create the parent directory for the lock if necessary
124 # (the lock dir is cleaned on destruction)
125 269 my ( $vol, $dir ) = File::Spec->splitpath($file);
126 269 my $parent = File::Spec->catpath($vol, $dir, '');
127 269 ▶ make_path($parent) unless -d $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $parent 9 260 269
128  
129 # open the lockfile, creating if necessary
130 269 ▶ open my $fh, "+>", $file or die $!;
Branch: 1 branch
BranchTrueFalseTotal
CORE::open my $fh, "+>", $file or die $! 269 0 269
131  
132 269 my $ret;
133  
134 269 ▶ if ( not($mode & LOCK_NB) and $self->has_timeout ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $mode & 4 and $self->has_timeout) { } 7 262 269
Condition100%3/3
not $mode & 4 and $self->has_timeout
!ll&&!rl&&r
552077
MC/DC100%2/2
not $mode & 4 and $self->has_timeout
not $mode & 4 $self->has_timeout
Truth table100%3/3
not(A) and B
A= not $mode & 4B= $self->has_timeout
ABresult
0X0
100
111
135 0 ▶ local $SIG{ALRM} = sub { croak "Lock timed out" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
136 7 alarm($self->timeout);
137 7 $ret = flock($fh, $mode);
138 7 alarm(0);
139 } else {
140 262 $ret = flock($fh, $mode);
141 }
142  
143 269 ▶ if ( $ret ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ret) { } 251 18 269
elsif ($!{'EWOULDBLOCK'} or $!{'EAGAIN'}) { } 18 0 18
144 251 ▶ my $class = ($mode & LOCK_EX) ? "Directory::Transactional::Lock::Exclusive" : "Directory::Transactional::Lock::Shared";
Branch: 1 branch
BranchTrueFalseTotal
$mode & 2 ? : 181 70 251
145 251 return bless $fh, $class;
146 1 ▶ } elsif ( $!{EWOULDBLOCK} or $!{EAGAIN} ) {
Subroutine BEGIN: called CC=1 SCAR=0.4
Condition33%1/3
$!{"EWOULDBLOCK"} or $!{"EAGAIN"}
l!l&&r!l&&!r
1800
MC/DC0%0/2
$!{"EWOULDBLOCK"} or $!{"EAGAIN"}
$!{"EWOULDBLOCK"} $!{"EAGAIN"}
Truth table33%1/3
A or B
A= $!{"EWOULDBLOCK"}B= $!{"EAGAIN"}
ABresult
1X1
011
000
147 # LOCK_NB failed
148 18 return;
149 } else {
150 # die on any error except failing to obtain a nonblocking lock
151 0 die $!;
152 }
153 }
154  
155 # support methods for fine grained locking
156 {
157 package Directory::Transactional::Lock;
158 8 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
159 8 $Directory::Transactional::Lock::VERSION = '0.09';
160 }
161  
162 0 ▶ sub unlock { close $_[0] }
Subroutine unlock: not called CC=1 SCAR=6.9
163 4 ▶ sub is_exclusive { 0 }
Subroutine is_exclusive: called CC=1 SCAR=0.0
164 0 ▶ sub is_shared { 0 }
Subroutine is_shared: not called CC=1 SCAR=6.9
165 11 ▶ sub upgrade { }
Subroutine upgrade: called CC=1 SCAR=0.0
166 0 ▶ sub upgrade_nb { $_[0] }
Subroutine upgrade_nb: not called CC=1 SCAR=6.9
167 0 ▶ sub downgrade { }
Subroutine downgrade: not called CC=1 SCAR=0.0
168  
169 package Directory::Transactional::Lock::Exclusive;
170 8 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
171 8 $Directory::Transactional::Lock::Exclusive::VERSION = '0.09';
172 }
173 8 ▶ use Fcntl qw(LOCK_SH);
Subroutine BEGIN: called CC=1 SCAR=0.0
174  
175 8 ▶ BEGIN { our @ISA = qw(Directory::Transactional::Lock) }
Subroutine BEGIN: called CC=1 SCAR=0.0
176  
177 1 ▶ sub is_exclusive { 1 }
Subroutine is_exclusive: called CC=1 SCAR=0.0
178  
179 1 ▶ sub downgrade {
Subroutine downgrade: called CC=2 SCAR=7.1
180 1 my $self = shift;
181 1 ▶ flock($self, LOCK_SH) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
flock $self, 1 or die $! 1 0 1
182 1 bless $self, "Directory::Transactional::Lock::Shared";
183 }
184  
185 package Directory::Transactional::Lock::Shared;
186 8 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
187 8 $Directory::Transactional::Lock::Shared::VERSION = '0.09';
188 }
189 8 ▶ use Fcntl qw(LOCK_EX LOCK_NB);
Subroutine BEGIN: called CC=1 SCAR=0.0
190  
191 8 ▶ BEGIN { our @ISA = qw(Directory::Transactional::Lock) }
Subroutine BEGIN: called CC=1 SCAR=0.0
192  
193 0 ▶ sub is_shared { 1 }
Subroutine is_shared: not called CC=1 SCAR=6.9
194 9 ▶ sub upgrade {
Subroutine upgrade: called CC=2 SCAR=7.1
195 9 my $self = shift;
196 9 ▶ flock($self, LOCK_EX) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
flock $self, 2 or die $! 9 0 9
197 9 bless($self, "Directory::Transactional::Lock::Exclusive");
198 }
199  
200 2 ▶ sub upgrade_nb {
Subroutine upgrade_nb: called CC=3 SCAR=12.7
201 2 my $self = shift;
202  
203 2 ▶ unless ( flock($self, LOCK_EX|LOCK_NB) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (flock $self, 6) 2 0 2
204 2 ▶ if ( $!{EWOULDBLOCK} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($!{'EWOULDBLOCK'}) { } 2 0 2
205 2 return;
206 } else {
207 0 die $!;
208 }
209 }
210  
211 0 bless($self, "Directory::Transactional::Lock::Exclusive");
212 }
213 }
214  
215 # this is the current active TXN (head of transaction stack)
216 8 has _txn => (
217 isa => "Directory::Transactional::TXN",
218 is => "rw",
219 clearer => "_clear_txn",
220 );
221  
222 8 has _shared_lock_file => (
223 isa => "Str",
224 is => "ro",
225 lazy_build => 1,
226 );
227  
228 20 ▶sub _build__shared_lock_file { shift->_work . ".lock" }
Subroutine _build__shared_lock_file: called CC=1 SCAR=0.0
229  
230 8 has _shared_lock => (
231 is => "ro",
232 lazy_build => 1,
233 );
234  
235 # the shared lock is always taken at startup
236 # a nonblocking attempt to lock it exclusively is made first, and if granted we
237 # have exclusive access to the work directory so recovery is run if necessary
238 20 ▶sub _build__shared_lock {
Subroutine _build__shared_lock: called CC=2 SCAR=6.9
239 20 my $self = shift;
240  
241 20 my $file = $self->_shared_lock_file;
242  
243 20 ▶ if ( my $ex_lock = $self->_get_lock( $file, LOCK_EX|LOCK_NB ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ex_lock = $self->_get_lock($file, 6)) 10 10 20
244 10 $self->recover;
245  
246 10 undef $ex_lock;
247 }
248  
249 20 $self->_get_lock($file, LOCK_SH);
250 }
251  
252 20 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.3
Pod undocumented
253 20 my $self = shift;
254  
255 20 ▶ croak "If 'nfs' is set then so must be 'global_lock'"
Branch: 1 branch
BranchTrueFalseTotal
if $self->nfs and not $self->global_lock 0 20 20
Condition66%2/3
$self->nfs and not $self->global_lock
!ll&&!rl&&r
1910
MC/DC0%0/2
$self->nfs and not $self->global_lock
$self->nfs not $self->global_lock
Truth table66%2/3
A and not(B)
A= $self->nfsB= not $self->global_lock
ABresult
0X0
100
111
256 if $self->nfs and !$self->global_lock;
257  
258 # obtains the shared lock, running recovery if needed
259 20 $self->_shared_lock;
260  
261 20 make_path($self->_work);
262 }
263  
264 20 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=4 SCAR=13.9
Pod undocumented
265 20 my $self = shift;
266  
267 20 ▶ return if $self->_fatal; # encountered a fatal error, we need to run recovery
Branch: 1 branch
BranchTrueFalseTotal
if $self->_fatal 2 18 20
268  
269 # rollback any open txns
270 18 ▶ while ( $self->_txn ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_txn) 0 18 18
271 0 $self->txn_rollback;
272 }
273  
274 # lose the shared lock
275 18 $self->_clear_shared_lock;
276  
277 # cleanup workdirs
278 # only remove if no other workers are active, so that there is no race
279 # condition in their directory creation code
280 18 ▶ if ( my $ex_lock = $self->_get_lock( $self->_shared_lock_file, LOCK_EX|LOCK_NB ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ex_lock = $self->_get_lock($self->_shared_lock_file, 6)) 10 8 18
281 # we don't really care if there's an error
282 10 ▶ try { local $SIG{__WARN__} = sub { }; remove_tree($self->_locks) };
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: not called CC=1 SCAR=0.0
283 10 rmdir $self->_work;
284 10 rmdir $self->_txns;
285 10 rmdir $self->_backups;
286  
287 10 unlink $self->_dirty;
288 10 unlink $self->_dirty_lock;
289  
290 10 rmdir $self->_work;
291  
292 10 CORE::unlink $self->_shared_lock_file;
293 }
294 }
295  
296 106 ▶sub check_dirty {
Subroutine check_dirty: called CC=8 SCAR=21.2
Pod documented
297 106 my $self = shift;
298  
299 106 ▶ return unless $self->crash_detection;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->crash_detection 0 106 106
300  
301 # get the short lived dirty flag manipulation lock
302 # nobody else can check or modify the dirty flag while we have it
303 106 my $ex_lock = $self->_get_lock( $self->_dirty_lock, LOCK_EX );
304  
305 106 my $dirty = $self->_dirty;
306  
307 # if the dirty flag is set, run a check
308 106 ▶ if ( -e $dirty ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $dirty) 27 79 106
309 27 my $b = $self->_backups;
310  
311 # go through the comitting transactions
312 27 foreach my $name ( IO::Dir->new($b)->read ) {
313 73 ▶ next unless $name =~ /^[\w\-]+$/; # txn dir
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^[\w\-]+$/ 71 2 73
314  
315 2 my $dir = File::Spec->catdir($b, $name);
316  
317 2 ▶ if ( my $ex_lock = $self->_get_lock( $dir . ".lock", LOCK_EX|LOCK_NB ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ex_lock = $self->_get_lock($dir . '.lock', 6)) 2 0 2
318 # there is a potential race condition between the readdir
319 # and getting the lock. make sure it still exists
320 2 ▶ if ( -d $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $dir) 2 0 2
321 2 $self->online_recover;
322 0 return $ex_lock;
323 }
324 }
325 }
326  
327 # the check passed, now we can clear the dirty flag if there are no
328 # other running commits
329 25 ▶ if ( my $flag_ex_lock = $self->_get_lock( $dirty, LOCK_EX|LOCK_NB ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $flag_ex_lock = $self->_get_lock($dirty, 6)) 25 0 25
330 25 unlink $dirty;
331 }
332 }
333  
334 # return the lock.
335 # for as long as it is held the workdir cannot be marked dirty except by
336 # this process
337 104 return $ex_lock;
338 }
339  
340 20 ▶sub set_dirty {
Subroutine set_dirty: called CC=4 SCAR=14.2
Pod documented
341 20 my $self = shift;
342  
343 20 ▶ return unless $self->crash_detection;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->crash_detection 0 20 20
344  
345 # first check that the dir is not dirty, and take an exclusive lock for
346 # dirty flag manipulation
347 20 my $ex_lock = $self->check_dirty;
348  
349 # next mark the dir as dirty, and take a shared lock so the flag won't be
350 # cleared by check_dirty
351  
352 20 my $dirty_lock = $self->_get_lock( $self->_dirty, LOCK_SH );
353  
354 # create the file if necessary (nfs uses an auxillary lock file)
355 20 ▶ open my $fh, ">", $self->_dirty or die $! if $self->nfs;
Branch: 1 branch
BranchTrueFalseTotal
if $self->nfs 9 11 20
Condition60%3/5
$self->nfs && (CORE::open(my $fh, '>', $self->_dirty) || die($!))
!ll&&!rl&&r
1190
CORE::open my $fh, ">", $self->_dirty or die $!
l!l
90
MC/DC0%0/2
$self->nfs && (CORE::open(my $fh, '>', $self->_dirty) || die($!))
$self->nfs CORE::open my $fh, ">", $self->_dirty
Truth table33%1/3
A && B
A= $self->nfsB= CORE::open my $fh, ">", $self->_dirty
ABresult
0X0
100
111
356  
357 20 return $dirty_lock;
358 }
359  
360 10 ▶sub recover {
Subroutine recover: called CC=7 SCAR=19.5
Pod documented
361 10 my $self = shift;
362  
363 # first rollback partially comitted transactions if there are any
364 10 ▶ if ( -d ( my $b = $self->_backups ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d (my $b = $self->_backups)) 2 8 10
365 2 foreach my $name ( IO::Dir->new($b)->read ) {
366 7 ▶ next if $name eq '.' || $name eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "." or $name eq ".." 4 3 7
Condition100%3/3
$name eq "." or $name eq ".."
l!l&&r!l&&!r
223
MC/DC100%2/2
$name eq "." or $name eq ".."
$name eq "." $name eq ".."
Truth table100%3/3
A or B
A= $name eq "."B= $name eq ".."
ABresult
1X1
011
000
367  
368 3 my $txn_backup = File::Spec->catdir($b, $name); # each of these is one transaction
369  
370 3 ▶ if ( -d $txn_backup ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $txn_backup) 2 1 3
371 2 my $files = $self->_get_file_list($txn_backup);
372  
373 # move all the backups back into the root directory
374 2 $self->merge_overlay( from => $txn_backup, to => $self->_root, files => $files );
375  
376 2 remove_tree($txn_backup);
377 }
378 }
379  
380 2 remove_tree($b, { keep_root => 1 });
381 }
382  
383 # delete all temp files (fully comitted but not cleaned up transactions,
384 # and uncomitted transactions)
385 10 ▶ if ( -d $self->_txns ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $self->_txns) 2 8 10
386 2 remove_tree( $self->_txns, { keep_root => 1 } );
387 }
388  
389 10 unlink $self->_dirty;
390 10 unlink $self->_dirty_lock;
391 }
392  
393 2 ▶sub online_recover {
Subroutine online_recover: called CC=3 SCAR=12.6
Pod documented
394 2 my $self = shift;
395  
396 2 ▶ unless ( $self->nfs ) { # can't upgrade an nfs lock
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->nfs) 2 0 2
397 2 my $lock = $self->_shared_lock;
398  
399 2 ▶ if ( $lock->upgrade_nb ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lock->upgrade_nb) 0 2 2
400 0 $self->recover;
401 0 $lock->downgrade;
402 0 return 1;
403 }
404 }
405  
406 2 $self->_fatal(1);
407 2 croak "Detected crashed transaction, terminate all processes and run recovery by reinstantiating directory";
408 }
409  
410 2 ▶sub _get_file_list {
Subroutine _get_file_list: called CC=1 SCAR=0.0
411 2 my ( $self, $from ) = @_;
412  
413 2 my $files = Set::Object->new;
414  
415 2 ▶ find( { no_chdir => 1, wanted => sub { $files->insert( File::Spec->abs2rel($_, $from) ) if -f $_ } }, $from );
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if -f $_ 2 3 5
416  
417 2 return $files;
418 }
419  
420 28 ▶sub merge_overlay {
Subroutine merge_overlay: called CC=15 SCAR=28.5
Pod documented
421 28 my ( $self, %args ) = @_;
422  
423 28 my ( $from, $to, $backup, $files ) = @args{qw(from to backup files)};
424  
425 28 my @rem;
426  
427 # if requested, back up first by moving all the files from the target
428 # directory to the backup directory
429 28 ▶ if ( $backup ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($backup) 20 8 28
430 20 foreach my $file ( $files->members ) {
431 25 my $src = File::Spec->catfile($to, $file);
432  
433 25 ▶ next unless -e $src; # there is no source file to back
Branch: 1 branch
BranchTrueFalseTotal
unless -e $src 9 16 25
434  
435 16 my $targ = File::Spec->catfile($backup, $file);
436  
437 # create the parent directory in the backup dir as necessary
438 16 my ( undef, $dir ) = File::Spec->splitpath($targ);
439 16 ▶ if ( $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) 16 0 16
440 16 ▶ make_path($dir) unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 12 4 16
441 }
442  
443 16 ▶ CORE::rename $src, $targ or die $!;
Branch: 1 branch
BranchTrueFalseTotal
CORE::rename $src, $targ or die $! 16 0 16
444 }
445 }
446  
447 # then apply all the changes to the target dir from the source dir
448 28 foreach my $file ( $files->members ) {
449 35 my $src = File::Spec->catfile($from,$file);
450  
451 35 ▶ if ( -f $src ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $src) 22 13 35
452 22 my $targ = File::Spec->catfile($to,$file);
453  
454 # make sure the parent directory in the target path exists first
455 22 my ( undef, $dir ) = File::Spec->splitpath($targ);
456 22 ▶ if ( $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) 22 0 22
457 22 ▶ make_path($dir) unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 2 20 22
458 }
459  
460 22 ▶ if ( -f $src ) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $src) { } 22 0 22
elsif (-f $targ) { } 0 0 0
461 22 ▶ CORE::rename $src => $targ or die $!;
Branch: 1 branch
BranchTrueFalseTotal
CORE::rename $src, $targ or die $! 22 0 22
462 } elsif ( -f $targ ) {
463 0 ▶ CORE::unlink $targ or die $!;
Branch: 1 branch
BranchTrueFalseTotal
CORE::unlink $targ or die $! 0 0 0
464 }
465 }
466 }
467 }
468  
469 28 ▶sub txn_do {
Subroutine txn_do: called CC=11 SCAR=25.3
Pod documented
470 28 my ( $self, @args ) = @_;
471  
472 28 ▶ unshift @args, "body" if @args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 28 0 28
473  
474 28 my %args = @args;
475  
476 28 my ( $coderef, $commit, $rollback, $code_args ) = @args{qw(body commit rollback args)};
477  
478 28 ▶ ref $coderef eq 'CODE' or croak '$coderef must be a CODE reference';
Branch: 1 branch
BranchTrueFalseTotal
ref $coderef eq "CODE" or croak("\$coderef must be a CODE reference") 28 0 28
479  
480 28 ▶ $code_args ||= [];
Condition50%1/2
$code_args ||= []
l!l
028
MC/DC0%0/1
$code_args ||= []
$code_args
Truth table50%1/2
A
A= $code_args
Aresult
11
00
481  
482 28 $self->txn_begin;
483  
484 28 my @result;
485  
486 28 my $wantarray = wantarray; # gotta capture, eval { } has its own
487  
488 28 my ( $success, $err ) = do {
489 28 local $@;
490  
491 28 my $success = eval {
492 28 ▶ if ( $wantarray ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($wantarray) { } 0 28 28
elsif (defined $wantarray) { } 0 28 28
493 0 @result = $coderef->(@$code_args);
494 } elsif( defined $wantarray ) {
495 0 $result[0] = $coderef->(@$code_args);
496 } else {
497 28 $coderef->(@$code_args);
498 }
499  
500 22 ▶ $commit && $commit->();
Branch: 1 branch
BranchTrueFalseTotal
$commit and &$commit() 0 22 22
501 22 $self->txn_commit;
502  
503 22 1;
504 };
505  
506 28 ( $success, $@ );
507 };
508  
509 28 ▶ if ( $success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success) { } 22 6 28
510 22 ▶ return wantarray ? @result : $result[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 22 22
511 } else {
512 6 my $rollback_exception = do {
513 6 local $@;
514 6 ▶ eval { $self->txn_rollback; $rollback && $rollback->() };
Branch: 1 branch
BranchTrueFalseTotal
$rollback and &$rollback() 0 6 6
515 6 $@;
516 };
517  
518 6 ▶ if ($rollback_exception) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rollback_exception) 0 6 6
519 0 croak "Transaction aborted: $err, rollback failed: $rollback_exception";
520 }
521  
522 6 die $err;
523 }
524 }
525  
526 52 ▶sub txn_begin {
Subroutine txn_begin: called CC=4 SCAR=13.9
Pod documented
527 52 my ( $self, @args ) = @_;
528  
529 52 my $txn;
530  
531 52 ▶ if ( my $p = $self->_txn ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $p = $self->_txn) { } 9 43 52
532 # this is a child transaction
533  
534 9 ▶ croak "Can't txn_begin if an auto transaction is still alive" if $p->auto_handle;
Branch: 1 branch
BranchTrueFalseTotal
if $p->auto_handle 1 8 9
535  
536 8 $txn = Directory::Transactional::TXN::Nested->new(
537 parent => $p,
538 manager => $self,
539 );
540 } else {
541 # this is a top level transaction
542 43 ▶ $txn = Directory::Transactional::TXN::Root->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->global_lock ? : 10 33 43
543 @args,
544 manager => $self,
545 ( $self->global_lock ? (
546 # when global_lock is set, take an exclusive lock on the root dir
547 # non global lockers take a shared lock on it
548 global_lock => $self->_get_flock( File::Spec->catfile( $self->_locks, ".lock" ), LOCK_EX)
549 ) : () ),
550 );
551 }
552  
553 51 $self->_txn($txn);
554  
555 51 return;
556 }
557  
558 51 ▶sub _pop_txn {
Subroutine _pop_txn: called CC=3 SCAR=11.0
559 51 my $self = shift;
560  
561 51 ▶ my $txn = $self->_txn or croak "No active transaction";
Branch: 1 branch
BranchTrueFalseTotal
my $txn = $self->_txn or croak("No active transaction") 51 0 51
562  
563 51 ▶ if ( $txn->isa("Directory::Transactional::TXN::Nested") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($txn->isa('Directory::Transactional::TXN::Nested')) { } 8 43 51
564 8 $self->_txn( $txn->parent );
565 } else {
566 43 $self->_clear_txn;
567 }
568  
569 51 return $txn;
570 }
571  
572 44 ▶sub txn_commit {
Subroutine txn_commit: called CC=4 SCAR=13.9
Pod documented
573 44 my $self = shift;
574  
575 44 my $txn = $self->_txn;
576  
577 44 my $changed = $txn->changed;
578  
579 44 ▶ if ( $changed->size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changed->size) 26 18 44
580 26 ▶ if ( $txn->isa("Directory::Transactional::TXN::Root") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($txn->isa('Directory::Transactional::TXN::Root')) { } 20 6 26
581 # commit the work, backing up in the backup dir
582  
583 # first take a lock on the backup dir
584 # this is used to detect crashed transactions
585 # if the dir exists but isn't locked then the transaction crashed
586 20 my $txn_lockfile = $txn->backup . ".lock";
587 20 my $txn_lock = $self->_get_lock( $txn_lockfile, LOCK_EX );
588  
589 {
590 # during a commit the work dir is considered dirty
591 # this flag is set until check_dirty clears it
592 20 my $dirty_lock = $self->set_dirty;
593  
594 20 $txn->create_backup_dir;
595  
596 # move all the files from the txn dir into the root dir, using the backup dir
597 20 $self->merge_overlay( from => $txn->work, to => $self->_root, backup => $txn->backup, files => $changed );
598  
599 # we're finished, remove backup dir denoting successful commit
600 20 ▶ CORE::rename $txn->backup, $txn->work . ".cleanup" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
CORE::rename $txn->backup, $txn->work . '.cleanup' or die $! 20 0 20
601 }
602  
603 20 unlink $txn_lockfile;
604 } else {
605 # it's a nested transaction, which means we don't need to be
606 # careful about comitting to the parent, just share all the locks,
607 # deletion metadata etc by merging it
608 6 $txn->propagate;
609  
610 6 $self->merge_overlay( from => $txn->work, to => $txn->parent->work, files => $changed );
611 }
612  
613 # clean up work dir and (renamed) backup dir
614 26 remove_tree( $txn->work );
615 26 remove_tree( $txn->work . ".cleanup" );
616 }
617  
618 44 $self->_pop_txn;
619  
620 44 return;
621 }
622  
623 7 ▶sub txn_rollback {
Subroutine txn_rollback: called CC=5 SCAR=16.3
Pod documented
624 7 my $self = shift;
625  
626 7 my $txn = $self->_pop_txn;
627  
628 7 ▶ if ( $txn->isa("Directory::Transactional::TXN::Root") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($txn->isa('Directory::Transactional::TXN::Root')) { } 5 2 7
629 # an error happenned during txn_commit trigerring a rollback
630 5 ▶ if ( -d ( my $txn_backup = $txn->backup ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d (my $txn_backup = $txn->backup)) 0 5 5
631 0 my $files = $self->_get_file_list($txn_backup);
632  
633 # move all the backups back into the root directory
634 0 $self->merge_overlay( from => $txn_backup, to => $self->_root, files => $files );
635 }
636 } else {
637 # any inherited locks that have been upgraded in this txn need to be
638 # downgraded back to shared locks
639 2 foreach my $lock ( @{ $txn->downgrade } ) {
640 1 $lock->downgrade;
641 }
642 }
643  
644 # now all we need to do is trash the tempfiles and we're done
645 7 ▶ if ( $txn->has_work ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($txn->has_work) 3 4 7
646 3 remove_tree( $txn->work );
647 }
648  
649 7 return;
650 }
651  
652 120 ▶sub _auto_txn {
Subroutine _auto_txn: called CC=3 SCAR=11.0
653 120 my $self = shift;
654  
655 120 ▶ return if $self->_txn;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_txn 117 3 120
656  
657 3 ▶ croak "Auto commit is disabled" unless $self->auto_commit;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->auto_commit 0 3 3
658  
659 3 require Scope::Guard;
660  
661 3 $self->txn_begin;
662  
663 3 ▶ return Scope::Guard->new(sub { $self->txn_commit });
Subroutine __ANON__: called CC=1 SCAR=0.0
664 }
665  
666 56 ▶sub _resource_auto_txn {
Subroutine _resource_auto_txn: called CC=3 SCAR=11.0
667 56 my $self = shift;
668  
669 56 ▶ if ( my $txn = $self->_txn ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $txn = $self->_txn) { } 46 10 56
670 # return the same handle so that more resources can be registered
671 46 return $txn->auto_handle;
672 } else {
673 10 ▶ croak "Auto commit is disabled" unless $self->auto_commit;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->auto_commit 2 8 10
674  
675 8 require Directory::Transactional::AutoCommit;
676  
677 8 my $h = Directory::Transactional::AutoCommit->new( manager => $self );
678  
679 8 $self->txn_begin( auto_handle => $h );
680  
681 8 return $h;
682 }
683 }
684  
685 174 ▶sub _lock_path_read {
Subroutine _lock_path_read: called CC=2 SCAR=6.9
686 174 my ( $self, $path ) = @_;
687  
688 174 my $txn = $self->_txn;
689  
690 174 ▶ if ( my $lock = $txn->find_lock($path) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $lock = $txn->find_lock($path)) { } 134 40 174
691 134 return $lock;
692 } else {
693 40 my $lock = $self->_get_flock( File::Spec->catfile( $self->_locks, $path . ".lock" ), LOCK_SH);
694 40 $txn->set_lock( $path, $lock );
695 }
696 }
697  
698 68 ▶sub _lock_path_write {
Subroutine _lock_path_write: called CC=4 SCAR=13.9
699 68 my ( $self, $path ) = @_;
700  
701 68 my $txn = $self->_txn;
702  
703 68 ▶ if ( my $lock = $txn->get_lock($path) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $lock = $txn->get_lock($path)) { } 16 52 68
elsif (my $inherited_lock = $txn->find_lock($path)) { } 5 47 52
704 # simplest scenario, we already have a lock in this transaction
705 16 $lock->upgrade; # upgrade it if necessary
706 } elsif ( my $inherited_lock = $txn->find_lock($path) ) {
707 # a parent transaction has a lock
708 5 ▶ unless ( $inherited_lock->is_exclusive ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($inherited_lock->is_exclusive) 4 1 5
709 # upgrade it, and mark for downgrade on rollback
710 4 $inherited_lock->upgrade;
711 4 push @{ $txn->downgrade }, $inherited_lock;
712 }
713 5 $txn->set_lock( $path, $inherited_lock );
714 } else {
715 # otherwise create a new lock
716 47 my $lock = $self->_get_flock( File::Spec->catfile( $self->_locks, $path . ".lock" ), LOCK_EX);
717 47 $txn->set_lock( $path, $lock );
718 }
719 }
720  
721 109 ▶sub _lock_parent {
Subroutine _lock_parent: called CC=6 SCAR=18.0
722 109 my ( $self, $path ) = @_;
723  
724 109 my ( undef, $dir ) = File::Spec->splitpath($path);
725  
726 109 my @dirs = File::Spec->splitdir($dir);
727  
728 {
729 8 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
730 109 ▶ pop @dirs unless length $dirs[-1]; # trailing slash
Branch: 1 branch
BranchTrueFalseTotal
unless length $dirs[-1] 109 0 109
731 }
732 109 ▶ pop @dirs if $dir eq $path;
Branch: 1 branch
BranchTrueFalseTotal
if $dir eq $path 7 102 109
733  
734 109 my $parent = "";
735  
736 109 ▶ do {
Branch: 2 branches
BranchTrueFalseTotal
@dirs and $parent = length $parent ? "File::Spec"->catdir($parent, shift @dirs) : shift @dirs and do { do { $self->_lock_path_read($parent) } } 24 109 133
length $parent ? : 0 24 24
Condition66%2/3
@dirs and $parent = length $parent ? "File::Spec"->catdir($parent, shift @dirs) : shift @dirs
!ll&&!rl&&r
109024
MC/DC50%1/2
@dirs and $parent = length $parent ? "File::Spec"->catdir($parent, shift @dirs) : shift @dirs
@dirs $parent = length $parent ? "File::Spec"->catdir($parent, shift @dirs) : shift @dirs
Truth table66%2/3
A and B
A= @dirsB= $parent = length $parent ? "File::Spec"->catdir($parent, shift @dirs) : shift @dirs
ABresult
0X0
100
111
737 133 $self->_lock_path_read($parent);
738 } while (
739 @dirs
740 and
741 $parent = length($parent)
742 ? File::Spec->catdir($parent, shift @dirs)
743 : shift @dirs
744 );
745  
746 109 return;
747 }
748  
749 # lock a path for reading
750 52 ▶sub lock_path_read {
Subroutine lock_path_read: called CC=3 SCAR=11.2
Pod documented
751 52 my ( $self, $path ) = @_;
752  
753 52 ▶ unless ( $self->_txn ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_txn) 0 52 52
754 0 croak("Can't lock file for reading without an active transaction");
755 }
756  
757 52 ▶ return if $self->global_lock;
Branch: 1 branch
BranchTrueFalseTotal
if $self->global_lock 11 41 52
758  
759 41 $self->_lock_parent($path);
760  
761 41 $self->_lock_path_read($path);
762  
763 41 return;
764 }
765  
766 93 ▶sub lock_path_write {
Subroutine lock_path_write: called CC=3 SCAR=11.2
Pod documented
767 93 my ( $self, $path ) = @_;
768  
769 93 ▶ unless ( $self->_txn ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_txn) 0 93 93
770 0 croak("Can't lock file for writing without an active transaction");
771 }
772  
773 93 ▶ return if $self->global_lock;
Branch: 1 branch
BranchTrueFalseTotal
if $self->global_lock 25 68 93
774  
775 68 $self->_lock_parent($path);
776  
777 68 $self->_lock_path_write($path);
778  
779 68 return;
780 }
781  
782 82 ▶sub _txn_stack {
Subroutine _txn_stack: called CC=3 SCAR=11.2
783 82 my $self = shift;
784  
785 82 ▶ if ( my $txn = $self->_txn ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $txn = $self->_txn) 82 0 82
786 82 my @ret = $txn;
787 82 ▶ push @ret, $txn = $txn->parent while $txn->can("parent");
Branch: 1 branch
BranchTrueFalseTotal
$txn->can("parent") and do { push @ret, $txn = $txn->parent } 16 82 98
788 82 return @ret;
789 }
790  
791 0 return;
792 }
793  
794 70 ▶sub _txn_for_path {
Subroutine _txn_for_path: called CC=5 SCAR=16.2
795 70 my ( $self, $path ) = @_;
796  
797 70 ▶ if ( my $txn = $self->_txn ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $txn = $self->_txn) 70 0 70
798 70 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$txn->can("parent") and $txn = $txn->parent and do { do { if ($txn->is_changed_in_head($path)) { return $txn; } } } 8 54 62
Condition66%2/3
$txn->can("parent") and $txn = $txn->parent
!ll&&!rl&&r
5408
MC/DC50%1/2
$txn->can("parent") and $txn = $txn->parent
$txn->can("parent") $txn = $txn->parent
Truth table66%2/3
A and B
A= $txn->can("parent")B= $txn = $txn->parent
ABresult
0X0
100
111
799 78 ▶ if ( $txn->is_changed_in_head($path) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($txn->is_changed_in_head($path)) 16 62 78
800 16 return $txn;
801 };
802 } while ( $txn->can("parent") and $txn = $txn->parent );
803 }
804  
805 54 return;
806 }
807  
808 32 ▶sub _locate_dirs_in_overlays {
Subroutine _locate_dirs_in_overlays: called CC=2 SCAR=6.9
809 32 my ( $self, $path ) = @_;
810  
811 32 my @dirs = ( (map { $_->work } $self->_txn_stack), $self->root );
812  
813 32 ▶ if ( defined $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $path) { } 18 14 32
814 18 return grep { -d $_ } map { File::Spec->catdir($_, $path) } @dirs;
815 } else {
816 14 return @dirs;
817 }
818 }
819  
820 70 ▶sub _locate_file_in_overlays {
Subroutine _locate_file_in_overlays: called CC=2 SCAR=6.9
821 70 my ( $self, $path ) = @_;
822  
823 70 ▶ if ( my $txn = $self->_txn_for_path($path) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $txn = $self->_txn_for_path($path)) { } 16 54 70
824 16 File::Spec->catfile($txn->work, $path);
825 } else {
826 #unless ( $self->_txn->find_lock($path) ) { # can't optimize this way if an explicit lock was taken
827 # we only take a read lock on the root dir if the state isn't dirty
828 54 my $ex_lock = $self->check_dirty;
829 52 $self->lock_path_read($path);
830 #}
831 52 File::Spec->catfile($self->_root, $path);
832 }
833 }
834  
835 2 ▶sub old_stat {
Subroutine old_stat: called CC=1 SCAR=0.0
Pod documented
836 2 my ( $self, $path ) = @_;
837  
838 2 my $t = $self->_auto_txn;
839  
840 2 CORE::stat($self->_locate_file_in_overlays($path));
841 }
842  
843 2 ▶sub stat {
Subroutine stat: called CC=1 SCAR=0.0
Pod documented
844 2 my ( $self, $path ) = @_;
845  
846 2 my $t = $self->_auto_txn;
847  
848 2 require File::stat;
849 2 File::stat::stat($self->_locate_file_in_overlays($path));
850 }
851  
852 24 ▶sub is_deleted {
Subroutine is_deleted: called CC=1 SCAR=0.0
Pod documented
853 24 my ( $self, $path ) = @_;
854  
855 24 not $self->exists($path);
856 }
857  
858 25 ▶sub exists {
Subroutine exists: called CC=1 SCAR=0.0
Pod documented
859 25 my ( $self, $path ) = @_;
860  
861 25 my $t = $self->_auto_txn;
862  
863 25 return -e $self->_locate_file_in_overlays($path);
864 }
865  
866 18 ▶sub is_dir {
Subroutine is_dir: called CC=3 SCAR=11.0
Pod documented
867 18 my ( $self, $path ) = @_;
868  
869 18 my $t = $self->_auto_txn;
870  
871 # FIXME this is an ugly kludge, we really need to keep better track of
872 # why/when directories are created, make note of them in 'is_changed', etc.
873  
874 18 my @dirs = ( (map { $_->work } $self->_txn_stack), $self->root );
875  
876 18 foreach my $dir ( @dirs ) {
877 36 ▶ return 1 if -d File::Spec->catdir($dir, $path);
Branch: 1 branch
BranchTrueFalseTotal
if -d "File::Spec"->catdir($dir, $path) 4 32 36
878 }
879  
880 14 return;
881 }
882  
883 0 ▶sub is_file {
Subroutine is_file: not called CC=1 SCAR=6.9
Pod documented
884 0 my ( $self, $path ) = @_;
885  
886 0 my $t = $self->_auto_txn;
887  
888 0 return -f $self->_locate_file_in_overlays($path);
889 }
890  
891 6 ▶sub unlink {
Subroutine unlink: called CC=3 SCAR=12.0
Pod documented
892 6 my ( $self, $path ) = @_;
893  
894 6 my $t = $self->_auto_txn;
895  
896 # lock parent for writing
897 6 my ( undef, $dir ) = File::Spec->splitpath($path);
898 6 $self->lock_path_write($dir);
899  
900 6 my $txn_file = $self->_work_path($path);
901  
902 6 ▶ if ( -e $txn_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $txn_file) { } 0 6 6
903 0 ▶ CORE::unlink $txn_file or die $!;
Branch: 1 branch
BranchTrueFalseTotal
CORE::unlink $txn_file or die $! 0 0 0
904 } else {
905 6 return 1;
906 }
907 }
908  
909 3 ▶sub rename {
Subroutine rename: called CC=3 SCAR=11.0
Pod documented
910 3 my ( $self, $from, $to ) = @_;
911  
912 3 my $t = $self->_auto_txn;
913  
914 3 foreach my $path ( $from, $to ) {
915 # lock parents for writing
916 6 my ( undef, $dir ) = File::Spec->splitpath($path);
917 6 $self->lock_path_write($dir);
918 }
919  
920 3 ▶ $self->vivify_path($from),
Branch: 1 branch
BranchTrueFalseTotal
$self->vivify_path($from), CORE::rename($self->_work_path($from), $self->_work_path($to)) or die $! 3 0 3
921  
922 CORE::rename (
923 $self->_work_path($from),
924 $self->_work_path($to),
925 ) or die $!;
926 }
927  
928 33 ▶sub openr {
Subroutine openr: called CC=3 SCAR=11.0
Pod documented
929 33 my ( $self, $file ) = @_;
930  
931 33 my $t = $self->_resource_auto_txn;
932  
933 32 my $src = $self->_locate_file_in_overlays($file);
934  
935 30 ▶ open my $fh, "<", $src or die "openr($file): $!";
Branch: 1 branch
BranchTrueFalseTotal
CORE::open my $fh, "<", $src or die "openr($file): $!" 30 0 30
936  
937 30 ▶ $t->register($fh) if $t;
Branch: 1 branch
BranchTrueFalseTotal
if $t 1 29 30
938  
939 30 return $fh;
940 }
941  
942 11 ▶sub openw {
Subroutine openw: called CC=5 SCAR=16.1
Pod documented
943 11 my ( $self, $path ) = @_;
944  
945 11 my $t = $self->_resource_auto_txn;
946  
947 10 my $txn = $self->_txn;
948  
949 10 my $file = File::Spec->catfile( $txn->work, $path );
950  
951 10 ▶ unless ( $txn->is_changed_in_head($path) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($txn->is_changed_in_head($path)) 8 2 10
952 8 my ( undef, $dir ) = File::Spec->splitpath($path);
953  
954 8 $self->lock_path_write($path);
955  
956 8 ▶ make_path( File::Spec->catdir($txn->work, $dir) ) if length($dir); # FIXME only if it exists in the original?
Branch: 1 branch
BranchTrueFalseTotal
if length $dir 4 4 8
957 }
958  
959 10 $txn->mark_changed($path);
960  
961 10 ▶ open my $fh, ">", $file or die "openw($path): $!";
Branch: 1 branch
BranchTrueFalseTotal
CORE::open my $fh, ">", $file or die "openw($path): $!" 10 0 10
962  
963 10 ▶ $t->register($fh) if $t;
Branch: 1 branch
BranchTrueFalseTotal
if $t 2 8 10
964  
965 10 return $fh;
966 }
967  
968 2 ▶sub opena {
Subroutine opena: called CC=3 SCAR=11.2
Pod documented
969 2 my ( $self, $file ) = @_;
970  
971 2 my $t = $self->_resource_auto_txn;
972  
973 2 $self->vivify_path($file);
974  
975 2 ▶ open my $fh, ">>", $self->_work_path($file) or die "opena($file): $!";
Branch: 1 branch
BranchTrueFalseTotal
CORE::open my $fh, ">>", $self->_work_path($file) or die "opena($file): $!" 2 0 2
976  
977 2 ▶ $t->register($fh) if $t;
Branch: 1 branch
BranchTrueFalseTotal
if $t 0 2 2
978  
979 2 return $fh;
980 }
981  
982 4 ▶sub open {
Subroutine open: called CC=3 SCAR=11.2
Pod documented
983 4 my ( $self, $mode, $file ) = @_;
984  
985 4 my $t = $self->_resource_auto_txn;
986  
987 4 $self->vivify_path($file);
988  
989 4 ▶ open my $fh, $mode, $self->_work_path($file) or die "open($mode, $file): $!";
Branch: 1 branch
BranchTrueFalseTotal
CORE::open my $fh, $mode, $self->_work_path($file) or die "open($mode, $file): $!" 4 0 4
990  
991 4 ▶ $t->register($fh) if $t;
Branch: 1 branch
BranchTrueFalseTotal
if $t 0 4 4
992  
993 4 return $fh;
994 }
995  
996 32 ▶sub _readdir_from_overlay {
Subroutine _readdir_from_overlay: called CC=4 SCAR=13.9
997 32 my ( $self, $path ) = @_;
998  
999 32 my $t = $self->_auto_txn;
1000  
1001 32 my $ex_lock = $self->check_dirty;
1002  
1003 32 my @dirs = $self->_locate_dirs_in_overlays($path);
1004  
1005 32 my $files = Set::Object->new;
1006  
1007 # compute union of all directories
1008 32 foreach my $dir ( @dirs ) {
1009 52 $files->insert( IO::Dir->new($dir)->read );
1010 }
1011  
1012 32 ▶ unless ( defined $path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $path) 14 18 32
1013 14 $files->remove(".txn_work_dir");
1014 14 $files->remove(".txn_work_dir.lock");
1015 14 ▶ $files->remove(".txn_work_dir.lock.NFSLock") if $self->nfs;
Branch: 1 branch
BranchTrueFalseTotal
if $self->nfs 4 10 14
1016 }
1017  
1018 32 return $files;
1019 }
1020  
1021 2 ▶sub readdir {
Subroutine readdir: called CC=10 SCAR=26.2
Pod documented
1022 2 my ( $self, $path ) = @_;
1023  
1024 2 ▶ undef $path if $path eq "/" or !length($path);
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "/" or not length $path 2 0 2
Condition33%1/3
$path eq "/" or not length $path
l!l&&r!l&&!r
200
MC/DC0%0/2
$path eq "/" or not length $path
$path eq "/" not length $path
Truth table33%1/3
A or not(B)
A= $path eq "/"B= not length $path
ABresult
1X1
011
000
1025  
1026 2 my $t = $self->_auto_txn;
1027  
1028 2 my $files = $self->_readdir_from_overlay($path);
1029  
1030 2 my @txns = $self->_txn_stack;
1031  
1032 # remove deleted files
1033 2 file: foreach my $file ( $files->members ) {
1034 10 ▶ next if $file eq '.' or $file eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $file eq "." or $file eq ".." 4 6 10
Condition100%3/3
$file eq "." or $file eq ".."
l!l&&r!l&&!r
226
MC/DC100%2/2
$file eq "." or $file eq ".."
$file eq "." $file eq ".."
Truth table100%3/3
A or B
A= $file eq "."B= $file eq ".."
ABresult
1X1
011
000
1035  
1036 6 ▶ my $file_path = $path ? File::Spec->catfile($path, $file) : $file;
Branch: 1 branch
BranchTrueFalseTotal
$path ? : 0 6 6
1037  
1038 6 foreach my $txn ( @txns ) {
1039 6 ▶ if ( $txn->is_changed_in_head($file_path) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($txn->is_changed_in_head($file_path)) 0 6 6
1040 0 ▶ if ( not( -e File::Spec->catfile( $txn->work, $file_path ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e "File::Spec"->catfile($txn->work, $file_path)) 0 0 0
1041 0 $files->remove($file);
1042 }
1043 0 next file;
1044 }
1045 }
1046 }
1047  
1048 2 return $files->members;
1049 }
1050  
1051 30 ▶sub list {
Subroutine list: called CC=8 SCAR=20.8
Pod documented
1052 30 my ( $self, $path ) = @_;
1053  
1054 30 ▶ undef $path if $path eq "/" or !length($path);
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "/" or not length $path 12 18 30
Condition100%3/3
$path eq "/" or not length $path
l!l&&r!l&&!r
8418
MC/DC100%2/2
$path eq "/" or not length $path
$path eq "/" not length $path
Truth table100%3/3
A or not(B)
A= $path eq "/"B= not length $path
ABresult
1X1
011
000
1055  
1056 30 my $t = $self->_auto_txn;
1057  
1058 30 my $files = $self->_readdir_from_overlay($path);
1059  
1060 30 $files->remove('.', '..');
1061  
1062 30 my @txns = $self->_txn_stack;
1063  
1064 30 my @ret;
1065  
1066 # remove deleted files
1067 30 file: foreach my $file ( $files->members ) {
1068 36 ▶ my $file_path = $path ? File::Spec->catfile($path, $file) : $file;
Branch: 1 branch
BranchTrueFalseTotal
$path ? : 16 20 36
1069  
1070 36 foreach my $txn ( @txns ) {
1071 42 ▶ if ( $txn->is_changed_in_head($file_path) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($txn->is_changed_in_head($file_path)) 8 34 42
1072 8 ▶ if ( -e File::Spec->catfile( $txn->work, $file_path ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "File::Spec"->catfile($txn->work, $file_path)) 4 4 8
1073 4 push @ret, $file_path;
1074 }
1075 8 next file;
1076 }
1077 }
1078  
1079 28 push @ret, $file_path;
1080 }
1081  
1082 30 return sort @ret;
1083 }
1084  
1085 31 ▶sub _work_path {
Subroutine _work_path: called CC=2 SCAR=6.9
1086 31 my ( $self, $path ) = @_;
1087  
1088 31 $self->lock_path_write($path);
1089  
1090 31 my $txn = $self->_txn;
1091  
1092 31 $txn->mark_changed($path);
1093  
1094 31 my $file = File::Spec->catfile( $txn->work, $path );
1095  
1096 31 my ( undef, $dir ) = File::Spec->splitpath($path);
1097 31 ▶ make_path( File::Spec->catdir($txn->work, $dir ) ) if length($dir); # FIXME only if it exists in the original?
Branch: 1 branch
BranchTrueFalseTotal
if length $dir 20 11 31
1098  
1099 31 return $file;
1100 }
1101  
1102 9 ▶sub vivify_path {
Subroutine vivify_path: called CC=6 SCAR=18.8
Pod documented
1103 9 my ( $self, $path ) = @_;
1104  
1105 9 my $txn = $self->_txn;
1106  
1107 9 my $txn_path = File::Spec->catfile( $txn->work, $path );
1108  
1109 9 ▶ unless ( $txn->is_changed_in_head($path) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($txn->is_changed_in_head($path)) 9 0 9
1110 9 $self->lock_path_write($path);
1111  
1112 9 my $src = $self->_locate_file_in_overlays($path);
1113  
1114 9 ▶ if ( my $stat = File::stat::stat($src) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $stat = &File::stat::stat($src)) 7 2 9
1115 7 ▶ if ( $stat->nlink > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stat->nlink > 1) 0 7 7
1116 0 croak "the file $src has a link count of more than one.";
1117 }
1118  
1119 7 ▶ if ( -l $src ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-l $src) 0 7 7
1120 0 croak "The file $src is a symbolic link.";
1121 }
1122  
1123 7 $self->_work_path($path); # FIXME vivifies parent dir
1124 7 ▶ copy( $src, $txn_path ) or die "copy($src, $txn_path): $!";
Branch: 1 branch
BranchTrueFalseTotal
copy($src, $txn_path) or die "copy($src, $txn_path): $!" 7 0 7
1125 }
1126 }
1127  
1128 9 return $txn_path;
1129 }
1130  
1131  
1132  
1133 6 ▶sub file_stream {
Subroutine file_stream: called CC=2 SCAR=7.0
Pod documented
1134 6 my ( $self, @args ) = @_;
1135  
1136 6 my $t = $self->_resource_auto_txn;
1137  
1138 6 require Directory::Transactional::Stream;
1139  
1140 6 my $stream = Directory::Transactional::Stream->new(
1141 manager => $self,
1142 @args,
1143 );
1144  
1145 6 ▶ $t->register($stream) if $t;
Branch: 1 branch
BranchTrueFalseTotal
if $t 6 0 6
1146  
1147 6 return $stream;
1148 }
1149  
1150 8 __PACKAGE__->meta->make_immutable;
1151  
1152 8 __PACKAGE__
1153  
1154 __END__
« Transactional/Stream.pm ↑ Summary Transactional/TXN/Nested.pm »