From 5c2ac1982cba4bee94de17d63c9399e99143a2bc Mon Sep 17 00:00:00 2001 From: fukachan Date: Thu, 1 Feb 2001 12:40:37 +0000 Subject: IO::File::Atomic::copy(src, dst) --- fml/lib/IO/File/Atomic.pm | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'fml') 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. -- cgit v1.2.1