summaryrefslogtreecommitdiff
path: root/fml/lib/IO/File/Atomic.pm
blob: 0efc717e81031d228b0f16e2fc16befa1efce10e (plain)
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
#-*- 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;

require Exporter;
@ISA = qw(IO::File);


sub BEGIN {}


sub new
{
    my ($class) = shift;
    my $self = $class->SUPER::new();
    $self->open(@_) if @_;
    $self;
}


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;
    ${*$self}{ _temp } = $temp;

    # real open with $mode
    $self->autoflush;
    $self->SUPER::open($temp, "w") ? $self : undef;
}


sub rw_open
{
    my ($self, $file, $mode) = @_;

    use FileHandle;
    my $rh = new FileHandle $file;
    my $wh = $self->open($file, $mode);

    return ($rh, $wh);
}


sub close
{
    my ($self) = @_;
    my $fh = $self;
    my $orig = ${ *$fh }{ _orig };
    my $temp = ${ *$fh }{ _temp };

    if (rename($temp, $orig)) {
       ${ *$fh }{ _error } = "fail to rename($temp, $orig)";
    }
    else {
       undef;
    }
}


sub error
{
    my ($self) = @_;
    my $fh = $self;
    ${ *$fh }{ _error };
}


sub rollback
{
    my ($self) = @_;
    my $fh = $self;
    my $temp = ${ *$fh }{ _temp };
    if (-f $temp) { unlink $temp;}
}


sub DESTROY 
{ 
    my ($self) = @_;
    $self->rollback;
}


=head1 NAME

IO::Atomic.pm - atomic  operation


=head1 SYNOPSIS

    use IO::Atomic;
    my $wh = new 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 = new 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;


=head1 DESCRIPTION

=head2 new

=item Function()


=head1 AUTHOR

=head1 COPYRIGHT

Copyright (C) 2001 __YOUR_NAME__

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::__MODULE_NAME__.pm appeared in fml5.

=cut


1;