1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
#-*- perl -*-
#
# Copyright (C) 2001 Ken'ichi Fukamachi
# All rights reserved. This program is free software; you can
# redistribute it and/or modify it under the same terms as Perl itself.
#
# $Id$
# $FML$
#
package IO::File::Atomic;
use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK);
use Carp;
use IO::File;
require Exporter;
@ISA = qw(IO::File);
BEGIN {}
END {}
=head1 NAME
IO::Atomic - atomic operation
=head1 SYNOPSIS
use IO::Atomic;
my $wh = IO::Atomic->open($file);
print $wh "new/updated things ...";
$wh->close;
So, in usual cases, you use in this way.
use FileHandle;
use IO::Atomic;
# get read handle for $file
my $rh = new FileHandle $file;
# get handle to update $file
my $wh = IO::Atomic->open($file);
while (<$rh>) {
print $wh "new/updated things ...";
}
$wh->close;
$rh->close;
You can use this method to open $file for both read and write.
use IO::Atomic;
my ($rh, $wh) = IO::Atomic->rw_open($file);
while (<$rh>) {
print $wh "new/updated things ...";
}
$wh->close;
$rh->close;
To copy from $src to $dst,
IO::File::Atomic->copy($src, $dst) || croak("fail to copy");
=head1 DESCRIPTION
=cut
# Descriptions: constructor
# forward new() request to superclass (IO::File)
# Arguments: $class_name
# Side Effects: none
# Return Value: class object
# XXX $self is blessed file handle.
sub new
{
my ($self) = shift;
my $me = $self->SUPER::new();
$me->open(@_) if @_;
$me;
}
# Descriptions: open( $file [, $mode] )
# open not $file but file.new.$$
# forward open() request to IO::File class
# Arguments: $self $file [$mode]
# XXX $self is blessed file handle.
# Side Effects: create ${ *$self } hash to save status information
# Return Value: write file handle (for $file.new.$$)
sub open
{
my ($self, $file, $mode) = @_;
# get an instance
ref($self) or $self = $self->new;
# default mode is "w"
$mode ||= "w";
# temporary file
my $temp = $file.".new.".$$;
${*$self}{ _orig_file } = $file;
${*$self}{ _temp_file } = $temp;
# real open with $mode
$self->autoflush;
$self->SUPER::open($temp, "w") ? $self : undef;
}
# Descriptions: open $file with the mode $mode for both
# reading and writing.
# Arguments: $class_name $file [$mode]
# Side Effects: none
# Return Value: LIST of file handle (read, write)
sub rw_open
{
my ($self, $file, $mode) = @_;
use FileHandle;
my $rh = new FileHandle $file;
my $wh = $self->open($file, $mode);
return ($rh, $wh);
}
# Descriptions: close "write" file handle
# XXX "read" file handle is closed by SUPERCLASS.
# Arguments: $self
# XXX $self is blessed file handle.
# Side Effects: rename the temporary file to the original file
# save the error message in ${ *$fh }
# Return Value: 1 if succeeded, 0 if failed
sub close
{
my ($self) = @_;
my $fh = $self;
my $orig = ${ *$fh }{ _orig_file };
my $temp = ${ *$fh }{ _temp_file };
# XXX close the "write" file handle (write .. to $temp file)
close($fh);
if (rename($temp, $orig)) {
return 1;
}
else {
${ *$fh }{ _error } = "fail to rename($temp, $orig)";
return 0;
}
}
# Descriptions: copy file, which ensures atomic operation
# Arguments: $self source_file destination_file
# Side Effects: $dst's file mode becomes the same as $src
# Return Value: 1 if succeeded, undef if not
sub copy
{
my ($self, $src, $dst) = @_;
my ($mode) = (stat($src))[2];
my $rh = new FileHandle $src;
my $wh = $self->open($dst);
if (defined($rh) && defined($wh)) {
while (sysread($rh, $_, 4096)) { print $wh $_;}
$wh->close;
$rh->close;
chmod $mode, $dst;
}
else {
return undef;
}
}
# Descriptions: return error message
# Arguments: $self
# XXX $self is blessed file handle.
# Side Effects: none
# Return Value: error message string
sub error
{
my ($self) = @_;
my $fh = $self;
${ *$fh }{ _error };
}
# Descriptions: reset the previous work
# Arguments: $self
# XXX $self is blessed file handle.
# Side Effects: clean up the previous work ;-)
# remove temporary files we created
# Return Value: none
sub rollback
{
my ($self) = @_;
my $fh = $self;
my $temp = ${ *$fh }{ _temp_file };
if (-f $temp) { unlink $temp;}
}
# Descriptions: destructor
# forward the request to rollback() in this class
# Arguments: $self
# XXX $self is blessed file handle.
# Side Effects: none
# Return Value: the same as rollback()
sub DESTROY
{
my ($self) = @_;
$self->rollback;
}
=head1 AUTHOR
Ken'ichi Fukamachi
=head1 COPYRIGHT
Copyright (C) 2001 Ken'ichi Fukamachi
All rights reserved. This program is free software; you can
redistribute it and/or modify it under the same terms as Perl itself.
=head1 HISTORY
IO::File::Atomic.pm appeared in fml5.
=cut
1;
|