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 $self->SUPER::close();
90 rename(${*$self}{'io_atomicfile_temp'},
91 ${*$self}{'io_atomicfile_path'})
92 or ($die ? die "close atomic file: $!\n" : return undef);
97 #------------------------------
99 #------------------------------
101 # Close the handle, and delete the temp file.
105 unless ($self->_closed(1)) { ### sentinel...
106 $self->SUPER::close();
107 return unlink(${*$self}{'io_atomicfile_temp'});
112 #------------------------------
114 #------------------------------
116 # Close the handle, but DO NOT delete the temp file.
120 $self->SUPER::close() unless ($self->_closed(1));
124 #------------------------------
131 IO::AtomicFile - write a file which is updated atomically
138 ### Write a temp file, and have it install itself when closed:
139 my $FH = IO::AtomicFile->open("bar.dat", "w");
140 print $FH "Hello!\n";
141 $FH->close || die "couldn't install atomic file: $!";
143 ### Write a temp file, but delete it before it gets installed:
144 my $FH = IO::AtomicFile->open("bar.dat", "w");
145 print $FH "Hello!\n";
148 ### Write a temp file, but neither install it nor delete it:
149 my $FH = IO::AtomicFile->open("bar.dat", "w");
150 print $FH "Hello!\n";
156 This module is intended for people who need to update files
157 reliably in the face of unexpected program termination.
159 For example, you generally don't want to be halfway in the middle of
160 writing I</etc/passwd> and have your program terminate! Even
161 the act of writing a single scalar to a filehandle is I<not> atomic.
163 But this module gives you true atomic updates, via rename().
164 When you open a file I</foo/bar.dat> via this module, you are I<actually>
165 opening a temporary file I</foo/bar.dat..TMP>, and writing your
166 output there. The act of closing this file (either explicitly
167 via close(), or implicitly via the destruction of the object)
168 will cause rename() to be called... therefore, from the point
169 of view of the outside world, the file's contents are updated
170 in a single time quantum.
172 To ensure that problems do not go undetected, the "close" method
173 done by the destructor will raise a fatal exception if the rename()
174 fails. The explicit close() just returns undef.
176 You can also decide at any point to trash the file you've been
182 =head2 Primary Maintainer
184 David F. Skoll (F<dfs@roaringpenguin.com>).
186 =head2 Original Author
188 Eryq (F<eryq@zeegee.com>).
189 President, ZeeGee Software Inc (F<http://www.zeegee.com>).