AtomicFile.pm untested

filter: Statementstmt 0.0% 0 / 47 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 92 CC 10 SCAR 24.4
CC
10
Cov
0%
close
30.0
new
6.0
open
6.0
CRAP 11.5
1 package IO::AtomicFile;
2  
3 use strict;
4 use warnings;
5 use parent 'IO::File';
6  
7 our $VERSION = '2.113';
8  
9 #------------------------------
10 # new ARGS...
11 #------------------------------
12 # Class method, constructor.
13 # Any arguments are sent to open().
14 #
15 sub new {
16 my $class = shift;
17 my $self = $class->SUPER::new();
18 ${*$self}{'io_atomicfile_suffix'} = '';
19 $self->open(@_) if @_;
20 $self;
21 }
22  
23 #------------------------------
24 # DESTROY
25 #------------------------------
26 # Destructor.
27 #
28 sub DESTROY {
29 shift->close(1); ### like close, but raises fatal exception on failure
30 }
31  
32 #------------------------------
33 # open PATH, MODE
34 #------------------------------
35 # Class/instance method.
36 #
37 sub open {
38 my ($self, $path, $mode) = @_;
39 ref($self) or $self = $self->new; ### now we have an instance!
40  
41 ### Create tmp path, and remember this info:
42 my $temp = "${path}..TMP" . ${*$self}{'io_atomicfile_suffix'};
43 ${*$self}{'io_atomicfile_temp'} = $temp;
44 ${*$self}{'io_atomicfile_path'} = $path;
45  
46 ### Open the file! Returns filehandle on success, for use as a constructor:
47 $self->SUPER::open($temp, $mode) ? $self : undef;
48 }
49  
50 #------------------------------
51 # _closed [YESNO]
52 #------------------------------
53 # Instance method, private.
54 # Are we already closed? Argument sets new value, returns previous one.
55 #
56 sub _closed {
57 my $self = shift;
58 my $oldval = ${*$self}{'io_atomicfile_closed'};
59 ${*$self}{'io_atomicfile_closed'} = shift if @_;
60 $oldval;
61 }
62  
63 #------------------------------
64 # close
65 #------------------------------
66 # Instance method.
67 # Close the handle, and rename the temp file to its final name.
68 #
69 sub close {
70 my ($self, $die) = @_;
71 unless ($self->_closed(1)) { ### sentinel...
72 if ($self->SUPER::close()) {
73 rename(${*$self}{'io_atomicfile_temp'},
74 ${*$self}{'io_atomicfile_path'})
75 or ($die ? die "close (rename) atomic file: $!\n" : return undef);
76 } else {
77 ($die ? die "close atomic file: $!\n" : return undef);
78 }
79 }
80 1;
81 }
82  
83 #------------------------------
84 # delete
85 #------------------------------
86 # Instance method.
87 # Close the handle, and delete the temp file.
88 #
89 sub delete {
90 my $self = shift;
91 unless ($self->_closed(1)) { ### sentinel...
92 $self->SUPER::close();
93 return unlink(${*$self}{'io_atomicfile_temp'});
94 }
95 1;
96 }
97  
98 #------------------------------
99 # detach
100 #------------------------------
101 # Instance method.
102 # Close the handle, but DO NOT delete the temp file.
103 #
104 sub detach {
105 my $self = shift;
106 $self->SUPER::close() unless ($self->_closed(1));
107 1;
108 }
109  
110 #------------------------------
111 1;
112 __END__