blib/lib/File/Append/TempFile.pm

filter: Statementstmt 80.0% 84 / 105 Branchbran 50.0% 20 / 40 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 93.3% 14 / 15 Podpod 100% 9 / 9 totaltotal 70.6% 130 / 184 CC 25 SCAR 16.8
CC
25
Cov
69%
commit
11.2
begin_work
7.8
err
6.0
CRAP 5.4
↑ Summary
1 package File::Append::TempFile;
2  
3 2 ▶use v5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 File::Append::TempFile - Perl extension for appending data to files
10  
11 =head1 SYNOPSIS
12  
13 use File::Append::TempFile;
14  
15 $f = new File::Append::TempFile();
16 $f->begin_work('/etc/hosts') or die "Appending: ".$f->err();
17 $f->add_line("127.0.0.2 localhvost\n");
18 $f->commit();
19  
20 $f->begin_work('/etc/hosts') or die "Appending: ".$f->err();
21 $f->add_line("...\n");
22 $f->rollback();
23  
24 =head1 DESCRIPTION
25  
26 The C<File::Append::TempFile> module provides an OOP interface to appending
27 data to files using a temporary file, in order to ensure the atomicity of
28 the updates.
29  
30 An append session is initiated by invoking the C<begin_work()> method and
31 passing it the name of the file. At this point, a temporary file is
32 created in the same directory as the original file and the original's
33 contents is copied to the temporary. More data is added to the temporary
34 file using the C<add_line()> method. When done appending, the C<commit()>
35 method will atomically move the temporary file over the original.
36 If something goes wrong, the C<rollback()> method will remove the temporary
37 file without affecting the original in any way.
38  
39 =cut
40  
41 2 ▶use File::Basename qw(basename dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 2 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 2 our @ISA = qw();
45  
46 2 our $VERSION = '0.07';
47  
48 2 our $debug = 0;
49  
50 2 my %tempfiles;
51  
52 =head1 METHODS
53  
54 The C<File::Append::TempFile> class defines the following methods:
55  
56 =over 4
57  
58 =item new ()
59  
60 Create a new C<File::Append::TempFile> object. No file processing is
61 done at this point.
62  
63 =cut
64  
65 sub new
66 1 ▶{
Subroutine new: called CC=2 SCAR=7.1
Pod documented
67 1 my $proto = shift;
68 1 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
69 1 my $self;
70  
71 1 $self = {
72 fname => undef,
73 f => undef,
74 err => undef,
75 debug => undef
76 };
77 1 bless $self, $class;
78 1 $tempfiles{$self} = $self;
79 1 return $self;
80 }
81  
82 =item err ( [MESSAGE] )
83  
84 Set or obtain an error message describing the last error that occurred
85 during the processing of the current C<File::Append::TempFile> object.
86  
87 =cut
88  
89 sub err($ $)
90 0 ▶{
Subroutine err: not called CC=2 SCAR=17.9
Pod documented
91 0 my ($self, $err) = @_;
92  
93 0 ▶ $self->{err} = $err if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
94 0 return $self->{err};
95 }
96  
97 =item diag ([FLAG])
98  
99 Set or obtain the diagnostic output flag. If it is set, the methods
100 will display diagnostic information on the standard error stream.
101  
102 =cut
103  
104 sub diag($ $)
105 3 ▶{
Subroutine diag: called CC=2 SCAR=6.9
Pod documented
106 3 my ($self, $debug) = @_;
107  
108 3 ▶ $self->{debug} = $debug if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 1 2 3
109 3 return $self->{debug};
110 }
111  
112 =item begin_work (FILENAME)
113  
114 Creates a temporary file in the same directory as the specified one and
115 copies the original's contents over to the new file. Further data may
116 be added using the C<add_line()> method and then either stored as the
117 original with the C<commit()> method, or discarded with the C<rollback()>
118 method.
119  
120 =cut
121  
122 sub begin_work($ $)
123 3 ▶{
Subroutine begin_work: called CC=6 SCAR=20.5
Pod documented
124 3 my ($self, $fname) = @_;
125 3 my ($orig, $f);
126 3 my @stat;
127  
128 3 ▶ if ($self->{f}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"f"}) 0 3 3
129 0 ▶ return undef unless $self->rollback();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->rollback 0 0 0
130 }
131 3 $self->{fname} = $self->{f} = undef;
132  
133 3 ▶ if (!open $orig, '<', $fname) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $orig, "<", $fname) 0 3 3
134 0 $self->err("Opening $fname: $!");
135 0 return undef;
136 }
137 3 @stat = stat $orig;
138 3 $f = File::Temp->new(basename($fname).'.XXXXXX',
139 DIR => dirname($fname));
140 3 ▶ if (!defined $f) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $f) 0 3 3
141 0 $self->err("Creating a temporary file for $fname: $!");
142 0 return undef;
143 }
144 3 ▶ return undef unless $self->do_copy($orig, $f);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->do_copy($orig, $f) 0 3 3
145 3 close $orig;
146  
147 3 $self->{fname} = $fname;
148 3 $self->{f} = $f;
149 3 $self->{stat} = [ @stat ];
150 3 return 1;
151 }
152  
153 =item add_line (DATA)
154  
155 Append data to the temporary file. This does not affect the original in
156 any way until C<commit()> is invoked.
157  
158 =cut
159  
160 sub add_line($ $)
161 4 ▶{
Subroutine add_line: called CC=3 SCAR=13.1
Pod documented
162 4 my ($self, $line) = @_;
163 4 my $f = $self->{f};
164  
165 4 ▶ if (!defined $f) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $f) 0 4 4
166 0 $self->err("Cannot add_line() to an unopened tempfile");
167 0 return undef;
168 }
169 4 $self->debug("RDBG about to add a line to $f for $self->{fname}\n");
170 4 ▶ if (!(print $f $line)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (print $f $line) 0 4 4
171 0 $self->err("Could not add to the tempfile: $!");
172 0 return undef;
173 }
174 4 return 1;
175 }
176  
177 =item commit ()
178  
179 Replace the original file with the temporary copy, to which data may have
180 been added using C<add_line()>.
181  
182 B<NOTE:> This method uninitializes the C<File::Append::TempFile> object,
183 that is, removes B<any> association between it and the original file and
184 even file name! The next method invoked on this C<File::Append::TempFile>
185 object should be C<begin_work()>.
186  
187 =cut
188  
189 sub commit($)
190 1 ▶{
Subroutine commit: called CC=7 SCAR=24.2
Pod documented
191 1 my ($self) = @_;
192 1 my $f = $self->{f};
193  
194 1 ▶ if (!defined $f || !defined $self->{fname}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $f and defined $self->{"fname"}) 0 1 1
Condition33%1/3
defined $f and defined $self->{"fname"}
!ll&&!rl&&r
001
MC/DC0%0/2
defined $f and defined $self->{"fname"}
defined $f defined $self->{"fname"}
Truth table33%1/3
A and B
A= defined $fB= defined $self->{"fname"}
ABresult
0X0
100
111
195 0 $self->err("Cannot commit an unopened tempfile");
196 0 return undef;
197 }
198 1 $self->debug("RDBG about to commit $f to $self->{fname}\n");
199  
200 # Fix stuff up
201 1 ▶ if (defined $self->{stat}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"stat"}) 1 0 1
202 # Mode
203 1 ▶ if (!chmod $self->{stat}->[2], $f) {
Branch: 1 branch
BranchTrueFalseTotal
unless (chmod $self->{"stat"}[2], $f) 0 1 1
204 0 $self->err("Could not chmod $self->{stat}->[2] ".
205 "$f: $!");
206 0 return undef;
207 }
208 # Owner & group
209 1 ▶ if (!chown $self->{stat}->[4], $self->{stat}->[5], $f) {
Branch: 1 branch
BranchTrueFalseTotal
unless (chown $self->{"stat"}[4], $self->{"stat"}[5], $f) 0 1 1
210 0 $self->err("Could not chown $self->{stat}->[4], ".
211 "$self->{stat}->[5], $f: $!");
212 0 return undef;
213 }
214 }
215
216 1 ▶ if (!rename $f, $self->{fname}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (rename $f, $self->{"fname"}) 0 1 1
217 0 $self->err("Renaming $f to $self->{fname}: $!");
218 0 return undef;
219 }
220 1 $f->unlink_on_destroy(0);
221 1 close $f;
222 1 $self->debug("RDBG successfully committed $f to $self->{fname}\n");
223 1 $self->{fname} = $self->{f} = undef;
224 1 return 1;
225 }
226  
227 =item rollback ()
228  
229 Discard all the changes made to the temporary copy and remove it. This
230 does not affect the original file in any way.
231  
232 B<NOTE:> This method uninitializes the C<File::Append::TempFile> object,
233 that is, removes B<any> association between it and the original file and
234 even file name! The next method invoked on this C<File::Append::TempFile>
235 object should be C<begin_work()>.
236  
237 =cut
238  
239 sub rollback($)
240 2 ▶{
Subroutine rollback: called CC=2 SCAR=6.9
Pod documented
241 2 my ($self) = @_;
242  
243 2 $self->debug(ref($self)."->rollback() for $self->{fname}\n");
244 2 ▶ if (defined $self->{f}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"f"}) 2 0 2
245 2 my $f = $self->{f};
246 2 $self->debug("RDBG closing and removing $f\n");
247 2 $f->unlink_on_destroy(1);
248 2 close $f;
249 2 undef $self->{f};
250 }
251 2 undef $self->{fname};
252 2 $self->debug("RDBG rollback seems complete\n");
253 2 return 1;
254 }
255  
256 =back
257  
258 There are also several methods used internally by the
259 C<File::Append::TempFile> routines:
260  
261 =over 4
262  
263 =item debug (MESSAGE)
264  
265 Display a diagnostic message to the standard error stream if the output
266 of diagnostic messages has been enabled.
267  
268 =cut
269  
270 sub debug($ $)
271 12 ▶{
Subroutine debug: called CC=3 SCAR=14.2
Pod documented
272 12 my ($self, $msg) = @_;
273  
274 12 ▶ if ($self->{debug} || $debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"} or $File::Append::TempFile::debug) 0 12 12
Condition33%1/3
$self->{"debug"} or $File::Append::TempFile::debug
l!l&&r!l&&!r
0012
MC/DC0%0/2
$self->{"debug"} or $File::Append::TempFile::debug
$self->{"debug"} $File::Append::TempFile::debug
Truth table33%1/3
A or B
A= $self->{"debug"}B= $File::Append::TempFile::debug
ABresult
1X1
011
000
275 0 print STDERR $msg;
276 }
277 }
278  
279 =item do_copy (ORIG TEMP)
280  
281 Actually perform the copying of the original file data into the temporary
282 file at C<begin_work()> time. This allows derived classes to modify
283 the file structure if needed.
284  
285 The two parameters are the file handles for the original and the
286 temporary file.
287  
288 =cut
289  
290 sub do_copy($ $ $)
291 3 ▶{
Subroutine do_copy: called CC=2 SCAR=6.9
Pod documented
292 3 my ($self, $orig, $f) = @_;
293
294 3 ▶ while (<$orig>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $orig)) 8 3 11
295 8 print $f $_;
296 }
297 3 return 1;
298 }
299  
300 END
301 1 ▶{
Subroutine END: called CC=5 SCAR=17.4
302 1 ▶ print STDERR "RDBG File::Append::TempFile END block\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Append::TempFile::debug 0 1 1
303 1 ▶ print STDERR "RDBG ".keys(%tempfiles)."\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Append::TempFile::debug 0 1 1
304 1 foreach (keys %tempfiles) {
305 1 ▶ $tempfiles{$_}->rollback() if $tempfiles{$_}->{f};
Branch: 1 branch
BranchTrueFalseTotal
if $tempfiles{$_}{"f"} 1 0 1
306 }
307 }
308  
309 =back
310  
311 =head1 SEE ALSO
312  
313 The C<File::Append::TempFile> website:
314  
315 http://devel.ringlet.net/sysutils/file-append-tempfile/
316  
317 =head1 BUGS
318  
319 =over 4
320  
321 =item * Note that the original file may have changed between C<begin_work()>
322 and C<commit()> - those changes B<will> be lost!
323  
324 =back
325  
326 =head1 AUTHOR
327  
328 Peter Pentchev, E<lt>roam@ringlet.netE<gt>
329  
330 =head1 COPYRIGHT AND LICENSE
331  
332 Copyright (C) 2006, 2015 Peter Pentchev.
333  
334 This library is free software; you can redistribute it and/or modify
335 it under the same terms as Perl itself, either Perl version 5.8.7 or,
336 at your option, any later version of Perl 5 you may have available.
337  
338 =cut
339  
340 2 1;
↑ Summary