summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfukachan <fukachan>2001-02-01 12:40:37 +0000
committerfukachan <fukachan>2001-02-01 12:40:37 +0000
commit5c2ac1982cba4bee94de17d63c9399e99143a2bc (patch)
treed07c957bfa4e86bef7d0718df2c9e611f1d22cd9
parentcd00a759db54b036d316c6c8acf535fc2beab591 (diff)
downloadfml8-5c2ac1982cba4bee94de17d63c9399e99143a2bc.tar.gz
fml8-5c2ac1982cba4bee94de17d63c9399e99143a2bc.tar.bz2
fml8-5c2ac1982cba4bee94de17d63c9399e99143a2bc.zip
IO::File::Atomic::copy(src, dst)
-rw-r--r--fml/lib/IO/File/Atomic.pm30
-rw-r--r--regress/base/copy.pl20
2 files changed, 50 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.
diff --git a/regress/base/copy.pl b/regress/base/copy.pl
new file mode 100644
index 00000000..049ff5ba
--- /dev/null
+++ b/regress/base/copy.pl
@@ -0,0 +1,20 @@
+#!/usr/local/bin/perl
+#-*- 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$
+#
+
+use IO::File::Atomic;
+
+($src, $dst) = @ARGV;
+print "IO::File::Atomic->copy($src, $dst);\n";
+print "sleep 3;\n";
+sleep 3;
+my $status = IO::File::Atomic->copy($src, $dst) || die("fail to copy");
+
+exit 0;