diff options
| author | fukachan <fukachan> | 2001-02-01 12:40:37 +0000 |
|---|---|---|
| committer | fukachan <fukachan> | 2001-02-01 12:40:37 +0000 |
| commit | 5c2ac1982cba4bee94de17d63c9399e99143a2bc (patch) | |
| tree | d07c957bfa4e86bef7d0718df2c9e611f1d22cd9 /fml/lib/IO | |
| parent | cd00a759db54b036d316c6c8acf535fc2beab591 (diff) | |
| download | fml8-5c2ac1982cba4bee94de17d63c9399e99143a2bc.tar.gz fml8-5c2ac1982cba4bee94de17d63c9399e99143a2bc.tar.bz2 fml8-5c2ac1982cba4bee94de17d63c9399e99143a2bc.zip | |
IO::File::Atomic::copy(src, dst)
Diffstat (limited to 'fml/lib/IO')
| -rw-r--r-- | fml/lib/IO/File/Atomic.pm | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/fml/lib/IO/File/Atomic.pm b/fml/lib/IO/File/Atomic.pm index f3f0c5db..ebd878d9 100644 --- a/fml/lib/IO/File/Atomic.pm +++ b/fml/lib/IO/File/Atomic.pm @@ -57,6 +57,11 @@ You can use this method to open $file for both read and write. $wh->close; $rh->close; +To copy from $src to $dst, + + IO::File::Atomic->copy($src, $dst) || croak("fail to copy"); + + =head1 DESCRIPTION =cut @@ -148,6 +153,31 @@ sub close } +# 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. |
