1 package IO::AtomicFile;
3 ### DOCUMENTATION AT BOTTOM OF FILE
12 #------------------------------
16 #------------------------------
17 use vars qw($VERSION @ISA);
19 # The package version, both in 1.23 style *and* usable by MakeMaker:
26 #------------------------------
28 #------------------------------
29 # Class method, constructor.
30 # Any arguments are sent to open().
34 my $self = $class->SUPER::new();
35 ${*$self}{'io_atomicfile_suffix'} = '';
36 $self->open(@_) if @_;
40 #------------------------------
42 #------------------------------
46 shift->close(1); ### like close, but raises fatal exception on failure
49 #------------------------------
51 #------------------------------
52 # Class/instance method.
55 my ($self, $path, $mode) = @_;
56 ref($self) or $self = $self->new; ### now we have an instance!
58 ### Create tmp path, and remember this info:
59 my $temp = "${path}..TMP" . ${*$self}{'io_atomicfile_suffix'};
60 ${*$self}{'io_atomicfile_temp'} = $temp;
61 ${*$self}{'io_atomicfile_path'} = $path;
63 ### Open the file! Returns filehandle on success, for use as a constructor:
64 $self->SUPER::open($temp, $mode) ? $self : undef;
67 #------------------------------
69 #------------------------------
70 # Instance method, private.
71 # Are we already closed? Argument sets new value, returns previous one.
75 my $oldval = ${*$self}{'io_atomicfile_closed'};
76 ${*$self}{'io_atomicfile_closed'} = shift if @_;
80 #------------------------------
82 #------------------------------
84 # Close the handle, and rename the temp file to its final name.
87 my ($self, $die) = @_;
88 unless ($self->_closed(1)) { ### sentinel...
89 if ($self->SUPER::close()) {
90 rename(${*$self}{'io_atomicfile_temp'},
91 ${*$self}{'io_atomicfile_path'})
92 or ($die ? die "close (rename) atomic file: $!\n" : return undef);
94 $die ? die "close atomic file: $!\n" : return undef;
100 #------------------------------
102 #------------------------------
104 # Close the handle, and delete the temp file.
108 unless ($self->_closed(1)) { ### sentinel...
109 $self->SUPER::close();
110 return unlink(${*$self}{'io_atomicfile_temp'});
115 #------------------------------
117 #------------------------------
119 # Close the handle, but DO NOT delete the temp file.
123 $self->SUPER::close() unless ($self->_closed(1));
127 #------------------------------
134 IO::AtomicFile - write a file which is updated atomically
141 ### Write a temp file, and have it install itself when closed:
142 my $FH = IO::AtomicFile->open("bar.dat", "w");
143 print $FH "Hello!\n";
144 $FH->close || die "couldn't install atomic file: $!";
146 ### Write a temp file, but delete it before it gets installed:
147 my $FH = IO::AtomicFile->open("bar.dat", "w");
148 print $FH "Hello!\n";
151 ### Write a temp file, but neither install it nor delete it:
152 my $FH = IO::AtomicFile->open("bar.dat", "w");
153 print $FH "Hello!\n";
159 This module is intended for people who need to update files
160 reliably in the face of unexpected program termination.
162 For example, you generally don't want to be halfway in the middle of
163 writing I</etc/passwd> and have your program terminate! Even
164 the act of writing a single scalar to a filehandle is I<not> atomic.
166 But this module gives you true atomic updates, via rename().
167 When you open a file I</foo/bar.dat> via this module, you are I<actually>
168 opening a temporary file I</foo/bar.dat..TMP>, and writing your
169 output there. The act of closing this file (either explicitly
170 via close(), or implicitly via the destruction of the object)
171 will cause rename() to be called... therefore, from the point
172 of view of the outside world, the file's contents are updated
173 in a single time quantum.
175 To ensure that problems do not go undetected, the "close" method
176 done by the destructor will raise a fatal exception if the rename()
177 fails. The explicit close() just returns undef.
179 You can also decide at any point to trash the file you've been
185 =head2 Primary Maintainer
187 David F. Skoll (F<dfs@roaringpenguin.com>).
189 =head2 Original Author
191 Eryq (F<eryq@zeegee.com>).
192 President, ZeeGee Software Inc (F<http://www.zeegee.com>).